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.rhkb.cn/news/154298.html

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

相关文章

Linux[find命令]-根据路径和条件搜索指定文件并删除

一、find命令简介 find命令&#xff1a;用于根据给定的路径和条件查找相关文件或目录&#xff0c;参数灵活方便&#xff0c;且支持正则表达式&#xff0c;结合管道符后能够实现更加复杂的功能。 基本语法格式&#xff1a;find pathname -options 搜索内容 [其他选项] pathname…

Python 自动化Web测试

限于作者水平有限&#xff0c;以下内容可能是管窥之见&#xff0c;希望大家高抬贵手&#xff0c;且让我斗胆抛砖引玉。 公司产品迪备主要是通过网页操作来进行数据库的备份与恢复&#xff0c;监控与管理&#xff0c;因此在测试的过程中&#xff0c;可以用python测试脚本来模拟…

LLVM(5)ORC实例分析

ORC实例总结 总结 因为API茫茫多&#xff0c;逻辑上的一些概念需要搞清&#xff0c;编码时会容易很多。JIT的运行实体使用LLVMOrcCreateLLJIT可以创建出来&#xff0c;逻辑上的JIT实例。JIT实例需要加入运行库&#xff08;依赖库&#xff09;和用户定义的context&#xff08;…

最新数据库流行度最新排名(每月更新)

2023年10月数据库流行度最新排名 TOP DB顶级数据库索引是通过分析在谷歌上搜索数据库名称的频率来创建的 一个数据库被搜索的次数越多&#xff0c;这个数据库就被认为越受欢迎。这是一个领先指标。原始数据来自谷歌Trends 如果您相信集体智慧&#xff0c;那么TOP DB索引可以帮…

flutter 常用组件:文本、图片和按钮

文章目录 文本控件富文本控件图片本地图片网络图片按钮文本控件 ##一’码’当先 Text(这是一段文本这是一段文本这是一段文本这是一段文本这是一段文本这是一段文本这是一段文本这是一段文本,textAlign:TextAlign.center,style: TextStyle(fontWeight: FontWeight.bold, font…

BC v1.2充电规范

1 JEITA Reference to https://www.mianbaoban.cn/blog/post/169964 符合 JEITA 规范的锂离子电池充电器解决方案 2 Battery Fuel Gauge 2.1 Cycle Count&#xff08;充放电循环次数&#xff09; 此指令回传一只读字段&#xff0c;代表电芯组已经历的完整充放电循环数。当放电容…

java 每种设计模式的作用,与应用场景

文章目录 前言java 每种设计模式的作用&#xff0c;与应用场景 前言 如果您觉得有用的话&#xff0c;记得给博主点个赞&#xff0c;评论&#xff0c;收藏一键三连啊&#xff0c;写作不易啊^ _ ^。   而且听说点赞的人每天的运气都不会太差&#xff0c;实在白嫖的话&#xff0…

2023品牌新媒体矩阵营销洞察报告:流量内卷下,如何寻找增长新引擎?

近年来&#xff0c;随着移动互联网的发展渗透&#xff0c;短视频、直播的兴起&#xff0c;新消费/新零售、兴趣电商/社交电商等的驱动下&#xff0c;布局线上渠道已成为绝大多数品牌的必然选择。 2022年&#xff0c;越来越多的品牌加入到自运营、自播的行列中&#xff0c;并且从…

SR660 V2 ESXI 的安装

连接BMC端口 登录BMC管理界面&#xff08;需要设置三个参数&#xff1a; IP DNS RAID &#xff09; 在网络设置里有IP DNS 的设置 配置IP 配置DNS Ctrl shift 选中物理驱动器配置里的两块磁盘 否则会弹出报错&#xff1a;最小值2物理设备应该按照所选的RAID等级来配置 配置…

java.util.concurrent.locks.Condition详解

Condition翻译成中文是“条件”&#xff0c;一般我们称其为条件变量&#xff0c;每一个Condition对象都通过链表保存了一个队列&#xff0c;我们称之为条件队列。 当然了&#xff0c;这里所说的Condition对象一般指的是Condition接口的实现类ConditionObject&#xff0c;比如我…

