当前位置:   article > 正文

springboot集成rabbitmq,实现发送和接收信息_springboot使用rabbitmq实现数据的实时传输

springboot使用rabbitmq实现数据的实时传输

一、配置文件 、yml配置

  1. rabbitmq:
  2. username: admin
  3. password: admin
  4. #单机配置
  5. host: IP地址
  6. port: 端口
  7. # 集群配置 addresses: ip:端口,ip:端口,ip:端口
  8. publisher-confirms: true
  9. publisher-returns: true # 消息发送到交换机确认机制,是否返回回调
  10. virtual-host: /
  11. listener:
  12. simple:
  13. retry:
  14. enabled: true
  15. initial-interval: 6000
  16. acknowledge-mode: manual #采用手动应答

二、直接模式发送接收代码实现

   1.可配置启动时添加消息队列或页面添加队列
  1. @SpringBootConfiguration
  2. @ComponentScan(basePackages = {"扫描包路径"})
  3. public class RabbitMQConfig {
  4. @Bean
  5. public Queue quorumQueue() {
  6. return QueueBuilder
  7. .durable("quorumqueue") // 持久化
  8. .quorum() // 仲裁队列
  9. .build();
  10. }
  11. }

 

2.controller发送消息
  1. @Autowired
  2. private AmqpTemplate amqpTemplate;
  3. public R saveCalculatemq(@RequestBody JSONObject data){
  4. Map<String,Object> send = new HashMap<>(16);
  5. send.put("data",data);
  6. String message = JSON.toJSONString(send);
  7. //发送到消息队列
  8. amqpTemplate.convertAndSend("quorumqueue",message);
  9. return R.ok();
  10. }
3.接收消息
  1. @Component
  2. @Async
  3. public class QueueListner {
  4. @RabbitListener(queues={"quorumqueue"})
  5. public synchronized void receiveMessage(Message message, Channel channel){
  6. log.debug("消息接收到了:"+new String(message.getBody()));
  7. }
  8. }

三、广播模式发送接收代码实现

 1、代码实现广播模式交换机和队列或者页面操作配置
  1. package com.bonc.calculate.config;
  2. import org.springframework.amqp.core.*;
  3. import org.springframework.boot.SpringBootConfiguration;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. @SpringBootConfiguration
  7. @ComponentScan(basePackages = {"需要扫描的包"})
  8. public class FanoutConfig {
  9. //声明FanoutExchange交换机
  10. @Bean
  11. public FanoutExchange fanoutExchange() {
  12. return new FanoutExchange("fanout.exchange");//交换机名称
  13. }
  14. //声明第1个队列
  15. @Bean
  16. public Queue fanoutQueue1(){
  17. return QueueBuilder
  18. .durable("fanout.queue1") // 持久化
  19. .quorum() // 仲裁队列
  20. .build();
  21. }
  22. //绑定队列1和交换机
  23. @Bean
  24. public Binding bindingQueue1(Queue fanoutQueue1,FanoutExchange fanoutExchange){
  25. return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
  26. }
  27. //声明第2个队列
  28. @Bean
  29. public Queue fanoutQueue2(){
  30. return QueueBuilder
  31. .durable("fanout.queue2") // 持久化
  32. .quorum() // 仲裁队列
  33. .build();
  34. }
  35. //绑定队列2和交换机
  36. @Bean
  37. public Binding bindingQueue2(Queue fanoutQueue2,FanoutExchange fanoutExchange){
  38. return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
  39. }
  40. }
2、controller发送消息
 amqpTemplate.convertAndSend(交换机名称,"",发送的消息);
3、接收消息 
  1. package com.bonc.calculate.listener;
  2. import com.rabbitmq.client.Channel;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.amqp.core.Message;
  6. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  7. import org.springframework.scheduling.annotation.Async;
  8. import org.springframework.stereotype.Component;
  9. import java.util.Map;
  10. @Component
  11. @Async
  12. public class QueueListner {
  13. protected static final Logger log = LoggerFactory.getLogger(QueueBoncListner.class);
  14. @RabbitListener(queues={"fanout.queue1"})
  15. public synchronized void receiveMessage(Message message, Channel channel){
  16. log.debug("fanout.queue1消息接收到了:"+new String(message.getBody()));
  17. }
  18. @RabbitListener(queues={"fanout.queue2"})
  19. public synchronized void receiveMessage2(Message message, Channel channel){
  20. log.debug("fanout.queue2消息接收到了:"+new String(message.getBody()));
  21. }
  22. //页面配置好 也可以直接接收消息
  23. @RabbitListener(queues={"fanout_queue3"})
  24. public synchronized void receiveMessage3(Message message, Channel channel){
  25. log.debug("fanout.queue3消息接收到了:"+new String(message.getBody()));
  26. }
  27. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/399291
推荐阅读
相关标签
  

闽ICP备14008679号