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:

scala 复制代码
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-amqp
	implementation '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

yaml 复制代码
server:
  port: 8014

spring:
  rabbitmq:
    username: admin
    password: 123456
    dynamic: true
#    port: 5672
#    host: 192.168.49.9
    addresses: 192.168.49.10:5672,192.168.49.9:5672,192.168.49.11:5672
    publisher-confirm-type: correlated
    publisher-returns: true
  application:
    name: shushan
  datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://ip/shushan
      username: root
      password: 
      hikari:
        minimum-idle: 10
        maximum-pool-size: 20
        idle-timeout: 50000
        max-lifetime: 540000
        connection-test-query: select 1
        connection-timeout: 600000

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

项目所使用到的常量:

java 复制代码
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:

java 复制代码
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 {

    @Bean
    public Queue demoDirectQueue() {
        return new Queue(MqConstant.demoDirectQueue, true, false, false);
    }

    @Bean
    DirectExchange demoDirectExchange() {
        return new DirectExchange(MqConstant.demoDirectExchange, true, false);
    }

    @Bean
    Binding bingingDirect() {
        return BindingBuilder.bind(demoDirectQueue()).to(demoDirectExchange()).with(MqConstant.demoDirectRouting);
    }

    @Bean
    DirectExchange lonelyDirectExchange() {
        return new DirectExchange(MqConstant.lonelyDirectExchange);
    }


}

DirectReceiver 消费者:

java 复制代码
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 {

    @RabbitHandler
    public void process(Map msg){
        log.info("1---receiver msg:"+msg.toString());
    }
}

DirectReceiverV2 消费者:

java 复制代码
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 {

    @RabbitHandler
    public void process(Map msg){
        log.info("2---receiver msg:"+msg.toString());
    }
}

MqController 生产者:

java 复制代码
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 {

    @Autowired
    RabbitTemplate 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";
    }

测试:

结果:

java 复制代码
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 :

java 复制代码
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 {

    @Bean
    public Queue bigCarQueue(){
        return new Queue(MqConstant.BIG_CAR_TOPIC);
    }

    @Bean
    public Queue smallCarQueue(){
        return new Queue(MqConstant.SMALL_CAR_TOPIC);
    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(MqConstant.topicExchange);
    }

    @Bean
    Binding bindingExchangeMessage(){
        return BindingBuilder.bind(bigCarQueue()).to(exchange()).with(MqConstant.BIG_CAR_TOPIC);
    }

    @Bean
    public Binding bindingExchangeMessageSmall(){
        return BindingBuilder.bind(smallCarQueue()).to(exchange()).with(MqConstant.TOPIC_ALL);
    }


}

TopicBigCarReceiver 消费者:

java 复制代码
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 {

    @RabbitHandler
    public void process(Map msg){
        log.info("topicBigCarReceiver msg :"+msg);
    }
}

TopicSmallCarReceiver 消费者:

java 复制代码
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 {


    @RabbitHandler
    public void process(Map msg){
        log.info("TopicSmallCarReceiver msg :"+msg);
    }
}
java 复制代码
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 {

    @Autowired
    RabbitTemplate 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";
    }

}
java 复制代码
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队列中。

java 复制代码
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 :

java 复制代码
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 {


    @Bean
    public Queue queueA(){
        return new Queue(MqConstant.FANOUT_A);
    }

    @Bean
    public Queue queueB(){
        return new Queue(MqConstant.FANOUT_B);
    }

    @Bean
    public Queue queueC(){
        return new Queue(MqConstant.FANOUT_C);
    }

    @Bean
    FanoutExchange fanoutExchange(){
        return new FanoutExchange(MqConstant.FANOUT_EXCHANGE);
    }

    @Bean
    public Binding bindingExchangeA(){
        return BindingBuilder.bind(queueA()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingExchangeB(){
        return BindingBuilder.bind(queueB()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingExchangeC(){
        return BindingBuilder.bind(queueC()).to(fanoutExchange());
    }


}

FanoutAReceiver 消费者:

java 复制代码
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 {


    @RabbitHandler
    public void process(Map msg){
        log.info("FanoutAReceiver msg :"+msg);
    }
}

FanoutBReceiver 消费者:

java 复制代码
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 {


    @RabbitHandler
    public void process(Map msg){
        log.info("FanoutBReceiver msg :"+msg);
    }
}

FanoutCReceiver 消费者:

java 复制代码
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 {


    @RabbitHandler
    public void process(Map msg){
        log.info("FanoutCReceiver msg :"+msg);
    }
}

MqController 生产者:

java 复制代码
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 {

    @Autowired
    RabbitTemplate 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";
    }
}

测试:

java 复制代码
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 都收到了消息,相当于广播。

相关推荐
苹果醋31 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行1 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园1 小时前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
wm10432 小时前
java web springboot
java·spring boot·后端
路在脚下@9 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
啦啦右一9 小时前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien9 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋310 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
荆州克莱12 小时前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
武昌库里写JAVA13 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计