1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、修改application.yml配置文件
spring:
rabbitmq:
host=127.0.0.1
port=5672
username=guest
password=guest
3、创建消息生产者配置类
package com.test.config.mq;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProducerConfig {
public static final String EXCHANGE = "test.rabbitmq.exchange"; // 交换器名称
public static final String ROUTINGKEY = "test.rabbitmq.routingkey"; // 设置路由key
public static final String QUEUE_NAME = "test.rabbitmq.queue"; // 队列名称
@Bean
public Binding bindingExchangeQueue() {
return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTINGKEY) ;
}
@Bean
public DirectExchange directExchange() { // 使用直连的模式
return new DirectExchange(EXCHANGE, true, true);
}
@Bean
public Queue queue() { // 要创建的队列信息
return new Queue(QUEUE_NAME);
}
}
4、创建消费者配置类
package com.test.config.mq;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConsumerConfig {
public static final String EXCHANGE = "test.rabbitmq.exchange"; // 交换器名称
public static final String ROUTINGKEY = "test.rabbitmq.routingkey"; // 设置路由key
public static final String QUEUE_NAME = "test.rabbitmq.queue"; // 队列名称
@Bean
public Binding bindingExchangeQueue() {
return BindingBuilder.bind(queue()).to(directExchange()).with(ROUTINGKEY) ;
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange(EXCHANGE, true, true);
}
// 要创建的队列信息
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME);
}
}
5、创建消息发送类
@Component
public class MessageProvider{
@Autowired
private AmqpTemplate rabbitTemplate;
public void converAndSend(String exchangName, String routeKey, Object object){
rabbitTemplate.converAndSend(exchangName, routeKey, object);
}
}
6、消费者(可以在控制台看自己消费的内容)
@Component
public class MessageConsumer {
@RabbitListener(queues="test.rabbitmq.queue")
public void receiveMsg(Message msg) { // 进行消息接收处理
try {
String jsonStr = new String(msg.getBody(), "UTF-8");
System.out.println("【*** 接收消息 ***】" + jsonStr);
} catch(Exception e){
e.prinStackTrace();
}
}
}
7、场景调用MQ
@Servere
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
@Autowired
private MessageProvider messageProvider;
@Override
public int insertUser(User user){
int num = userMapper.insertUser(user);
messageProvider.convertAndSend("test.rabbitmq.exchange", "test.rabbitmq.routingkey", "test.rabbitmq.queue");
return num;
}
}
注意:
消息确认 因为在属性配置文件里面开启了ACK确认 所以如果代码没有执行ACK确认 你在RabbitMQ的后台会看到消息会一直留在队列里面未消费掉 只要程序一启动开始接受该队列消息的时候 又会收到。添加
channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
8、总结
RabbitMQ是消息中间件的一种,使用的是 AMQP 处理,该处理属于一种协议处理,所以处理的性能会比较高,在 RabbitMQ 里面提供有 exchange、 queue、bind 的概念,所有的用户提交的消息发送给 exchange,而后由 bind 绑定 exchange 与 queue,最后根据 routingkey 进行消息 的发送处理,利用这一概念可以实现 fanout(广播)、topic(主题)、direct(直连)的操作处理。同时在 Rabbitmq 之中还通过有虚 拟主机的概念,也就是说不同的虚拟主机可以有自己独立的用户管理、空间管理。
四种模式:
Direct是RabbitMQ默认的交换机模式,也是最简单的模式.即创建消息队列的时候,指定一个BindingKey.当发送者发送消息的时候,指定对应的Key.当Key和消息队列的BindingKey一致的时候,消息将会被发送到该消息队列中.
topic转发信息主要是依据通配符,队列和交换机的绑定主要是依据一种模式(通配符+字符串),而当发送消息的时候,只有指定的Key和该模式相匹配的时候,消息才会被发送到该消息队列中.
headers也是根据一个规则进行匹配,在消息队列和交换机绑定的时候会指定一组键值对规则,而发送消息的时候也会指定一组键值对规则,当两组键值对规则相匹配的时候,消息会被发送到匹配的消息队列中.
Fanout是路由广播的形式,将会把消息发给绑定它的全部队列,即便设置了key,也会被忽略.