springboot3 整合rabbitmq 配置topic
时间: 2023-11-04 14:04:53 浏览: 212
首先,需要在pom.xml中添加RabbitMQ的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
```
然后在application.properties中配置RabbitMQ连接信息:
```properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
```
接着,创建一个TopicExchange的Bean:
```java
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topic.exchange");
}
```
然后创建两个Queue:
```java
@Bean
public Queue queue1() {
return new Queue("queue1");
}
@Bean
public Queue queue2() {
return new Queue("queue2");
}
```
最后,将Queue和Exchange通过Binding绑定:
```java
@Bean
public Binding binding1() {
return BindingBuilder.bind(queue1()).to(topicExchange()).with("*.topic.*");
}
@Bean
public Binding binding2() {
return BindingBuilder.bind(queue2()).to(topicExchange()).with("#.topic");
}
```
其中,binding1表示将queue1绑定到topic.exchange上,routing key为*.topic.*,即匹配所有三段式的topic;binding2表示将queue2绑定到topic.exchange上,routing key为#.topic,即匹配所有二段式以及更长的topic。
以上就是整合RabbitMQ的Topic模式的配置过程。
阅读全文
相关推荐


















