1、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
2、在application.properties中配置mq地址
# spring.rabbitmq.host=10.10.1.1 spring.rabbitmq.port=5672 spring.rabbitmq.virtual-host=/ spring.rabbitmq.username=guest spring.rabbitmq.password=guest
3、新建配置类
@Configuration public class WebClientAutoConfiguration { @Bean public OkHttp3ClientHttpRequestFactory okHttp3ClientHttpRequestFactory() { OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(); return factory; } @Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(okHttp3ClientHttpRequestFactory()); return restTemplate; } }
@Configuration public class RabbitmqMessageConverterConfiguration { @Bean public ClassMapper defaultClassMapper() { Map<String, Class<?>> idClassMapping = new HashMap<String, Class<?>>(); //如果有新的消息类,往map里添加即可 idClassMapping.put("RedbagSendMessage", RedbagSendMessage.class); DefaultClassMapper bean = new DefaultClassMapper(); bean.setIdClassMapping(idClassMapping); return bean; } @Bean public MessageConverter jackson2JsonMessageConverter() { Jackson2JsonMessageConverter bean = new Jackson2JsonMessageConverter(); bean.setClassMapper(defaultClassMapper()); return bean; } }
@Configuration public class RabbitmqConfiguration { public static final String INTEGRAL_MALL_SEND_REDBAG_EXCHANGE = "integral:mall:send:redbag:exchange"; public static final String USER_INTEGRAL_CALCULATE_QUEN = "integral.mall.send.redbag.queue"; //消费者需要添加绑定关系,按照下边注释部分配置即可。生产者可以不用 注意:测试的时候,同一队列消息的生产者和消费者不能在同一个项目里,否则消息收不到。 实际上也不会存在这样的场景 // @Bean // public FanoutExchange smsSendExchange() { // return new FanoutExchange(INTEGRAL_MALL_SEND_REDBAG_EXCHANGE); // } // @Bean // public Queue messageQueue() { // return new Queue(USER_INTEGRAL_CALCULATE_QUEN); // } // @Bean // public Binding messageBinding() { // return BindingBuilder.bind(messageQueue()).to(smsSendExchange()); // } @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jackson2JsonMessageConverter()); return template; } @Bean public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); factory.setMessageConverter(new Jackson2JsonMessageConverter()); return factory; } }
4、消息体
public class RedbagSendMessage { private Long userId; //get set方法 }
5、消息生产者代码用例
@Component public class RedbagSendProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendUserRedbag(Long userId){ RedbagSendMessage message = new RedbagSendMessage(); message .setUserId(userId); rabbitTemplate.convertAndSend(RabbitmqConfiguration.INTEGRAL_MALL_SEND_REDBAG_EXCHANGE, "", message); } }
6、消息消费者代码用例
@Component public class QueueConsumer { @RabbitListener(queues = RabbitmqConfiguration.USER_INTEGRAL_CALCULATE_QUEN) @RabbitHandler public void process(RedbagSendMessage message) { //接受到消息后,处理 相关业务 System.out.println(JSON.toJSON(message)); } }
7、启动类
@SpringBootApplication @ComponentScan("com.xxx.xx") public class IntegralServerApplication { public static void main(String[] args) { SpringApplication.run(IntegralServerApplication .class, args); } }