springboot 整合rabbitmq 如何自动创建交换机 ,代码举例
时间: 2023-10-23 20:25:07 浏览: 57
在 Spring Boot 中,我们可以使用 `RabbitAutoConfiguration` 自动配置 RabbitMQ。其中,我们可以通过在 application.properties 文件中添加如下配置来自动创建交换机:
```
spring.rabbitmq.template.exchange=myExchange
spring.rabbitmq.template.exchange-type=topic
```
其中,`spring.rabbitmq.template.exchange` 指定交换机的名称,`spring.rabbitmq.template.exchange-type` 指定交换机的类型。
代码示例:
```java
@Configuration
public class RabbitmqConfig {
@Bean
public Exchange directExchange() {
return ExchangeBuilder.directExchange("directExchange").durable(true).build();
}
@Bean
public Exchange topicExchange() {
return ExchangeBuilder.topicExchange("topicExchange").durable(true).build();
}
@Bean
public Exchange fanoutExchange() {
return ExchangeBuilder.fanoutExchange("fanoutExchange").durable(true).build();
}
}
```
在上面的示例中,我们使用 `ExchangeBuilder` 创建了三个不同类型的 Exchange,分别是 Direct、Topic 和 Fanout。通过设置 `durable` 属性为 `true`,我们可以指定这些交换机是持久化的。
这样,当我们启动应用程序时,这些交换机就会被自动创建。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)