SpringBoot整合RabbitMQ详解

SpringBoot整合RabbitMQ

producer
  • 导入依赖

     <!-- 1. 父工程依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    
    <dependencies>
        <!--2. rabbitmq-->
        <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>
        </dependency>
    </dependencies>
    
  • 配置文件application.yml

    # 配置RabbitMQ的基本信息  ip 端口 username  password..
    spring:
      rabbitmq:
        host: 192.168.33.129 # ip
        port: 5672
        username: gmx
        password: gmx
        virtual-host: /it
    
  • JavaConfig方式声明队列、交换信息

    @Configuration
    public class RabbitMQConfig {
        public static final String EXCHANGE_NAME = "boot_topic_exchange";
        public static final String QUEUE_NAME = "boot_queue";
        //1.声明交换机构建器
        @Bean("bootExchange")
        public Exchange bootExchange(){
            return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
        }
    
        //2.声明queue构建器
        @Bean("bootQueue")
        public Queue bootQueue(){
            return QueueBuilder.durable(QUEUE_NAME).build();
        }
    
        //3. 声明绑定关系构建器 队列和交互机绑定关系 Binding
        /*
            1. 知道哪个队列
            2. 知道哪个交换机
            3. routing key
            with指定routing key
         */
        @Bean
        public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){
            return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
        }
    }
    
  • 创建启动类

    @SpringBootApplication
    public class ProducerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProducerApplication.class);
        }
    }
    
  • 消息发送测试

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class ProducerTest {
    
        //1.注入RabbitTemplate完成消息发送
        @Autowired
        private RabbitTemplate rabbitTemplate;
    
        @Test
        public void testSend(){
            rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.haha","boot mq hello~~~");
        }
    }
    
consumer
  • 导入依赖

    <dependencies>
        <!--RabbitMQ 启动依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
    </dependencies>
    
  • 配置文件application.yml

    spring:
      rabbitmq:
        host: 192.168.33.129 #主机ip
        port: 5672 #端口
        username: gmx
        password: gmx
        virtual-host: /it
        publisher-confirms: true
        publisher-returns: true
    
  • 接受消息

    @Component
    public class RabbimtMQListener {
        @RabbitListener(queues = "boot_queue")
        public void ListenerQueue(Message message, Channel channel){
            //System.out.println(message);
            System.out.println(new String(message.getBody()));
        }
    }
    
  • 创建启动类

    @SpringBootApplication
    public class ConsumerSpringbootApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerSpringbootApplication.class, args);
        }
    
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值