当前位置:   article > 正文

RabbitMQ学习(三)-- Spring boot 集成 RabbitTemplate_springboot rabbittemplate

springboot rabbittemplate

1,引包

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-amqp</artifactId>
  4. <version>2.4.0</version>
  5. </dependency>

2,添加配置

  1. spring:
  2. rabbitmq:
  3. addresses: 10.211.55.7
  4. port: 5672
  5. username: guest
  6. password: guest
  7. virtual-host: /

3,添加RabbitConfig

  1. package com.wayne.core.config;
  2. import com.rabbitmq.client.AMQP;
  3. import org.springframework.amqp.core.*;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.stereotype.Component;
  8. @Configuration
  9. public class RabbitConfig {
  10. public static final String EXCHANGE_NAME = "boot_topic_exchange";
  11. public static final String QUEUE_NAME = "boot_queue";
  12. //1,交换机对象
  13. @Bean("buildExchange")
  14. public Exchange buildExchange(){
  15. return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
  16. }
  17. //2,队列
  18. @Bean("buildQueue")
  19. public Queue buildQueue(){
  20. return QueueBuilder.durable(QUEUE_NAME).build();
  21. }
  22. //3,绑定交换机和队列
  23. @Bean
  24. public Binding bindQueueExchange(@Qualifier("buildQueue") Queue queue,@Qualifier("buildExchange") Exchange exchange){
  25. return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
  26. }
  27. }

4,添加生产者

  1. @Autowired
  2. private RabbitTemplate rabbitTemplate;
  3. @PostMapping("/send")
  4. public void sendMessage(){
  5. rabbitTemplate.convertAndSend(EXCHANGE_NAME,"boot.hahah","hello spring boot rabbitMQ!~~~~~~~");
  6. }

5,添加消费者

  1. @RabbitListener(queues = QUEUE_NAME)
  2. public void rabbitMQConsumerListener(Message message){
  3. log.info("当前队列信息为"+message);
  4. }

6,测试

  1. 2020-12-14 00:13:25,635 INFO (AbstractConnectionFactory.java:558)- Attempting to connect to: [10.211.55.7:5672]
  2. 2020-12-14 00:13:25,663 INFO (AbstractConnectionFactory.java:511)- Created new connection: rabbitConnectionFactory#46a488c2:0/SimpleConnection@1ecf0ac6 [delegate=amqp://guest@10.211.55.7:5672/, localPort= 55692]
  3. 2020-12-14 00:13:25,711 INFO (RabbitMQConsumerQueue.java:16)- 当前队列信息为(Body:'hello spring boot rabbitMQ!~~~~~~~' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hahah, deliveryTag=1, consumerTag=amq.ctag-p9F4ZgPsG4ECSHqMBmjRfA, consumerQueue=boot_queue])
  4. 2020-12-14 00:13:25,711 INFO (StartupInfoLogger.java:61)- Started SpringBootDemo in 2.389 seconds (JVM running for 2.974)
  5. 2020-12-14 00:13:37,691 INFO (DirectJDKLog.java:173)- Initializing Spring DispatcherServlet 'dispatcherServlet'
  6. 2020-12-14 00:13:37,691 INFO (FrameworkServlet.java:525)- Initializing Servlet 'dispatcherServlet'
  7. 2020-12-14 00:13:37,697 INFO (FrameworkServlet.java:547)- Completed initialization in 6 ms
  8. 2020-12-14 00:13:37,718 INFO (RabbitMQConsumerQueue.java:16)- 当前队列信息为(Body:'hello spring boot rabbitMQ!~~~~~~~' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=boot_topic_exchange, receivedRoutingKey=boot.hahah, deliveryTag=2, consumerTag=amq.ctag-p9F4ZgPsG4ECSHqMBmjRfA, consumerQueue=boot_queue])

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/543486
推荐阅读
相关标签
  

闽ICP备14008679号