9.RabbitMQ
同步通讯与异步通讯
同步
异步
常见实现是事件驱动模式
常见技术
安装部署
使用centos7_docker进行安装
docker pull rabbitmq:3-management
运行
15672是控制台端口
docker run \
-d \
--name mq \
--hostname mq1 \
-e RABBITMQ_DEFAULT_USER=test \
-e RABBITMQ_DEFAULT_PASS=test \
-p 15672:15672 \
-p 5672:5672 \
rabbitmq:3-management
如果有问题,开启ipv4转发
#IPv4 forwarding is disabled. Networking will not work.
vim /etc/sysctl.conf
#配置转发
net.ipv4.ip_forward=1
#保存退出就可以了
:wq!
#重启服务,让配置生效
systemctl restart network
#查看是否成功,如果返回为“net.ipv4.ip_forward = 1”则表示成功
sysctl net.ipv4.ip_forward
此时查看docker ps可以看到rabbitMQ正在运行
进入页面,账号密码就是启动命令设置的
基本架构
常见消息模型
简单队列模型(原生api)
生产者
public class PublisherTest {
@Test
public void testSendMessage() throws IOException, TimeoutException {
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
factory.setHost("192.168.111.128");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("test");
factory.setPassword("test");
// 1.2.建立连接
Connection connection = factory.newConnection();
// 2.创建通道Channel
Channel channel = connection.createChannel();
// 3.创建队列
String queueName = "simple.queue";
channel.queueDeclare(queueName, false, false, false, null);
// 4.发送消息
String message = "hello, rabbitmq!";
channel.basicPublish("", queueName, null, message.getBytes());
System.out.println("发送消息成功:【" + message + "】");
// 5.关闭通道和连接
channel.close();
connection.close();
}
}
消费者
注:不要使用maven运行托管
public class ConsumerTest {
public static void main(String[] args) throws IOException, TimeoutException {
// 1.建立连接
ConnectionFactory factory = new ConnectionFactory();
// 1.1.设置连接参数,分别是:主机名、端口号、vhost、用户名、密码
factory.setHost("192.168.111.128");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("test");
factory.setPassword("test");
// 1.2.建立连接
Connection connection = factory.newConnection();
// 2.创建通道Channel
Channel channel = connection.createChannel();
// 3.创建队列
String queueName = "simple.queue";
channel.queueDeclare(queueName, false, false, false, null);
// 4.订阅消息
channel.basicConsume(queueName, true, new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException {
// 5.处理消息
String message = new String(body);
System.out.println("接收到消息:【" + message + "】");
}
});
System.out.println("等待接收消息。。。。");
}
}
SpringAMQP
简单队列模型
-
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency>
-
在publisher和consumer配置文件中设置mq连接信息
spring: rabbitmq: host: 192.168.111.128 port: 5672 virtual-host: / username: test password: test
-
使用RabbitTemplate发送消息
@RunWith(SpringRunner.class) @SpringBootTest public class SpringAmqpTest { @Resource private RabbitTemplate rabbitTemplate; @Test public void testSendMessage2SimpleQueue(){ String queueName = "simple.queue"; String message = "hello,spring ampq!"; rabbitTemplate.convertAndSend(queueName,message); } }
-
接收消息,并启动springboot
@Component public class SpringRabbitListener { @RabbitListener(queues = "simple.queue") public void listenSimpleQueue(String msg) { System.out.println("接收到消息:" + msg); } }
工作队列
@Test
public void testSendMessage2WorkQueue() throws InterruptedException {
String queueName = "simple.queue";
String message = "hello,spring ampq!";
for (int i = 1; i <= 50; i++) {
rabbitTemplate.convertAndSend(queueName, message + i);
Thread.sleep(20);
}
}
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue1(String msg) throws InterruptedException {
System.out.println("接收到消息:" + msg + "--------" + LocalTime.now());
Thread.sleep(20);
}
@RabbitListener(queues = "simple.queue")
public void listenWorkQueue2(String msg) throws InterruptedException {
System.err.println("接收到消息:" + msg + "--------" + LocalDateTime.now());
Thread.sleep(200);
}
可以发现,两个消费者是对半分的,因为有消息预取措施(就是队列中的消息,先拿到自己这,然后再进行处理,并不根据自己的消费能力获取)
可以修改配置文件,设置消息预取的上限
spring:
rabbitmq:
host: 192.168.111.128
port: 5672
virtual-host: /
username: test
password: test
listener:
simple:
prefetch: 1 # 每次只能获取一个消息,处理完成才能获取下一个消息
可以看到,消费能力强的消费者消费的多
发布、订阅
Fanout(广播)
发送消息到所有的queue
-
在consumer服务创建一个类,添加@Configuration注解,并声明FanoutExchange、Queue和绑定关系对象Binding
@Configuration public class FanoutConfig { //交换机 @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("rokned.fanout"); } //队列1 @Bean public Queue fanoutQ1(){ return new Queue("fanout.q1"); } //队列2 @Bean public Queue fanoutQ2(){ return new Queue("fanout.q2"); } //绑定队列1 @Bean public Binding fanoutBinding1(Queue fanoutQ1,FanoutExchange fanoutExchange){ return BindingBuilder.bind(fanoutQ1).to(fanoutExchange); } //绑定队列2 @Bean public Binding fanoutBinding2(Queue fanoutQ2,FanoutExchange fanoutExchange){ return BindingBuilder.bind(fanoutQ2).to(fanoutExchange); } }
-
监听队列
@RabbitListener(queues = "fanout.queue1") public void listenFanoutQueue1(String msg) { System.out.println("fanout.queue1接收到消息:" + msg); } @RabbitListener(queues = "fanout.queue2") public void listenFanoutQueue2(String msg) { System.out.println("fanout.queue2接收到消息:" + msg); }
-
发送消息
@Test public void testSendFanoutExchange(){ //交换机名称 String exchangeName = "rokned.fanout"; //消息 String msg = "hello,fanout"; //发送 rabbitTemplate.convertAndSend(exchangeName,"",msg); }
-
Direct(路由)
根据路由规则,发送到指定的queue。每一个queue都与Exchange设置一个BindingKey,发送消息时,指定一个RoutingKey,Exchange将消息发送到两个key相同的queue中。
一个queue可以绑定多个key
使用注解方式声明
-
在consumer中编写消费方法,并且使用@RabbitListener声明Exchange、Queue、BindingKey
@RabbitListener(bindings = @QueueBinding( value = @Queue("direct.q1"), exchange = @Exchange(value = "rokned.direct", type = ExchangeTypes.DIRECT), key = {"red", "blue"} )) public void listenDirectQueue1(String msg) { System.out.println("direct.q1接收到消息:" + msg); } @RabbitListener(bindings = @QueueBinding( value = @Queue("direct.q2"), exchange = @Exchange(value = "rokned.direct", type = ExchangeTypes.DIRECT), key = {"red", "yellow"} )) public void listenDirectQueue2(String msg) { System.out.println("direct.q2接收到消息:" + msg); }
-
发送消息
@Test public void testSendDirectExchange(){ //交换机名称 String exchangeName = "rokned.direct"; //消息 String msg = "hello,direct"; //发送 rabbitTemplate.convertAndSend(exchangeName,"yellow",msg + " yellow"); rabbitTemplate.convertAndSend(exchangeName,"red",msg + " red"); }
Topic(话题)
与DirectExchange类似,但是TopicExchange的routingKey必须是多个单词列表,且以**.
**分割
在Queue与Exchange指定BindingKey时可以使用通配符:
- # : 代指0个或多个单词
- * :代指一个单词
代码与Direct类似
接收消息
@RabbitListener(bindings = @QueueBinding(
value = @Queue("topic.q1"),
exchange = @Exchange(value = "rokned.topic", type = ExchangeTypes.TOPIC),
key = {"#.news"}
))
public void listenTopicQueue1(String msg) {
System.out.println("topic.q1接收到消息:" + msg);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue("topic.q2"),
exchange = @Exchange(value = "rokned.topic", type = ExchangeTypes.TOPIC),
key = {"china.#"}
))
public void listenTopicQueue2(String msg) {
System.out.println("topic.q2接收到消息:" + msg);
}
发送消息
@Test
public void testSendTopicExchange(){
//交换机名称
String exchangeName = "rokned.topic";
//消息
String msg = "hello,topic";
//发送
rabbitTemplate.convertAndSend(exchangeName,"china.weather",msg + " china.weather");
rabbitTemplate.convertAndSend(exchangeName,"china.news",msg + " china.news");
}
消息转换器
spring的消息处理是由org.springframework.amqp.support.converter.MessageConverter处理的。默认实现的是SimpleMessageConverter,基于JDK的ObjectOutputStream完成序列化(比较垃圾)
如果要修改,直接定义一个MessageConverter类型的bean即可,推荐使用JSON方式进行序列化
-
引入jackson依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>
-
在publisher声明MessageConverter,(注:需要在配置类中声明,启动类也是配置类)
@Bean public MessageConverter messageConverter(){ return new Jackson2JsonMessageConverter(); }
发送消息是json,同理接收消息也需要进行转换。
-
在consumer进入jackson依赖,和publisher一样,可以在父工程直接引入
-
在consumer服务定义MessageConverter,与publisher一样
-
定义消费者
@RabbitListener(queues = "object.queue") public void listenObjectQueue(Map<String,Object> msg){ System.out.println("接收到object.queue消息:" + msg); }