springboot集成使用rabbitmq笔记(2.rabbitmq使用)
1.引入包
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!-- spring-web相关 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 工具 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
2.配置
spring: rabbitmq: addresses: 192.168.37.134 port: 5672 username: admin password: admin server: port: 8035 context-path: / rabbit: queueName: testQueue1: testqueue1 testQueue2: testqueue2 testQueue3: testqueue3 exchange: exchangeName: test_publish_exchange
3.使用代码
3.1.配置类生成使用的bean
@Configuration public class RabbitmqConfig { @Value("${rabbit.queueName.testQueue1}") private String testQueue1; @Value("${rabbit.queueName.testQueue2}") private String testQueue2; @Value("${rabbit.queueName.testQueue3}") private String testQueue3; @Value("${rabbit.exchange.exchangeName}") private String exchangeName; /**========================生产者配置===================*/ @Bean public Exchange testExchange() { return new TopicExchange(exchangeName); } /**===================消费者配置===================*/ @Bean public Queue getQueue1(){ return new Queue(testQueue1); } @Bean public Queue getQueue2(){ return new Queue(testQueue2); } @Bean public Queue getQueue3(){ return new Queue(testQueue3); } @Bean public Binding testConsumeBinding1() { //消费队列绑定 return new Binding(testQueue1, Binding.DestinationType.QUEUE, exchangeName,"*.test1.*", null); } @Bean public Binding testConsumeBinding2() { //消费队列绑定 return new Binding(testQueue2, Binding.DestinationType.QUEUE, exchangeName,"*.*.test2", null); } @Bean public Binding testConsumeDlxBinding3() { //消费队列绑定 return new Binding(testQueue3, Binding.DestinationType.QUEUE, exchangeName,"test3.#", null); } }
3.2.发送消息
@RestController @Slf4j public class RabbitMQProduceController { /** 发送消息注入 AmqpTemplate*/ @Autowired private AmqpTemplate rabbitTemplate; @Value("${rabbit.queueName.testQueue1}") private String testQueue; @RequestMapping(value = "/send") public String sendMsg(String msg){ send("test.test1.test2",msg+"-send1"); log.info("send1发送消息成功,routingKey:test.test1.test2,消息:{}",msg+"-send1"); send("test3.test1.test",msg+"-send2"); log.info("send2发送消息成功,routingKey:test3.test1.test,消息:{}",msg+"-send2"); send("test3.test2.test1",msg+"-send3"); log.info("send3发送消息成功,routingKey:test3.test2.test1,消息:{}",msg+"-send3"); return "ok"; } private void send(String routingKey,Object content) { rabbitTemplate.convertAndSend("test_publish_exchange",routingKey,content); } }
3.3.接收消息
@Component @Slf4j public class RecieveListener { @RabbitListener(queues = "${rabbit.queueName.testQueue1}") public void processMsg(Message msg) { log.info("1(*.test1.*).接收rabbitmq的msg : {}", msg.getPayload()); } } @Component @RabbitListener(queues = "${rabbit.queueName.testQueue2}") @Slf4j public class RecieveListener2 { @RabbitHandler public void processMsg(@Payload String msg) { log.info("2(*.*.test2).接收rabbitmq的msg : {}", msg.toString()); } } @Component @Slf4j public class RecieveListener3 { @RabbitListener(queues = "${rabbit.queueName.testQueue3}") public void processMsg(@Payload String msg) { log.info("3(test3.#).接收rabbitmq的msg : {}", msg); } }
3.4.启动并访问接口http://localhost:8035/send?msg=message
INFO 10976 --- [nio-8035-exec-1] c.e.s.produce.RabbitMQProduceController : send1发送消息成功,routingKey:test.test1.test2,消息:message-send1 INFO 10976 --- [nio-8035-exec-1] c.e.s.produce.RabbitMQProduceController : send2发送消息成功,routingKey:test3.test1.test,消息:message-send2 INFO 10976 --- [nio-8035-exec-1] c.e.s.produce.RabbitMQProduceController : send3发送消息成功,routingKey:test3.test2.test1,消息:message-send3 INFO 10976 --- [cTaskExecutor-1] c.e.s.consumer.RecieveListener3 : 3(test3.#).接收rabbitmq的msg : message-send2 INFO 10976 --- [cTaskExecutor-1] c.e.s.consumer.RecieveListener2 : 2(*.*.test2).接收rabbitmq的msg : message-send1 INFO 10976 --- [cTaskExecutor-1] c.e.s.consumer.RecieveListener : 1(*.test1.*).接收rabbitmq的msg : message-send1 INFO 10976 --- [cTaskExecutor-1] c.e.s.consumer.RecieveListener : 1(*.test1.*).接收rabbitmq的msg : message-send2 INFO 10976 --- [cTaskExecutor-1] c.e.s.consumer.RecieveListener3 : 3(test3.#).接收rabbitmq的msg : message-send3