RabbitMQ与springboot整合

1、基本概念
  • Server:接收客户端的连接,实现AMQP实体服务;
  • Connection:连接,应用程序与Server的网络连接,TCP连接;
  • Channel:信道,消息读写等操作在信道中进行。客户端可以建立多个信道,每个信道代表一个会话任务;
  • Message:消息,应用程序和服务器之间传送的数据,消息可以非常简单,也可以很复杂。由Properties和Body组成。Properties为外包装,可以对消息进行修饰,比如消息的优先级、延迟等高级特性;Body就是消息体内容;
  • Virtual Host:虚拟主机,用于逻辑隔离。一个虚拟主机里面可以有若干个Exchange和Queue,同一个虚拟主机里面不能有相同名称的Exchange或Queue;
  • Exchange:交换器,接收消息,按照路由规则将消息路由到一个或者多个队列。如果路由不到,或者返回给生产者,或者直接丢弃。RabbitMQ常用的交换器常用类型有direct、topic、fanout、headers四种,后面详细介绍;
  • Binding:绑定,交换器和消息队列之间的虚拟连接,绑定中可以包含一个或者多个RoutingKey;
  • RoutingKey:路由键,生产者将消息发送给交换器的时候,会发送一个RoutingKey,用来指定路由规则,这样交换器就知道把消息发送到哪个队列。路由键通常为一个“.”分割的字符串,例如“com.rabbitmq”;
  • Queue:消息队列,用来保存消息,供消费者消费;

在这里插入图片描述
交换器:
在这里插入图片描述

2、RabbitMQ与springboot整合(Gradle项目):

build.gradle:

plugins {id 'java'id 'org.springframework.boot' version '3.1.1'id 'io.spring.dependency-management' version '1.1.0'
}group = 'com.kexuexiong'
version = '0.0.1-SNAPSHOT'java {sourceCompatibility = '17'
}configurations {compileOnly {extendsFrom annotationProcessor}
}repositories {
//	mavenCentral()maven {url 'https://maven.aliyun.com/repository/public'}
}dependencies {implementation 'org.springframework.boot:spring-boot-starter-jdbc'implementation 'org.springframework.boot:spring-boot-starter-validation'implementation 'org.springframework.boot:spring-boot-starter-web'implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.2'compileOnly 'org.projectlombok:lombok'runtimeOnly 'com.mysql:mysql-connector-j'annotationProcessor 'org.projectlombok:lombok'testImplementation 'org.springframework.boot:spring-boot-starter-test'testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.2'// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqpimplementation 'org.springframework.boot:spring-boot-starter-amqp'implementation 'cn.hutool:hutool-all:5.8.16'
}tasks.named('test') {useJUnitPlatform()
}

yml:
使用的RabbitMQ的集群部署,192.168.49.10:5672,192.168.49.9:5672,192.168.49.11:5672

server:port: 8014spring:rabbitmq:username: adminpassword: 123456dynamic: true
#    port: 5672
#    host: 192.168.49.9addresses: 192.168.49.10:5672,192.168.49.9:5672,192.168.49.11:5672publisher-confirm-type: correlatedpublisher-returns: trueapplication:name: shushandatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://ip/shushanusername: rootpassword: hikari:minimum-idle: 10maximum-pool-size: 20idle-timeout: 50000max-lifetime: 540000connection-test-query: select 1connection-timeout: 600000

下面根据三种交换机的类型举例子:DirectExchange、TopicExchange、FanoutExchange。

项目所使用到的常量:

