消息队列-RabbitMQ学习笔记(五)

本文简单的介绍了,在SpringBoot中如何整合RabbitMQ,以及一些常用的方法代码。详细原理等具体内容请阅读👇👇👇“消息队列-RabbitMQ学习笔记(四)”

消息队列-RabbitMQ学习笔记(四)-CSDN博客

1. SpringBoot整合RabbitMQ

1.1. 引入依赖

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

1.2. 配置参数

server:
  port: 8132
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: 123456
    virtual-host: /boot

1.3. Fanout

@Configuration
public class ConsumerFanout {

    /**
     * 注册交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout_exchange", true, false);
    }

    /**
     * 注册队列
     *
     * @return Queue
     */
    @Bean
    public Queue fanoutQueue1() {
        return new Queue("fanout_queue1", true, false, false);
    }

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

    /**
     * 绑定队列到交换机
     */
    @Bean
    public Binding fanoutBind1() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBind2() {
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

    /**
     * 消费消息
     */
    @RabbitListener(queues = "fanout_queue1")
    public void process1(String msg) {
        System.out.println("fanout_queue1 接收到消息:" + msg);
    }

    @RabbitListener(queues = "fanout_queue2")
    public void process2(String msg) {
        System.out.println("fanout_queue2 接收到消息:" + msg);
    }
}
@Configuration
public class ProducerFanout {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void send() {
        String msg = "fanout message";
        rabbitTemplate.convertAndSend("fanout_exchange", "",msg);
    }
}

1.4. Direct

@Configuration
public class ConsumerDirect {
    /**
     * 注册交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange", true, false);
    }

    /**
     * 注册队列
     */
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue", true, false, false);
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding bind() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
    }

    /**
     * 消费消息
     */
    @RabbitListener(queues = "directQueue")
    public void process(String msg) {
        System.out.println("directQueue接收到消息:" + msg);
    }
}
@Configuration
public class ProducerDirect {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     */

    public void send() {
        String msg = "hello lz";
        rabbitTemplate.convertAndSend("directExchange", "direct", msg);
    }
}

1.5. Topic

@Configuration
public class ConsumerTopic {

    /**
     * 注册交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange("lz_boot_topic_exchange").build();
    }

    /**
     * 注册队列
     */
    @Bean
    public Queue topicQueue1() {
        return QueueBuilder.durable("lz_boot_topic_queue1").build();
    }

    @Bean
    public Queue topicQueue2() {
        return QueueBuilder.durable("lz_boot_topic_queue2").build();
    }

    @Bean
    public Queue topicQueue3() {
        return QueueBuilder.durable("lz_boot_topic_queue3").build();
    }

    @Bean
    public Binding topicBind1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("lz.#");
    }

    @Bean
    public Binding topicBind2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("lz.*");
    }

    @Bean
    public Binding topicBind3() {
        return BindingBuilder.bind(topicQueue3()).to(topicExchange()).with("#");
    }

    @RabbitListener(queues = "lz_boot_topic_queue1")
    public void topicProcess1(String msg) {
        System.out.println("lz_boot_topic_queue1 接收到消息:" + msg);
    }

    @RabbitListener(queues = "lz_boot_topic_queue2")
    public void topicProcess2(String msg) {
        System.out.println("lz_boot_topic_queue2 接收到消息:" + msg);
    }

    @RabbitListener(queues = "lz_boot_topic_queue3")
    public void topicProcess3(String msg) {
        System.out.println("lz_boot_topic_queue3 接收到消息:" + msg);
    }
}
@Configuration
public class ProducerTopic {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void send() {
        String msg = "topic 主题路由器";
        rabbitTemplate.convertAndSend("lz_boot_topic_exchange", "lz.lq.666", msg + "1");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值