目录
一、简介
二、代码展示
父pom文件
pom文件
配置文件
config
生产者
消费者
测试
结果
一、简介
主题交换机,这个交换机其实跟直连交换机流程差不多,但是它的特点就是在它的路由键和绑定键之间是有规则的。
简单地介绍下规则:
* (星号) 用来表示一个单词 (必须出现的),代表两点之间一个占位单词/
# (井号) 用来表示任意数量(零个或多个)单词,代表后面所有,匹配所有
举个小例子:
队列Q1 绑定键为 *.TT.*
队列Q2绑定键为 TT.#
如果一条消息携带的路由键为 A.TT.B,那么队列Q1将会收到;
如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到;
当一个队列的绑定键为 "#"(井号) 的时候,这个队列将会无视消息的路由键,接收所有的消息。
当 * (星号) 和 # (井号) 这两个特殊字符都未在绑定键中出现的时候,此时主题交换机就拥有的直连交换机的行为。
如果只有 # ,它就实现了扇形交换机的功能。
所以主题交换机也就实现了扇形交换机的功能,和直连交换机的功能
二、代码展示
父pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<!-- <version>2.2.5.RELEASE</version>-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chensir</groupId>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-rabbitmq</name>
<properties>
<java.version>8</java.version>
<hutool.version>5.8.3</hutool.version>
<lombok.version>1.18.24</lombok.version>
</properties>
<description>spring-boot-rabbitmq</description>
<packaging>pom</packaging>
<modules>
<module>direct-exchange</module>
<module>fanout-exchange</module>
<module>topic-exchange</module>
<module>game-exchange</module>
<module>dead-letter-queue</module>
<module>delay-queue</module>
<module>delay-queue2</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chensir</groupId>
<artifactId>spring-boot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml </relativePath>
</parent>
<artifactId>topic-exchange</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置文件
server.port=8083
#host
spring.rabbitmq.host=121.40.100.66
#默认5672
spring.rabbitmq.port=5672
#用户名
spring.rabbitmq.username=guest
#密码
spring.rabbitmq.password=guest
#连接到代理时用的虚拟主机
spring.rabbitmq.virtual-host=/
#每个消费者每次可最大处理的nack消息数量
spring.rabbitmq.listener.simple.prefetch=1
#表示消息确认方式,其有三种配置方式,分别是none、manual(手动)和auto(自动);默认auto
spring.rabbitmq.listener.simple.acknowledge-mode=auto
#监听重试是否可用
spring.rabbitmq.listener.simple.retry.enabled=true
#最大重试次数
#spring.rabbitmq.listener.simple.retry.max-attempts=5
#最大重试时间间隔
spring.rabbitmq.listener.simple.retry.max-interval=20000ms
#第一次和第二次尝试传递消息的时间间隔
spring.rabbitmq.listener.simple.retry.initial-interval=3000ms
#应用于上一重试间隔的乘数
spring.rabbitmq.listener.simple.retry.multiplier=2
#决定被拒绝的消息是否重新入队;默认是true(与参数acknowledge-mode有关系)
spring.rabbitmq.listener.simple.default-requeue-rejected=false
config
package com.chensir.config;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
public static final String TOPIC_DILIREBA = "TOPIC_迪丽热巴";
public static final String TOPIC_ZHANGBOZHI = "TOPIC_张柏芝";
public static final String TOPIC_LAMUYANGZHI = "TOPIC_辣目洋子";
//解决对象类型乱码
@Bean
public Jackson2JsonMessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("TopicExchange-01",true,false);
}
@Bean
public Queue topic_dilireba(){
return QueueBuilder.durable(TOPIC_DILIREBA).build();
}
@Bean
public Queue topic_zhangbozhi(){
return QueueBuilder.durable(TOPIC_ZHANGBOZHI).build();
}
@Bean
public Queue topic_lamuyangzhi(){
return QueueBuilder.durable(TOPIC_LAMUYANGZHI).build();
}
//“#” 匹配一个或多个词
//“*” 匹配一个词
//“.” 表示分隔一个词。
//“log.#” 能够匹配到 log.info.blog 和log.err
//“log.*” 能够匹配到 log.err 但是不能匹配到 log.info.blog
@Bean
public Binding binding(){
return BindingBuilder.bind(topic_dilireba()).to(topicExchange()).with("1.9.#");
}
@Bean
public Binding binding2(){
//1.7.0-1.7.9 代码这里修改匹配字符#和*后 还需要在rabbitMQ中把队列删除,队列不会自动修改
return BindingBuilder.bind(topic_zhangbozhi()).to(topicExchange()).with("1.7.#");
}
@Bean
public Binding binding3(){
//1.0-1.99
return BindingBuilder.bind(topic_lamuyangzhi()).to(topicExchange()).with("1.#");
}
}
生产者
package com.chensir.provider;
import com.chensir.model.SunnyBoy;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class TopicProvider {
@Resource
private RabbitTemplate rabbitTemplate;
public void send(){
// * 代表两点之间一个占位单词
// # 代表后面所有,匹配所有
SunnyBoy boy = new SunnyBoy();
boy.setName("陈冠希");
boy.setBrief("1.7.2.2");
rabbitTemplate.convertAndSend("TopicExchange-01",boy.getBrief(),boy);
}
}
消费者
package com.chensir.consumer;
import cn.hutool.json.JSONUtil;
import com.chensir.config.RabbitConfig;
import com.chensir.model.SunnyBoy;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class TopicConsumer {
@RabbitHandler
@RabbitListener(queues = RabbitConfig.TOPIC_DILIREBA )
public void process(SunnyBoy boy) {
System.out.println("迪丽热巴收到消息:"+ JSONUtil.toJsonStr(boy));
}
@RabbitHandler
@RabbitListener(queues = RabbitConfig.TOPIC_ZHANGBOZHI )
public void process2(SunnyBoy boy) {
System.out.println("张柏芝收到消息:"+ JSONUtil.toJsonStr(boy));
String name = boy.getName();
System.out.println(name);
System.out.println(boy);
}
@RabbitHandler
@RabbitListener(queues = RabbitConfig.TOPIC_LAMUYANGZHI )
public void process3(SunnyBoy boy) {
System.out.println("辣目洋子收到消息:"+ JSONUtil.toJsonStr(boy));
}
}
测试
结果