RabbitMQ整合SpringBoot项目用法

一、Docker安装RabbitMQ

下载镜像

docker pull rabbitmq:management

创建容器

docker run -di --name=rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 rabbitmq:management

测试访问
http://ip地址:15672/ 用户名密码 guest guest

在这里插入图片描述

二、环境准备

pom依赖

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

yml配置文件

server:
  port: 8080
spring:
  rabbitmq:
    host: 192.168.103.10

三、RabbitMQ配置类

RabbitMQ有三种模式:

  1. 直接模式
  2. 分裂模式
  3. 主题模式

流程就是:

  1. 创建队列
  2. 创建交换机
  3. 将队列绑定到交换机
package com.lsh.config;

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 :LiuShihao
 * @date :Created in 2020/8/7 9:10 上午
 * @desc :rabbitMQ配置类
 *
 * 1. 创建Queue队列                  ---->new Queue("myQueue1")
 * 2. 创建交换机FanoutExchange       ---->new FanoutExchange("myFanoutExchange")
 * 3. 将队列绑定到交换机上             --->BindingBuilder.bind(q3).to(myFanoutExchange)
 */
@Configuration
public class RabbitMQConfig {
    @Bean
    @Qualifier("myQueue1")
    public Queue getQueue1(){
        return new Queue("myQueue1");//对列名:first
    }

    @Bean
    @Qualifier("myQueue2") // 都是Queue类型的,需要指定名字加以区分,不然无法注入
    public Queue getQueue2() {
        // 队列名1
        return new Queue("myQueue2");
    }

    @Bean
    @Qualifier("myQueue3")
    public Queue getQueue3() {
        // 队列名2
        return new Queue("myQueue3");
    }

    @Bean
    @Qualifier("myQueue4")
    public Queue getQueue4() {
        // 队列名3
        return new Queue("myQueue4");
    }
    /**
     * 创建交换机
     */
    @Bean
    public FanoutExchange getFanoutExchange() {
        // 交换机名字
        return new FanoutExchange("myFanoutExchange");
    }

    /**
     * 队列绑定到交换机上
     */

    @Bean//将myQueue3队列绑定到myFanoutExchange这个交换机上
    public Binding bingding2(@Qualifier("myQueue3") Queue q2, FanoutExchange myFanoutExchange){
        return BindingBuilder.bind(q2).to(myFanoutExchange);
    }
    @Bean
    public Binding bingding3(@Qualifier("myQueue4") Queue q3,FanoutExchange myFanoutExchange){
        return BindingBuilder.bind(q3).to(myFanoutExchange);
    }
    @Bean
    @Qualifier("topic1")
    public Queue getMyQueue1() {
        // 队列名1
        return new Queue("topic1");
    }

    @Bean
    @Qualifier("topic2")
    public Queue getMyQueue2() {
        // 队列名2
        return new Queue("topic2");
    }

    @Bean
    @Qualifier("hw_data")
    public Queue getMyQueue3() {
        // 队列名3
        return new Queue("hw_data");
    }
    /**
     * 创建主题类型交换机
     */
    @Bean
    public TopicExchange getTopicExchange() {
        // 交换机名字
        return new TopicExchange("myTopicExchange");
    }
    /**
     * 队列绑定到交换机上,并且指定routingKey
     */
    @Bean
    public Binding bingding6(@Qualifier("topic1") Queue topic1, TopicExchange myTopicExchange){
        return BindingBuilder.bind(topic1).to(myTopicExchange).with("orders.#");
    }
    @Bean
    public Binding bingding4(@Qualifier("topic2") Queue topic2, TopicExchange myTopicExchange){
        return BindingBuilder.bind(topic2).to(myTopicExchange).with("#.log");
    }
    @Bean
    public Binding bingding5(@Qualifier("hw_data")Queue topic3,TopicExchange myTopicExchange){
        return BindingBuilder.bind(topic3).to(myTopicExchange).with("hw_data_key");
    }
}

四、消费者

@Component
@Slf4j
public class RabbitMQConsumer {

    @RabbitHandler
    @RabbitListener(queues = "hw_data") //指定监听哪个队列的消息
    public void receiveMsg(Message msg) {
        System.out.println(msg);
    }
}

五、测试类生产消息进行测试

package com.lsh;

import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author :LiuShihao
 * @date :Created in 2020/8/17 4:44 下午
 * @desc :
 */
@SpringBootTest
public class RabbitMQTest {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Test
    public  void  test1(){
        rabbitTemplate.convertAndSend("hw_data", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss")));
        System.out.println("RabbitTemplate已生产消息");
    }
}

搞定!收工!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu_Shihao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值