springboot整合rabbitmq
springboot整合rabbitmq
rabbitmq简介
路由模式:
交换机:1个交换机接受生产者数据
队列:多个队列提供消息给多个消费者
路由key:交换机和队列的绑定key
POM添加rabbitmq
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
maven import导入包
交换机配置
新建ExchangeConfig.java文件,配置1个交换机
import org.springframework.amqp.core.DirectExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 17:36
*/
@Configuration
public class ExchangeConfig {
/**
* 消息交换机的名字
*/
public static final String EXCHANGE = "exchangeTest";
/**
* 1.定义direct exchange,绑定queueTest
* 2.durable="true" rabbitmq重启的时候不需要创建新的交换机
* 3.direct交换器相对来说比较简单,匹配规则为:如果路由键匹配,消息就被投送到相关的队列
* fanout交换器中没有路由键的概念,他会把消息发送到所有绑定在此交换器上面的队列中。
* topic交换器你采用模糊匹配路由键的原则进行转发消息到队列中
* key: queue在该direct-exchange中的key值,当消息发送给direct-exchange中指定key为设置值时,
* 消息将会转发给queue参数指定的消息队列
*/
@Bean
public DirectExchange directExchange() {
DirectExchange directExchange = new DirectExchange(EXCHANGE, true, false);
return directExchange;
}
}
队列配置
新建QueueConfig.java 文件,配置2个队列
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 17:35
*/
@Configuration
public class QueueConfig {
@Bean
public Queue firstQueue() {
/**
durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
exclusive 表示该消息队列是否只在当前connection生效,默认是false
*/
return new Queue("first-queue", true, false, false);
}
@Bean
public Queue secondQueue() {
return new Queue("second-queue", true, false, false);
}
}
RabbitConfig配置
新建RabbitConfig配置文件,交换机和队列之间,绑定key
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 16:34
*/
/**
* Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指定的方式进行传输,
* Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
* Queue:消息的载体,每个消息都会被投到一个或多个队列。
* Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来.
* Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
* vhost:虚拟主机,一个broker里可以有多个vhost,用作不同用户的权限分离。
* Producer:消息生产者,就是投递消息的程序.
* Consumer:消息消费者,就是接受消息的程序.
* Channel:消息通道,在客户端的每个连接里,可建立多个channel.
*/
@Configuration
public class RabbitConfig {
/**
* 队列key1
*/
public static final String ROUTINGKEY1 = "queue_one_key1";
/**
* 队列key2
*/
public static final String ROUTINGKEY2 = "queue_one_key2";
@Autowired
private QueueConfig queueConfig;
@Autowired
private ExchangeConfig exchangeConfig;
/**
* 连接工厂
*/
@Autowired
private ConnectionFactory connectionFactory;
/**
* 将消息队列1和交换机进行绑定
*/
@Bean
public Binding binding_one() {
return BindingBuilder.bind(queueConfig.firstQueue()).to(exchangeConfig.directExchange()).with(RabbitConfig.ROUTINGKEY1);
}
/**
* 将消息队列2和交换机1进行绑定
*/
@Bean
public Binding binding_one_two() {
return BindingBuilder.bind(queueConfig.firstQueue()).to(exchangeConfig.directExchange()).with(RabbitConfig.ROUTINGKEY2);
}
/**
* 将消息队列2和交换机2进行绑定
*/
@Bean
public Binding binding_two() {
return BindingBuilder.bind(queueConfig.secondQueue()).to(exchangeConfig.directExchange()).with(RabbitConfig.ROUTINGKEY2);
}
/**
* 定义rabbit template用于数据的接收和发送
*
* @return
*/
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
return template;
}
/**
* 定义rabbitAdmin用于动态新建交换机和队列
*
* @return
*/
@Bean
RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
}
消息生产者
import com.ruoyi.framework.config.rabbitmq.ExchangeConfig;
import com.ruoyi.framework.config.rabbitmq.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 17:02
*/
@Component
public class MsgProducer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private RabbitAdmin rabbitAdmin;
/**
* 发送消息
*
* @param uuid
* @param message 消息
*/
public void send(String uuid, Object message) {
CorrelationData correlationId = new CorrelationData(uuid);
rabbitTemplate.convertAndSend(ExchangeConfig.EXCHANGE, RabbitConfig.ROUTINGKEY2,
message, correlationId);
}
/**
* 发送消息
*
* @param uuid
* @param message 消息
*/
public void sendAdmin(String uuid, Object message) {
DirectExchange directExchange = new DirectExchange(ExchangeConfig.EXCHANGE, true, false);
rabbitAdmin.declareExchange(directExchange);
Queue queue = new Queue("testQueue", true, false, false);
rabbitAdmin.declareQueue(queue);
Binding binding = BindingBuilder.bind(queue).to(directExchange).with("testKey");
rabbitAdmin.declareBinding(binding);
CorrelationData correlationId = new CorrelationData(uuid);
rabbitTemplate.convertAndSend(ExchangeConfig.EXCHANGE, "testKey", message, correlationId);
String testQueue = (String) rabbitTemplate.receiveAndConvert("testQueue");
System.out.println(testQueue);
}
}
消息消费者
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 17:04
*/
@Component
public class MsgReceiver {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@RabbitListener(queues = {"first-queue"}, containerFactory = "rabbitListenerContainerFactory")
public void handleMessage(String message) throws Exception {
// 处理消息
System.out.println("FirstConsumer {} handleMessage :" + message);
}
@RabbitListener(queues = {"second-queue"}, containerFactory = "rabbitListenerContainerFactory")
public void handleMessage2(String message) throws Exception {
// 处理消息
System.out.println("SecondConsumer {} handleMessage :" + message);
}
}
controller测试
import com.ruoyi.framework.rabbitmq.MsgProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @packageName:
* @description:
* @author: xumiaofeng
* @date: 2019/3/20 0020 17:43
*/
@RestController
public class SendController {
@Autowired
private MsgProducer firstSender;
@GetMapping("/send")
public String send(String message) {
String uuid = UUID.randomUUID().toString();
// firstSender.send(uuid, message);
firstSender.sendAdmin(uuid, message);
return uuid;
}
}