SpringBoot整合RabbitMQ
一、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有三种模式:
- 直接模式
- 分裂模式
- 主题模式
流程就是:
- 创建队列
- 创建交换机
- 将队列绑定到交换机
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已生产消息");
}
}
搞定!收工!