package com.kexuexiong.shushan.common.mq;public class MqConstant {public final static String demoDirectQueue = "demoDirectQueue";public final static String demoDirectExchange = "demoDirectExchange";public final static String demoDirectRouting = "demoDirectRouting";public final static String lonelyDirectExchange = "lonelyDirectExchange";public final static String topicExchange = "topicExchange";public final static String BIG_CAR_TOPIC = "topic.big_car";public final static String SMALL_CAR_TOPIC = "topic.small_car";public final static String TOPIC_ALL = "topic.#";public final static String FANOUT_A = "fanout.A";public final static String FANOUT_B = "fanout_B";public final static String FANOUT_C = "fanout_c";public final static String FANOUT_EXCHANGE = "fanoutExchange";public final static String DEAD_LETTER_EXCHANGE = "dead.letter.exchange";public final static String DEAD_LETTER_QUEUE = "dead.letter.queue";public final static String DEAD_LETTER_ROUTING_KEY = "dead.letter.routing.key";public final static String BUSINESS_QUEUE = "business.queue";public final static String BUSINESS_ROUTING_KEY = "business.routing.key";public final static String BUSINESS_EXCHANGE = "business.exchange";}
3、Direct模式

config:

package com.kexuexiong.shushan.common.mq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DirectRabbitConfig {@Beanpublic Queue demoDirectQueue() {return new Queue(MqConstant.demoDirectQueue, true, false, false);}@BeanDirectExchange demoDirectExchange() {return new DirectExchange(MqConstant.demoDirectExchange, true, false);}@BeanBinding bingingDirect() {return BindingBuilder.bind(demoDirectQueue()).to(demoDirectExchange()).with(MqConstant.demoDirectRouting);}@BeanDirectExchange lonelyDirectExchange() {return new DirectExchange(MqConstant.lonelyDirectExchange);}}

DirectReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Component
@Slf4j
@RabbitListener(queues = MqConstant.demoDirectQueue)
public class DirectReceiver {@RabbitHandlerpublic void process(Map msg){log.info("1---receiver msg:"+msg.toString());}
}

DirectReceiverV2 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Component
@Slf4j
@RabbitListener(queues = MqConstant.demoDirectQueue)
public class DirectReceiverV2 {@RabbitHandlerpublic void process(Map msg){log.info("2---receiver msg:"+msg.toString());}
}

MqController 生产者:

