RabbitMQ整合SpringBoot

本文详细介绍了RabbitMQ的多种消息传递模式,包括点对点、发布订阅和主题模式,并深入探讨了Direct、Topic和Fanout等交换机类型。通过具体的Java API示例,展示了如何在SpringBoot应用中整合RabbitMQ,实现消息的生产和消费。
摘要由CSDN通过智能技术生成

RabbitMQ的几种模式

1."Hello world"(点对点的模式)
2.发布订阅模式
3.主题模式(带了条件而已)

常用的几种交换机

1.Direct交换机
2.主题交换机(*   #)
3.分裂模式(跟路由键是没有关系的,只要交换机绑定的队列都是可以收到消息的)
4.Header模式

使用javaAPI操作RabbitMQ

1.先要创建连接工厂 ConnectionFactory
2.创建连接对象    Connection
3.创建信道       Channel
3.信道消费或者发送消息

使用SpringBoot整合RabbitMQ

1.先启动RabbitMQ服务

systemctl restart rabbitmq-server.service

2.创建消息的消费者

  • application.yml

    spring:
      rabbitmq:
        host: 192.168.126.144
    
  • pom.xml

    <dependencies>
        <!--springboot整合rmq的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
  • RabbitConfig.java

    package com.ddmzx.springboot_rabbitvip_consumer.config;
    
    import org.springframework.amqp.core.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.Resource;
    
    @Configuration
    public class RabbitConfig {
        //定义三种类型的交换机
        //直连交换机
        @Bean
        public DirectExchange directExchange(){
            return new DirectExchange("DIRECT_EXCHANGE");
        }
    
        //主题交换机
        @Bean
        public TopicExchange topicExchange(){
            return new TopicExchange("TOPIC_EXCHANGE");
        }
    
        //广播交换机
        @Bean
        public FanoutExchange fanoutExchange(){
            return new FanoutExchange("FANOUT_EXCHANGE");
        }
    
        //还需要创建队列(创建四种类型的队列)
        @Bean
        public Queue firstQueue(){
            return new Queue("FIRST_QUEUE");
        }
    
        @Bean
        public Queue secondQueue(){
            return new Queue("SECOND_QUEUE");
        }
    
        @Bean
        public Queue thirdQueue(){
            return new Queue("THIRD_QUEUE");
        }
    
        @Bean
        public Queue fourthQueue(){
            return new Queue("FOURTH_QUEUE");
        }
    
        //Autowired是根据类型匹配的
        /*@Autowired
        @Qualifier("studentDaoMySQLImpl")
    
        @Resource(name = "studentDaoMySQLImpl")
        private StudentDao studentDao;(StudentDaoMySQLImpl,StudentDaoOracleImpl)*/
    
        //进行交换机和队列的绑定
        @Bean
        public Binding bindFirst(@Qualifier("firstQueue") Queue queue,
                                 @Qualifier("directExchange") DirectExchange directExchange){
            return BindingBuilder.bind(queue).to(directExchange).with("ddm.ange");
        }
    
        @Bean
        public Binding bindSecond(@Qualifier("secondQueue") Queue queue,
                                 @Qualifier("topicExchange") TopicExchange topicExchange){
            return BindingBuilder.bind(queue).to(topicExchange).with("*.ddm.*");
        }
    
        @Bean
        public Binding bindThird(@Qualifier("thirdQueue") Queue queue,
                                 @Qualifier("fanoutExchange") FanoutExchange fanoutExchange){
            return BindingBuilder.bind(queue).to(fanoutExchange);
        }
    
        @Bean
        public Binding bindFourth(@Qualifier("fourthQueue") Queue queue,
                                 @Qualifier("fanoutExchange") FanoutExchange fanoutExchange){
            return BindingBuilder.bind(queue).to(fanoutExchange);
        }
    }
    
    
  • 消费者

    package com.ddmzx.springboot_rabbitvip_consumer.consumer;
    
    import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    /*指定当前的消费者监听哪个队列中的消息,一旦有消息过来就可以进行消费了*/
    /*说明一个消费者可以监听多个队列中内容*/
    @Component
    @RabbitListener(queues = {"FIRST_QUEUE"})
    public class FirstConsumer {
    
        @RabbitHandler
        public void process(String msg){
            System.out.println("first queue received msg : " + msg);
        }
    }
    

2.消息的生产者

  • application.yml

    spring:
      rabbitmq:
        host: 192.168.126.144
    
  • MyProducer.java

    package com.ddmzx.springboot_rabbitvip_producer.producer;
    
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyProducer {
    
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        public void sendMsg(){
            /*
            * 1.交换机的名称
            * 2.路由键
            * 3.具体的消息内容
            * */
            rabbitTemplate.convertAndSend("DIRECT_EXCHANGE","ddm.ange","a direct message");
    
            rabbitTemplate.convertAndSend("TOPIC_EXCHANGE","shanghai.ddm.student","a topic message shanghai");
    
            rabbitTemplate.convertAndSend("TOPIC_EXCHANGE","changsha.ddm.student","a topic message changsha");
    
            rabbitTemplate.convertAndSend("FANOUT_EXCHANGE","","a fanout message");
        }
    }
    
    
  • 测试类

    package com.ddmzx.springboot_rabbitvip_producer;
    
    import com.ddmzx.springboot_rabbitvip_producer.producer.MyProducer;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest
    class SpringbootRabbitvipProducerApplicationTests {
        @Autowired
        private MyProducer myProducer;
        
        @Test
        void contextLoads() {
            myProducer.sendMsg();
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值