SpringBoot 整合 RabbitMQ 使用topic交换机

9 篇文章 0 订阅
7 篇文章 0 订阅

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置文件

# 连接地址
spring.rabbitmq.host=127.0.0.1
# 端口号
spring.rabbitmq.port=5672
# 账号
spring.rabbitmq.username=guest
# 密码
spring.rabbitmq.password=guest
# 地址
spring.rabbitmq.virtual-host=myvh

# 消息开启手动确认
#spring.rabbitmq.listener.direct.acknowledge-mode=manual
#采取手动应答
#spring.rabbitmq.listener.simple.acknowledge-mode=manual
#支持重试
#spring.rabbitmq.listener.simple.retry.enabled=true
#最大重试次数
#spring.rabbitmq.listener.simple.retry.max-attempts: 5
#重试间隔次数
#spring.rabbitmq.listener.simple.retry.initial-interval: 3000

配置类

@Configuration
public class RabbitmqConfig {

    //配置Exchange
    @Bean
    public TopicExchange directExchange() {
        return new TopicExchange("mytopic");
    }

    // 绑定交换机与队列
    @Bean
    public Binding getMyqueue(TopicExchange topicExchange) {
        Queue queue = new Queue("topicqueue");
        //key -- 消费用的key
        return BindingBuilder.bind(queue).to(topicExchange).with("key.*");
    }

}

发送消息

@Service
public class RabbitmqProducerService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void amqpTemplateTest(){
        String message = UUID.randomUUID().toString().substring(0, 6);
        //mytopic --  交换机Name;key -- 发送的key
        amqpTemplate.convertAndSend("mytopic","key.11",message);
        System.out.println("发送完成");
    }
}

接收消息

@Component
public class RabbitmqListener {

    @RabbitListeners({@RabbitListener(queues = "topicqueue")})
    public void reveiceMessage(String msg, Channel channel, Message message) {
        System.out.println(msg);
        System.out.println(channel);
        System.out.println(message);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用RabbitMQ绑定交换机的类型可以通过以下步骤进行: 1. 创建一个Exchange声明类,通过注解@Exchange声明交换机,指定交换机名称、类型、是否持久化等属性。 ```java @Configuration public class ExchangeConfig { @Bean public Exchange directExchange() { return ExchangeBuilder.directExchange("direct.exchange") .durable(true) .build(); } @Bean public Exchange fanoutExchange() { return ExchangeBuilder.fanoutExchange("fanout.exchange") .durable(true) .build(); } @Bean public Exchange topicExchange() { return ExchangeBuilder.topicExchange("topic.exchange") .durable(true) .build(); } @Bean public Exchange headersExchange() { return ExchangeBuilder.headersExchange("header.exchange") .durable(true) .build(); } } ``` 2. 在RabbitMQ配置文件中配置交换机绑定关系,将Exchange声明类中定义的交换机与队列进行绑定。 ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / listener: simple: acknowledge-mode: manual template: mandatory: true publisher-returns: true publisher-confirm-type: correlated listener: simple: default-requeue-rejected: false bindings: direct-binding: destination: direct.queue exchange: direct.exchange routing-key: direct fanout-binding: destination: fanout.queue exchange: fanout.exchange topic-binding: destination: topic.queue exchange: topic.exchange routing-key: topic.* headers-binding: destination: headers.queue exchange: header.exchange headers: key: value ``` 在以上配置中,我们定义了四个交换机类型并分别绑定了队列,其中direct-binding使用了routing-key,topic-binding使用了通配符*,headers-binding使用了headers参数。 这样,在Spring Boot应用中就可以使用RabbitMQ进行消息的发送和接收了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值