package com.kexuexiong.shushan.controller.mq;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.kexuexiong.shushan.common.mq.MqConstant;
import com.kexuexiong.shushan.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;@Slf4j
@RestController
@RequestMapping("/mq/")
public class MqController extends BaseController {@AutowiredRabbitTemplate rabbitTemplate;@GetMapping("/sendDirectMessage")public String sendDirectMessage(){String msgId = UUID.randomUUID().toString();String msg = "demo msg ,kexuexiong";String createTime = DateUtil.format(new Date(),"YYYY-MM-dd HH:mm:ss");Map<String,Object> map = new HashMap();map.put("msgId",msgId);map.put("msg",msg);map.put("createTime",createTime);rabbitTemplate.convertAndSend("demoDirectExchange","demoDirectRouting",map);return "ok";}

测试:
在这里插入图片描述
结果:

2023-10-10T16:33:09.411+08:00  INFO 27232 --- [nio-8014-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-10-10T16:33:09.412+08:00  INFO 27232 --- [nio-8014-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-10-10T16:33:09.413+08:00  INFO 27232 --- [nio-8014-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2023-10-10T16:33:09.471+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:09.471+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:09.472+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:09.481+08:00  INFO 27232 --- [ntContainer#0-1] c.k.shushan.common.mq.DirectReceiver     : 1---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:09, msgId=e2dfe4c7-22b5-42b7-8f7a-967148472eaa}
2023-10-10T16:33:28.327+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:28.327+08:00  INFO 27232 --- [ntContainer#1-1] c.k.shushan.common.mq.DirectReceiverV2   : 2---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:28, msgId=9c3318df-35a1-44c3-8ac3-395e7932c45d}
2023-10-10T16:33:28.327+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:28.327+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:29.047+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:29.047+08:00  INFO 27232 --- [ntContainer#0-1] c.k.shushan.common.mq.DirectReceiver     : 1---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:29, msgId=c5959bbd-dfb2-485f-86f1-19e0617d9e30}
2023-10-10T16:33:29.047+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:29.047+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:38.846+08:00  INFO 27232 --- [ntContainer#1-1] c.k.shushan.common.mq.DirectReceiverV2   : 2---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:38, msgId=7b38272e-133e-4aac-affc-4dc22a4d3ade}
2023-10-10T16:33:38.846+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:38.846+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:38.846+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:39.588+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:39.588+08:00  INFO 27232 --- [ntContainer#0-1] c.k.shushan.common.mq.DirectReceiver     : 1---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:39, msgId=7ddaf70b-db56-440e-b32e-21c299cfd374}
2023-10-10T16:33:39.588+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:39.588+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:40.307+08:00  INFO 27232 --- [ntContainer#1-1] c.k.shushan.common.mq.DirectReceiverV2   : 2---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:40, msgId=2168972a-1f29-46a1-9c0f-1d90871d6aee}
2023-10-10T16:33:40.307+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:40.307+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:40.307+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:40.962+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:40.962+08:00  INFO 27232 --- [ntContainer#0-1] c.k.shushan.common.mq.DirectReceiver     : 1---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:40, msgId=3c2c55b7-746a-4c3b-9d4d-da1f52a7e32a}
2023-10-10T16:33:40.962+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:40.962+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
2023-10-10T16:33:41.710+08:00  INFO 27232 --- [ntContainer#1-1] c.k.shushan.common.mq.DirectReceiverV2   : 2---receiver msg��{msg=demo msg ,kexuexiong, createTime=2023-10-10 16:33:41, msgId=e276c091-6526-4c1f-ba18-76c6aa7577d7}
2023-10-10T16:33:41.711+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:33:41.711+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:33:41.711+08:00  INFO 27232 --- [nectionFactory1] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null
4、Topic模式

TopicRabbitConfig :

package com.kexuexiong.shushan.common.mq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TopicRabbitConfig {@Beanpublic Queue bigCarQueue(){return new Queue(MqConstant.BIG_CAR_TOPIC);}@Beanpublic Queue smallCarQueue(){return new Queue(MqConstant.SMALL_CAR_TOPIC);}@Beanpublic TopicExchange exchange(){return new TopicExchange(MqConstant.topicExchange);}@BeanBinding bindingExchangeMessage(){return BindingBuilder.bind(bigCarQueue()).to(exchange()).with(MqConstant.BIG_CAR_TOPIC);}@Beanpublic Binding bindingExchangeMessageSmall(){return BindingBuilder.bind(smallCarQueue()).to(exchange()).with(MqConstant.TOPIC_ALL);}}

TopicBigCarReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Slf4j
@Component
@RabbitListener(queues = MqConstant.BIG_CAR_TOPIC)
public class TopicBigCarReceiver {@RabbitHandlerpublic void process(Map msg){log.info("topicBigCarReceiver msg :"+msg);}
}

TopicSmallCarReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Slf4j
@Component
@RabbitListener(queues = MqConstant.SMALL_CAR_TOPIC)
public class TopicSmallCarReceiver {@RabbitHandlerpublic void process(Map msg){log.info("TopicSmallCarReceiver msg :"+msg);}
}
package com.kexuexiong.shushan.controller.mq;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.kexuexiong.shushan.common.mq.MqConstant;
import com.kexuexiong.shushan.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;@Slf4j
@RestController
@RequestMapping("/mq/")
public class MqController extends BaseController {@AutowiredRabbitTemplate rabbitTemplate;@GetMapping("/sendTopicMessageBigCar")public String sendTopicMessageBigCar(){String msgId = UUID.randomUUID().toString();String msg = "demo msg ,BIG CAR";String createTime = DateUtil.format(new Date(),"YYYY-MM-dd HH:mm:ss");Map<String,Object> map = new HashMap();map.put("msgId",msgId);map.put("msg",msg);map.put("createTime",createTime);rabbitTemplate.convertAndSend(MqConstant.topicExchange,MqConstant.BIG_CAR_TOPIC,map);return "ok";}@GetMapping("/sendTopicMessageSmallCar")public String sendTopicMessageSmallCar(){String msgId = UUID.randomUUID().toString();String msg = "demo msg ,small CAR";String createTime = DateUtil.format(new Date(),"YYYY-MM-dd HH:mm:ss");Map<String,Object> map = new HashMap();map.put("msgId",msgId);map.put("msg",msg);map.put("createTime",createTime);rabbitTemplate.convertAndSend(MqConstant.topicExchange,MqConstant.SMALL_CAR_TOPIC,map);return "ok";}}

在这里插入图片描述

2023-10-10T16:37:05.876+08:00  INFO 27232 --- [ntContainer#5-1] c.k.s.common.mq.TopicBigCarReceiver      : topicBigCarReceiver msg :{msg=demo msg ,BIG CAR, createTime=2023-10-10 16:37:05, msgId=333bb01b-0bf9-4d24-b140-f2814fb0e416}
2023-10-10T16:37:05.876+08:00  INFO 27232 --- [ntContainer#6-1] c.k.s.common.mq.TopicSmallCarReceiver    : TopicSmallCarReceiver msg :{msg=demo msg ,BIG CAR, createTime=2023-10-10 16:37:05, msgId=333bb01b-0bf9-4d24-b140-f2814fb0e416}
2023-10-10T16:37:05.878+08:00  INFO 27232 --- [nectionFactory2] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:37:05.879+08:00  INFO 27232 --- [nectionFactory2] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:37:05.879+08:00  INFO 27232 --- [nectionFactory2] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null

应为SMALL_CAR_TOPIC既符合topic.big_car也符合topic.#,所以消息都会被路由进SMALL_CAR_TOPIC队列和BIG_CAR_TOPIC队列中。

在这里插入图片描述

2023-10-10T16:42:07.369+08:00  INFO 27232 --- [ntContainer#6-1] c.k.s.common.mq.TopicSmallCarReceiver    : TopicSmallCarReceiver msg :{msg=demo msg ,small CAR, createTime=2023-10-10 16:42:07, msgId=fa42a681-22cc-4489-b816-c2fae6050b98}
2023-10-10T16:42:07.370+08:00  INFO 27232 --- [nectionFactory3] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:42:07.370+08:00  INFO 27232 --- [nectionFactory3] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:42:07.370+08:00  INFO 27232 --- [nectionFactory3] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null

该消息只有TopicSmallCarReceiver 消费。

5、Fanout模式

FanoutRabbitConfig :

package com.kexuexiong.shushan.common.mq;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutRabbitConfig {@Beanpublic Queue queueA(){return new Queue(MqConstant.FANOUT_A);}@Beanpublic Queue queueB(){return new Queue(MqConstant.FANOUT_B);}@Beanpublic Queue queueC(){return new Queue(MqConstant.FANOUT_C);}@BeanFanoutExchange fanoutExchange(){return new FanoutExchange(MqConstant.FANOUT_EXCHANGE);}@Beanpublic Binding bindingExchangeA(){return BindingBuilder.bind(queueA()).to(fanoutExchange());}@Beanpublic Binding bindingExchangeB(){return BindingBuilder.bind(queueB()).to(fanoutExchange());}@Beanpublic Binding bindingExchangeC(){return BindingBuilder.bind(queueC()).to(fanoutExchange());}}

FanoutAReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Slf4j
@Component
@RabbitListener(queues = MqConstant.FANOUT_A)
public class FanoutAReceiver {@RabbitHandlerpublic void process(Map msg){log.info("FanoutAReceiver msg :"+msg);}
}

FanoutBReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Slf4j
@Component
@RabbitListener(queues = MqConstant.FANOUT_B)
public class FanoutBReceiver {@RabbitHandlerpublic void process(Map msg){log.info("FanoutBReceiver msg :"+msg);}
}

FanoutCReceiver 消费者:

package com.kexuexiong.shushan.common.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;import java.util.Map;@Slf4j
@Component
@RabbitListener(queues = MqConstant.FANOUT_C)
public class FanoutCReceiver {@RabbitHandlerpublic void process(Map msg){log.info("FanoutCReceiver msg :"+msg);}
}

MqController 生产者:

package com.kexuexiong.shushan.controller.mq;import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.kexuexiong.shushan.common.mq.MqConstant;
import com.kexuexiong.shushan.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;@Slf4j
@RestController
@RequestMapping("/mq/")
public class MqController extends BaseController {@AutowiredRabbitTemplate rabbitTemplate;@GetMapping("/sendTopicMessageFanoutMsg")public String sendTopicMessageFanoutMsg(){String msgId = UUID.randomUUID().toString();String msg = "demo msg ,fanout msg";String createTime = DateUtil.format(new Date(),"YYYY-MM-dd HH:mm:ss");Map<String,Object> map = new HashMap();map.put("msgId",msgId);map.put("msg",msg);map.put("createTime",createTime);rabbitTemplate.convertAndSend(MqConstant.FANOUT_EXCHANGE,null,map);return "ok";}
}

测试:
在这里插入图片描述

2023-10-10T16:46:36.226+08:00  INFO 27232 --- [ntContainer#4-1] c.k.shushan.common.mq.FanoutCReceiver    : FanoutCReceiver msg :{msg=demo msg ,fanout msg, createTime=2023-10-10 16:46:36, msgId=eca24bc1-4c70-456a-b026-b5d9b7ef0c21}
2023-10-10T16:46:36.226+08:00  INFO 27232 --- [ntContainer#3-1] c.k.shushan.common.mq.FanoutBReceiver    : FanoutBReceiver msg :{msg=demo msg ,fanout msg, createTime=2023-10-10 16:46:36, msgId=eca24bc1-4c70-456a-b026-b5d9b7ef0c21}
2023-10-10T16:46:36.226+08:00  INFO 27232 --- [ntContainer#2-1] c.k.shushan.common.mq.FanoutAReceiver    : FanoutAReceiver msg :{msg=demo msg ,fanout msg, createTime=2023-10-10 16:46:36, msgId=eca24bc1-4c70-456a-b026-b5d9b7ef0c21}
2023-10-10T16:46:36.229+08:00  INFO 27232 --- [nectionFactory4] c.k.shushan.common.config.RabbitConfig   : confirmCallback  data: null
2023-10-10T16:46:36.229+08:00  INFO 27232 --- [nectionFactory4] c.k.shushan.common.config.RabbitConfig   : confirmCallback ack :true
2023-10-10T16:46:36.229+08:00  INFO 27232 --- [nectionFactory4] c.k.shushan.common.config.RabbitConfig   : confirmCallback cause :null

FanoutAReceiver 、FanoutBReceiver 、FanoutCReceiver 都收到了消息,相当于广播。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/130808.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Congestion Control for Large-Scale RDMA Deployments

文章目录 IntroductionDCQCNBuffer Setting Introduction PFC是粗粒度的流量控制机制&#xff0c;在端口层面发挥作用&#xff0c;不区别不同的流。这会导致很多弊端&#xff0c;比如不公平&#xff0c;受害流等。 解决PFC限制的解决方法是flow-level的拥塞控制&#xff0c;D…

目标识别项目实战:基于Yolov7-LPRNet的动态车牌目标识别算法模型(三)

前言 目标识别如今以及迭代了这么多年&#xff0c;普遍受大家认可和欢迎的目标识别框架就是YOLO了。按照官方描述&#xff0c;YOLOv8 是一个 SOTA 模型&#xff0c;它建立在以前 YOLO 版本的成功基础上&#xff0c;并引入了新的功能和改进&#xff0c;以进一步提升性能和灵活性…

Android开发-Android项目Jenkins自动化打包流程搭建与配置

Android 项目 Jenkins 自动化打包流程搭建与配置 1. 前言2. Jenkins 下载3. 配置电脑的 JDK 环境4. Jenkins 安装和设置5. Jenkins 设置 Android 项目自动打包流程 1. 前言 由于之前公司的 Android 项目需要 APK 自动打包的功能&#xff0c;所以需要搭建 Jenkins 自动化打包的…

【云计算网络安全】僵尸网络详解:工作原理、控制和保护方法

文章目录 一、什么是僵尸网络&#xff1f;二、僵尸网络因为什么原因而诞生&#xff1f;三、僵尸网络主要用途四、僵尸网络如何工作&#xff1f;五、如何控制僵尸网络&#xff1f;5.1 客户端/服务器僵尸网络模型5.1.1 星形网络拓扑5.1.2 多服务器网络拓扑5.1.3 分层网络拓扑 5.2…

3D开发工具HOOPS助力Eleven Dynamics加速开发QA自动化平台

Nexos平台提供强大的可视化功能&#xff0c;并将整体测量时间减少80%。 2021年10月19日&#xff0c;俄勒冈州本德市&#xff08;Newswire.com&#xff09;——工程软件开发工具包的领先供应商Tech Soft 3D今天宣布&#xff0c;Eleven Dynamics是一家位于瑞士的初创公司&#x…

知识增强语言模型提示 零样本知识图谱问答10.8

知识增强语言模型提示 零样本知识图谱问答 摘要介绍相关工作方法零样本QA的LM提示知识增强的LM提示与知识问题相关的知识检索 摘要 大型语言模型&#xff08;LLM&#xff09;能够执行 零样本closed-book问答任务 &#xff0c;依靠其在预训练期间存储在参数中的内部知识。然而&…

在SOLIDWORKS搭建一个简易的履带式机器人

文章目录 前言一、构建模型基本单元二、搭建车体模块三.插入轮子4.构建履带 前言 趁着十一假期&#xff0c;在solidworks中搭建了一个履带式机器人小车&#xff0c;计划将其应用在gazebo中完成多机器人编队的仿真。 一、构建模型基本单元 构建底板&#xff08;a面&#xff09…

Spring5应用之事务处理

作者简介&#xff1a;☕️大家好&#xff0c;我是Aomsir&#xff0c;一个爱折腾的开发者&#xff01; 个人主页&#xff1a;Aomsir_Spring5应用专栏,Netty应用专栏,RPC应用专栏-CSDN博客 当前专栏&#xff1a;Spring5应用专栏_Aomsir的博客-CSDN博客 文章目录 参考文献前言事务…

arcgis添加天地图山东wtms服务

arcgis添加天地图wtms服务 首先打开天地图山东网站&#xff0c;进入首页![天地图山东首页](https://img-blog.csdnimg.cn/89df69e1c3d645b4a9e9652a08580342.png)然后点击开发资源进入开发页面点击成为开发者&#xff0c;进入申请秘钥页面&#xff0c;申请秘钥&#xff1b;![在…

Spring5应用之整合MyBatis

作者简介&#xff1a;☕️大家好&#xff0c;我是Aomsir&#xff0c;一个爱折腾的开发者&#xff01; 个人主页&#xff1a;Aomsir_Spring5应用专栏,Netty应用专栏,RPC应用专栏-CSDN博客 当前专栏&#xff1a;Spring5应用专栏_Aomsir的博客-CSDN博客 文章目录 参考文献前言为什…

Python图形界面框架PyQt5使用详解

概要 使用Python开发图形界面的软件其实并不多&#xff0c;相对于GUI界面&#xff0c;可能Web方式的应用更受人欢迎。但对于像我一样对其他编程语言比如C#或WPF并不熟悉的人来说&#xff0c;未必不是一个好的工具。 常见GUI框架 PyQt5&#xff1a;Qt是一个跨平台的 C图形用户界…

sanic框架解决多进程共享缓存问题

最近在用sanic框架做项目&#xff0c;今天需要处理多进程共享缓存问题&#xff0c;在网上搜索了很多&#xff0c;知道使用multiprocessing模块&#xff0c;但是导入后&#xff0c;直接使用会报错&#xff0c;然后看官网解决问题。 直接看官方文档点我哦 大致意思如下&#xf…