springboot整合rabbitmq之主题topic模式

配置队列,路由器,并绑定

package com.gkl1120.topicmq.conf;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ctx_gao kailong
 * @date 2020/5/22 16:10
 */
@Configuration
public class TopicMqConfig {
    @Bean
    public TopicExchange myTopicExchange() {
        return new TopicExchange("gkl_topic_exchange",true,false);
    }

    @Bean
    public Queue queueTopic1() {
        return new Queue("queueTopic1" , true ,false ,false);
    }

    @Bean
    public Queue queueTopic2() {
        return new Queue("queueTopic2" , true ,false ,false);
    }

    @Bean
    public Queue queueTopic3() {
        return new Queue("queueTopic3" , true ,false ,false);
    }

    @Bean
    public Binding topicBinding1() {
        return BindingBuilder.bind(queueTopic1()).to(myTopicExchange()).with("test.gkl.hello");
    }

    @Bean
    public Binding topicBinding2() {
        return BindingBuilder.bind(queueTopic2()).to(myTopicExchange()).with("test.#");
    }

    @Bean
    public Binding topicBinding3() {
        return BindingBuilder.bind(queueTopic3()).to(myTopicExchange()).with("test.*");
    }

}

消费者

package com.gkl1120.topicmq.service;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @author ctx_gao kailong
 * @date 2020/5/22 16:21
 */
@Service
public class Listener {

    @RabbitListener(queues = "queueTopic1")
    @RabbitHandler
    public void topic1(String msg) {
        System.out.println("topic1:"+msg);
    }

    @RabbitListener(queues = "queueTopic2")
    @RabbitHandler
    public void topic2(String msg) {
        System.out.println("topic2:"+msg);
    }

    @RabbitListener(queues = "queueTopic3")
    @RabbitHandler
    public void topic3(String msg) {
        System.out.println("topic3:"+msg);
    }
}

生产者:

package com.gkl1120.topicmq.service;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ctx_gao kailong
 * @date 2020/5/22 16:24
 */
@Component
public class Productr {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String msg) {
        rabbitTemplate.convertAndSend("gkl_topic_exchange","test.gkl.hello",msg);
    }
}

controller

package com.gkl1120.topicmq.controller;

import com.gkl1120.topicmq.service.Productr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ctx_gao kailong
 * @date 2020/5/22 16:26
 */
@RestController
@RequestMapping("/mq")
public class MqController {
    @Autowired
    private Productr productr;

    @RequestMapping("/send")
    public String send() {
        productr.send("好不好玩啊!");
        return "ok";
    }
}

结果:
topic1:好不好玩啊!
topic2:好不好玩啊!
总结:通配符是以.隔开通配,*表示匹配一个词,#可以匹配多个词

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值