RabbitMQ整合SpringBoot

RabbitMQ整合SpringBoot

pom

<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>
</dependencies>

yml

spring:
  rabbitmq:
    username: username
    password: password
    host: 127.0.0.1
    port: 5672
    virtual-host: /

Code

Configuration

public class RabbitMQConstants {
	// 队列
    public static final String QUEUE = "queue";
    // 路由模式
    public static final String ROUTE = "route";
    // 发布订阅
    public static final String PUBSUB = "pubsub1";
    // topic模式
    public static final String TOPIC = "topic";
    // topic模式
    public static final String TOPIC1 = "topic1";
	// 队列时绑定的交换机
    public static final String DIRECT_EXCHANGE = "direct_exchange";
    // 路由模式时绑定的交换机
    public static final String ROUTE_EXCHANGE = "route_exchange";
    // 发布订阅模式时绑定的交换机
    public static final String PUBSUB_EXCHANGE = "pubsub_exchange";
    // 主题模式时绑定的交换机
    public static final String TOPIC_EXCHANGE = "topic_exchange";
	// 队列时路由键
    public static final String QUEUE_ROUTING_KEY = "queue_routeing_key";
    // 路由模式时路由键
    public static final String ROUTE_ROUTING_KEY = "route_routeing_key";
    // 主题模式时路由键1
    public static final String TOPIC_ROUTING_KEY = "topic_routeing_key.*";
    // 主题模式时路由键2
    public static final String TOPIC_ROUTING_KEY1 = "topic_routeing_key.A.*";
    // 主题模式发布时路由键
    public static final String TOPIC_ROUTING_KEY_TEST = "topic_routeing_key.A.B";
}
@Configuration
public class RabbitMQConfig {
	// 队列模式
    @Bean
    public Queue queue(){
        return new Queue(RabbitMQConstants.QUEUE);
    }
    // 发布订阅模式
    @Bean
    public Queue pubsub(){
        return new Queue(RabbitMQConstants.PUBSUB);
    }
	// 路由模式
    @Bean
    public Queue route(){
        return new Queue(RabbitMQConstants.ROUTE);
    }
	// 主题模式
    @Bean
    public Queue topic(){
        return new Queue(RabbitMQConstants.TOPIC);
    }
	// 主题模式
    @Bean
    public Queue topic1(){
        return new Queue(RabbitMQConstants.TOPIC1);
    }
	// 队列模式交换机
    @Bean
    public Exchange directExchange(){
        return new DirectExchange(RabbitMQConstants.DIRECT_EXCHANGE);
    }
	// 发布订阅模式交换机
    @Bean
    public Exchange pubsubExchange(){
        return new FanoutExchange(RabbitMQConstants.PUBSUB_EXCHANGE);
    }
	// 路由模式交换机
    @Bean
    public Exchange routeExchange(){
        return new DirectExchange(RabbitMQConstants.ROUTE_EXCHANGE);
    }
	// 主题模式交换机
    @Bean
    public Exchange topicExchange(){
        return new TopicExchange(RabbitMQConstants.TOPIC_EXCHANGE);
    }
	// 队列模式队列和交换机绑定
    @Bean
    public Binding queueBinding(){
        return new Binding(RabbitMQConstants.QUEUE,
                Binding.DestinationType.QUEUE,
                RabbitMQConstants.DIRECT_EXCHANGE,
                RabbitMQConstants.QUEUE_ROUTING_KEY,
                null);
    }
	// 发布订阅模式队列和交换机绑定
    @Bean
    public Binding pubsubBinding(){
        return new Binding(RabbitMQConstants.PUBSUB,
                Binding.DestinationType.QUEUE,
                RabbitMQConstants.PUBSUB_EXCHANGE,
                "",
                null);
    }
	// 路由模式队列和交换机绑定
    @Bean
    public Binding routeBinding(){
        return new Binding(RabbitMQConstants.ROUTE,
                Binding.DestinationType.QUEUE,
                RabbitMQConstants.ROUTE_EXCHANGE,
                RabbitMQConstants.ROUTE_ROUTING_KEY,
                null);
    }
	// 主题模式队列和交换机绑定
    @Bean
    public Binding topic1Binding(){
        return new Binding(RabbitMQConstants.TOPIC,
                Binding.DestinationType.QUEUE,
                RabbitMQConstants.TOPIC_EXCHANGE,
                RabbitMQConstants.TOPIC_ROUTING_KEY,
                null);
    }
	// 主题模式队列和交换机绑定
    @Bean
    public Binding topic2Binding(){
        return new Binding(RabbitMQConstants.TOPIC1,
                Binding.DestinationType.QUEUE,
                RabbitMQConstants.TOPIC_EXCHANGE,
                RabbitMQConstants.TOPIC_ROUTING_KEY1,
                null);
    }
}

listener

@Component
public class RabbitMQListener {

    @RabbitListener(queues = RabbitMQConstants.QUEUE)
    public void queue1(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.queue1 -> " + message);
    }

    @RabbitListener(queues = RabbitMQConstants.QUEUE)
    public void queue2(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.queue2 -> " + message);
    }

    @RabbitListener(queues = RabbitMQConstants.PUBSUB)
    public void pubsub1(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.pubsub1 -> " + message);
    }


    @RabbitListener(queues = RabbitMQConstants.ROUTE)
    public void route1(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.route1 -> " + message);
    }

    @RabbitListener(queues = RabbitMQConstants.ROUTE)
    public void route2(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.route2 -> " + message);
    }

    @RabbitListener(queues = RabbitMQConstants.TOPIC)
    public void topic1(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.topic1 -> " + message);
    }

    @RabbitListener(queues = RabbitMQConstants.TOPIC1)
    public void topic2(String message){
        System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.topic2 -> " + message);
    }
}

controller

@RestController
@RequestMapping("rabbit")
public class RabbitMQController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @GetMapping("queue")
    public void queue(String message){
        amqpTemplate.convertAndSend(RabbitMQConstants.DIRECT_EXCHANGE,RabbitMQConstants.QUEUE_ROUTING_KEY,message);
    }

    @GetMapping("pubsub")
    public void pubsub(String message){
        amqpTemplate.convertAndSend(RabbitMQConstants.PUBSUB_EXCHANGE,null, message);
    }

    @GetMapping("route")
    public void route(String message){
        amqpTemplate.convertAndSend(RabbitMQConstants.ROUTE_EXCHANGE, RabbitMQConstants.ROUTE_ROUTING_KEY,message);
    }

    @GetMapping("topic")
    public void topic(String message){
        amqpTemplate.convertAndSend(RabbitMQConstants.TOPIC_EXCHANGE, RabbitMQConstants.TOPIC_ROUTING_KEY_TEST,message);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值