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.mzph.cn/news/103348.shtml

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

相关文章

读书笔记-《ON JAVA 中文版》-摘要26[第二十三章 注解]

文章目录 第二十三章 注解1. 基本语法1.1 基本语法1.2 定义注解1.3 元注解 2. 编写注解处理器2.1 编写注解处理器2.2 注解元素2.3 默认值限制 3. 使用javac处理注解4. 基于注解的单元测试5. 本章小结 第二十三章 注解 注解&#xff08;也被称为元数据&#xff09;为我们在代码…

【Overload游戏引擎细节分析】从视图投影矩阵提取视锥体及overload对视锥体的封装

overoad代码中包含一段有意思的代码&#xff0c;可以从视图投影矩阵逆推出摄像机的视锥体&#xff0c;本文来分析一下原理 一、平面的方程 视锥体是用平面来表示的&#xff0c;所以先看看平面的数学表达。 平面方程可以由其法线N&#xff08;A, B, C&#xff09;和一个点Q(x0,…

让 Visual Studio 用上 ChatGPT

一、简介 Visual chatGPT Studio 是 Visual Studio 的一个免费扩展&#xff0c;它直接在 IDE 中添加了 chatGPT 功能。它允许用户以可以根据菜单功能的方式使用 chatGPT。 二、功能介绍 该扩展提供了一组使用 ChatGPT 命令&#xff0c;可以在编辑器中选择你需要处理的代码或…

k8s修改集群IP--不重置集群

正常在用集群想要更换ip master 节点ip192.168.10.138 改为192.168.10.148 node1节点ip192.168.10.139 改为192.168.10.149 node2节点ip192.168.10.140 改为192.168.10.150 master 节点 1)执行脚本1233.sh 1233.sh 内容如下&#xff1a; # master 节点 export oldip1192.168.…

可完全替代FTP的文件传输工具大集合

在当今的信息时代&#xff0c;文件传输是我们日常工作和生活中不可或缺的一项功能。无论是企业内部还是与外部合作伙伴之间&#xff0c;我们都需要频繁地进行各种类型和大小的文件的交换和共享。然而&#xff0c;传统的文件传输方式&#xff0c;如FTP、HTTP、CIFS等&#xff0c…

一键部署开源AI(人工智能对话模型)(支持显卡或CPU加内存运行)--ChatGLM-6B

一、基本介绍&#xff1a; 基于ChatGLM-6B 的快速安装服务&#xff0c;支持一键安装&#xff0c;无需任何服务配置和代码编写即可完成。 ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型&#xff0c;基于 General Language Model (GLM) 架构&#xff0c;具有 62 亿参数…

多尺度retinex图像去雾算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 2.算法运行软件版本 matlab2022a 3.部分核心程序 clc; clear; close all; warning off; addpath(genpath(pwd)); rng(default)img_in im2doub…

微软 AR 眼镜新专利:包含热拔插电池

近日&#xff0c;微软在增强现实&#xff08;AR&#xff09;领域进行深入的研究&#xff0c;并申请了一项有关于“热插拔电池”的专利。该专利于2023年10月5日发布&#xff0c;描述了一款采用模块化设计的AR眼镜&#xff0c;其热插拔电池放置在镜腿部分&#xff0c;可以直接替代…

SpringBoot 如何使用 Sleuth 进行分布式跟踪

使用Spring Boot Sleuth进行分布式跟踪 在现代分布式应用程序中&#xff0c;跟踪请求和了解应用程序的性能是至关重要的。Spring Boot Sleuth是一个分布式跟踪解决方案&#xff0c;它可以帮助您在分布式系统中跟踪请求并分析性能问题。本文将介绍如何在Spring Boot应用程序中使…

Zabbix第二部分:基于Proxy分布式部署实现Web监控和Zabbix HA集群的搭建