如何在 Spring Boot 中进行数据备份

在Spring Boot中进行数据备份 数据备份是确保数据安全性和可恢复性的关键任务之一。Spring Boot提供了多种方法来执行数据备份&#xff0c;无论是定期备份数据库&#xff0c;还是将数据导出到外部存储。本文将介绍在Spring Boot应用程序中进行数据备份的不同方法。 方法1: 使用…

CentOS 7 安装 MySQL8.0

由于centOS7中默认安装了 MariaDB , 需要先进行卸载 # 查看版本 rpm -qa | grep mariadb # 卸载 rpm -e --nodeps 文件名 # 查看是否卸载干净 rpm -qa | grep mariadb安装wget&#xff1a; yum -y install wget进入/usr/local/下&#xff1a; cd /usr/local/新建mysqlrpm文…

【unity】制作一个角色的初始状态(左右跳二段跳)【2D横板动作游戏】

前言 hi~ 大家好&#xff01;欢迎大家来到我的全新unity学习记录系列。现在我想在2d横板游戏中&#xff0c;实现一个角色的初始状态-闲置状态、移动状态、空中状态。并且是利用状态机进行实现的。 本系列是跟着视频教程走的&#xff0c;所写也是作者个人的学习记录笔记。如有错…

win10电脑插入耳机,右边耳机声音比左边小很多

最近使用笔记本看视频&#xff0c;发现插入耳机&#xff08;插入式和头戴式&#xff09;后&#xff0c;右边耳机声音比左边耳机声音小很多很多&#xff0c;几乎是一边很清晰&#xff0c;另一边什么都听不到。 将耳机插到别人电脑上测试耳机正常&#xff0c;那就是电脑的问题。试…

Spring AOP的失效场景

首先&#xff0c;Spring的AOP其实是通过动态代理实现的&#xff0c;所以&#xff0c;想要让AOP生效&#xff0c;前提必须是动态代理生效&#xff0c;并且可以调用到代理对象的方法什么情况下会不走代理对象的调用呢&#xff1f;首先就是类内部的调用&#xff0c;比如一些私有方…

Postman历史版本下载

1. 下载对应版本的postman 历史版本下载 请把下面链接的"版本号"替换为指定的版本号&#xff0c;例如&#xff1a;8.8.0 Windows64位 ​https://dl.pstmn.io/download/version/版本号/win64​ Windows32位 https://dl.pstmn.io/download/version/版本号…

GEO生信数据挖掘(六)实践案例——四分类结核病基因数据预处理分析

前面五节&#xff0c;我们使用阿尔兹海默症数据做了一个数据预处理案例&#xff0c;包括如下内容&#xff1a; GEO生信数据挖掘&#xff08;一&#xff09;数据集下载和初步观察 GEO生信数据挖掘&#xff08;二&#xff09;下载基因芯片平台文件及注释 GEO生信数据挖掘&…

matlab相机标定实验

实验原理 1. 相机标定坐标系 相机的参数对目标的识别、定位精度有很大的影响&#xff0c;相机标定就是为了求出相机的内外参数。标定中有3个不同层次的坐标系&#xff1a;世界坐标系、相机坐标系和图像坐标系&#xff08;图像物理坐标系和图像像素坐标系&#xff09;。世界坐…

清华智谱AI大模型ChatGLM-Pro申请开通教程

清华智谱AI大模型ChatGLM-Pro申请开通教程 ChatGLM系列模型&#xff0c;包括ChatGLM-130B和ChatGLM-6B模型&#xff0c;支持相对复杂的自然语言指令&#xff0c;并且能够解决困难的推理类问题。其中&#xff0c;ChatGLM-6B模型吸引了全球超过 160 万人下载安装&#xff0c;该模…

flutter 绘制原理探究

文章目录 Widget1、简介2、源码分析Element1、简介2、源码分析RenderObjectWidget 渲染过程总结思考Flutter 的核心设计思想便是“一切皆 Widget”,Widget 是 Flutter 功能的抽象描述,是视图的配置信息,同样也是数据的映射,是 Flutter 开发框架中最基本的概念。 在 Flutter…