RabbitMQ与springboot整合

news2024/11/15 11:41:04
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-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

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。

项目所使用到的常量:

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 {

    @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 消费者:

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 消费者:

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 生产者:

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";
    }

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

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 {

    @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 消费者:

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 消费者:

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);
    }
}

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";
    }

}

在这里插入图片描述

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 {


    @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 消费者:

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 消费者:

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 消费者:

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 生产者:

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";
    }
}

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

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.coloradmin.cn/o/1078932.html

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

相关文章

Python 图形化界面基础篇:发布应用程序到不同平台

Python 图形化界面基础篇&#xff1a;发布应用程序到不同平台 引言步骤1&#xff1a;准备应用程序1.1 处理依赖关系1.2 创建用户文档1.3 处理平台差异 步骤2&#xff1a;创建安装程序2.1 使用cx_Freeze创建独立包2.2 使用 PyInstaller 创建可执行文件2.3 使用其他工具 步骤3&am…

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

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

CSS图文悬停翻转效果完整源码附注释

实现效果截图 HTML页面源码 <!DOCTYPE html> <html><head><meta http-equiv="content-type

正点原子嵌入式linux驱动开发——Linux内核移植

之前的两篇笔记&#xff0c;简单了解了一下Linux内核顶层 Makefile和Linux内核的启动流程&#xff0c;本篇内容来学习一下如何将ST官方提供的Linux内核移植到正点原子的STM32MP157开发板上。通过本章的学习&#xff0c;将掌握如何将半导体厂商提供的Linux BSP包移植到自己的平台…

【分享】小红书数据采集入excel表格

思路&#xff1a; 1. 打开小红书关键词页面 2. 循环指定次数&#xff0c;并鼠标往下滚 3. 获取元素列表&#xff0c;循环元素列表 4. 判断标题是否在list中&#xff0c;如果在就跳过&#xff0c;如果不在将标题存入list中 5. 点击元素&#xff0c;读取标题和内容&#xff…

LeetCode 1503. 所有蚂蚁掉下来前的最后一刻【脑筋急转弯】1618

本文属于「征服LeetCode」系列文章之一&#xff0c;这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁&#xff0c;本系列将至少持续到刷完所有无锁题之日为止&#xff1b;由于LeetCode还在不断地创建新题&#xff0c;本系列的终止日期可能是永远。在这一系列刷题文章…

2018架构真题案例(四十九)

某文件采用多级索引结构&#xff0c;磁盘大小4K字节&#xff0c;每个块号4字节&#xff0c;那么二级索引结果时&#xff0c;文件最大。 A、1024 B、1024*1024 C、2048*2048 D、4096*4096 答案&#xff1a;B 霍尔三维结构以时间堆、&#xff08;&#xff09;堆、知识堆组成…

vue踩坑

文章目录 1.error1 1.error1 在项目里面前端报这个错&#xff0c;有点蒙 确定了错误是在遍历数组中的图片部分 猜测可能是一开始的时候没有把photoList在form中写出来&#xff0c;form里面啥没有&#xff0c;导致渲染的时候有问题 所以以后在页面上渲染数据的都在data里…

网络-WebSocket

文章目录 前言一、WebSocket简介应用场景原理 二、使用心跳监测广播消息 三、群聊demo总结 前言 本文主要记录WebSocket的简单介绍和使用&#xff0c;完成群聊的demo 一、WebSocket简介 WebSocket是一种通信协议&#xff0c;它通过单个TCP连接提供全双工的通信通道。 它允许客…

【C#】标准WebService Soap1.1 兼容 ContentType: application/xml

一、问题描述 1.1 ESB平台要求 ContentType&#xff1a;application/xml Soap协议版本&#xff1a;1.1 1.2 提供的 WebService 接口 语言&#xff1a;C# 目标框架&#xff1a;.NetFramework 4.6.1 1.3 Postman 测试结果 HTTP Error 415.0 - Unsupported Media Type 服务器…

计算机竞赛YOLOv7 目标检测网络解读

文章目录 0 前言1 yolov7的整体结构2 关键点 - backbone关键点 - head3 训练4 使用效果5 最后 0 前言 世界变化太快&#xff0c;YOLOv6还没用熟YOLOv7就来了&#xff0c;如果有同学的毕设项目想用上最新的技术&#xff0c;不妨看看学长的这篇文章&#xff0c;学长带大家简单的…

如何为您的 Linux 服务器设置简单的 Grafana 云监控仪表板

Grafana 是一个开源可观察性平台,用于创建可视化数据集的仪表板。您可以使用它方便地监控服务器统计信息,例如 CPU 消耗、网络吞吐量和正常运行时间。 Grafana可以自托管,也可以通过官方Grafana Cloud实例作为 SaaS 解决方案进行访问。在本文中,您将了解如何设置 Grafana …

修炼k8s+flink+hdfs+dlink(四:k8s(一)概念)

一&#xff1a;概念 1. 概述 1.1 kubernetes对象. k8s对象包含俩个嵌套对象字段。 spec&#xff08;规约&#xff09;&#xff1a;期望状态 status&#xff08;状态&#xff09;&#xff1a;当前状态 当创建对象的时候&#xff0c;会按照spec的状态进行创建&#xff0c;如果…

scratch芝麻开门 2023年9月中国电子学会图形化编程 少儿编程 scratch编程等级考试一级真题和答案解析

目录 scratch芝麻开门 一、题目要求 1、准备工作 2、功能实现 二、案例分析

操作系统学习笔记3-同步互斥问题

文章目录 1 同步与互斥逻辑图2、并发性异步性独立性3、临界资源临界区4、同步与互斥 1 同步与互斥逻辑图 2、并发性异步性独立性 3、临界资源临界区 4、同步与互斥

汽车一键启动点火开关按键一键启动按钮型号规格

汽车点火开关/移动管家一键启动按键/汽车改装引擎启动按钮型号&#xff1a;YD828溥款开关 一键启动按钮&#xff08;适用于配套启动主机使用或原车一键启动开关更换&#xff09; 1.适合配套专用板板安装 2.开孔器开孔安装 3.原车钥匙位安装 外观&#xff1a;黑色 按钮上有3种不…

【融合ChatGPT等AI模型】Python-GEE遥感云大数据分析、管理与可视化及多领域案例实践应用

目录 第一章 理论基础 第二章 开发环境搭建 第三章 遥感大数据处理基础与ChatGPT等AI模型交互 第四章 典型案例操作实践 第五章 输入输出及数据资产高效管理 第六章 云端数据论文出版级可视化 更多应用 随着航空、航天、近地空间等多个遥感平台的不断发展&#xff0c;近…

免费的ChatGPT与StableDiffusion AI绘画 二合一 附在线地址

ChatGPT与StableDiffusion 在线地址在文末 介绍 嘿&#xff0c;大家好&#xff01;今天我要给大家介绍一个非常酷炫的技术结合——ChatGPT与StableDiffusion的合作。听起来是不是很有趣&#xff1f;那么&#xff0c;让我们一起来看看这个组合到底能带给我们什么样的奇妙体验…

Go图片文件按照时间戳如何排序

涉及点包括 文件创建;时间控制器;自建封装包以及方法; 模板渲染;路由配置;不同的数据类型之间的转换拼接; 对于之前进行的文件上传操作,囊括单文件以及同名多文件和非同名多文件的编程方法,在生产中会遇到一个问题,如果上传的图片是同名的,那么在单文件上传的时候会将…

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

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