代理和高可用 一、基于zabbix-proxy的分布式监控1.1 分布式监控的作用1.2 数据流向1.3 构成组件 二、部署zabbix代理服务器Step1 前置准备Step2 设置 zabbix 的下载源&#xff0c;安装 zabbix-proxyStep3 部署数据库并将zabbix相关文件导入Step4 修改zabbix-proxy的配置文件&am…

简述WPF中MVVM的设计思想

近年来&#xff0c;随着WPF在生产、制造、工控等领域应用越来越广泛&#xff0c;对WPF的开发需求也在逐渐增多&#xff0c;有很多人不断的从Web、WinForm开发转向了WPF开发。 WPF开发有很多新的概念及设计思想&#xff0c;如数据驱动、数据绑定、依赖属性、命令、控件模板、数…

智慧工地:助力数字建造、智慧建造、安全建造、绿色建造

智慧工地管理系统融合计算机技术、物联网、视频处理、大数据、云计算等&#xff0c;为工程项目管理提供先进的技术手段&#xff0c;构建施工现场智能监控系统&#xff0c;有效弥补传统监理中的缺陷&#xff0c;对人、机、料、法、环境的管理由原来的被动监督变成全方位的主动管…

AlphaPose Pytorch 代码详解(一):predict

前言 代码地址&#xff1a;AlphaPose-Pytorch版 本文以图像 1.jpg&#xff08;854x480&#xff09;为例对整个预测过程的各个细节进行解读并记录 python demo.py --indir examples/demo --outdir examples/res --save_img1. YOLO 1.1 图像预处理 cv2读取BGR图像 img [480,…

SyntaxError: invalid character ‘:‘ (U+FF1A)问题解决

问题&#xff1a; SyntaxError: invalid character &#xff1a; (UFF1A) 原因及解决方法&#xff1a; 冒号输入的格式不对&#xff0c;冒号的输入为中文&#xff0c;改成英文即可。

Design patterns--策略模式

设计模式之策略模式 笔者经常使用Modbus TCP和Modbus RTU通信协议&#xff0c;而两种的请求数据的格式不一样&#xff0c;故而采用策略模式来健壮整个工程项目。 代码示例 #ifndef MODBUS_H #define MODBUS_H #include <string>std::string convertToHex(unsigned char…

[GAMES101]透视投影变换矩阵中为什么需要改变z值

一、问题提出 在GAMES101-Lecture4 Transformation Matrices 一节中&#xff0c;闫老师介绍了正交投影和透视投影。 在讲透视投影变换矩阵 M p e r s p → o r t h o M_{persp→ortho} Mpersp→ortho​时&#xff0c;同学们对矩阵中的z分量是变化的还是不变的有很多争论。即下…

数据一致性分发

为什么要数据分发 微服务中&#xff0c;每个服务都有独立的数据源&#xff0c;这使得数据同步成为难题。 拉模式or推模式&#xff1f; 拉模式存在的问题 由于网络延迟&#xff0c;拉取的数据不一定是最新的 如果频繁向另一服务拉取数据&#xff0c;会给服务造成压力&#xf…

保护敏感数据的艺术:数据安全指南

多年来&#xff0c;工程和技术迅速转型&#xff0c;生成和处理了大量需要保护的数据&#xff0c;因为网络攻击和违规的风险很高。为了保护企业数据&#xff0c;组织必须采取主动的数据安全方法&#xff0c;了解保护数据的最佳实践&#xff0c;并使用必要的工具和平台来实现数据…

大模型时代的开发者:从飞桨PPDE到文心布道师

飞桨开发者技术专家&#xff08;PPDE&#xff09;谢杰航研究方向为AI城市规划、景观设计、生态环境及农业等领域的应用落地。他在此前Wave Summit 2023深度学习开发者大会上为大家带来了主题为《大模型时代的开发者&#xff1a;从飞桨PPDE到文心布道师》的演讲。本次演讲共分为…

解决MySQL错误-this is incompatible with sql_mode=only_full_group_by

报错 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘数据库名.表名.字段名’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_modeonly_full_group_by 原因 MySQL错误-t…