1.简介
1.1 什么是RabbitMq
RabbitMq是一个由erlang开发的AMQP(Advanced Message Queue 高级消息队列协议)的开源实现,RabbitMq是一个消息代理:它接收和转发消息。
1.2 RabbitMq的优点
1)异步消息处理
2)业务解耦
3)错峰流控
4)处理分布式事务
1.3 RabbitMq中的重要角色
1)生产者:消息的创建者,负责创建和推送数据到消息服务器
2)消费者:消息的接收方,用于处理数据和确认消息
3)代理:就是RabbitMq本身,不生产消息,只做消息代理
1.4 RabbitMq中的重要组件
1)ConnectionFactory:连接管理器,应用程序与Rabbit之间简历连接的管理器
2)channel:信道,消息推送使用的通道
3)Exchange:交换器,用于接收,分配消息
4)Queue:队列,用于存储生产者的消息
5)RoutingKey:路由键,用户把生产者的数据分配到交换器上
6)BindingKey:绑定键,用于把交换器的消息绑定到队列上
1.5 RabbitMq中的广播类型
1)direct:默认方式,最基础最简单的模式,发送方把消息发送给订阅方,如果有多个订阅者,默认采取轮询的方式进行消息发送
2)headers:与direct类似,只是性能很差,此类型几乎用不到
3)fanout:分发模式,把消费分发给所有订阅者
4)topic:匹配订阅模式,使用正则匹配到消息队列,能匹配到的都能接收
1.6 SpringAMQP 主要对象
1.7 RabbitMq的运行流程
2.安装启动RabbitMq
2.1 docker 拉起镜像
docker pull rabbitmq:3-management
2.2 docker 安装RabbitMq
userName,password 请自行设置,--name 是名称,可以自定义
docker run \
-e RABBITMQ_DEFAULT_USER=userName \
-e RABBITMQ_DEFAULT_PASS=password \
--name mq \
--hostname mq1 \
-p 15672:15672 \
-p 5672:5672 \
-d \
rabbitmq:3-management
2.3 docker 启动RabbitMq
docker start mq
2.4 访问RabbitMq
网址为你的虚拟机ip加上15672,密码和用户名是自己设置,登录进去页面如下
3.集成RabbitMq
3.1 项目结构图
publisher是生产者模块,consumer是消费者模块
3.2 导入RabbitMq 相关依赖,版本有由SpringBoo管理
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3.3 在publisher创建一个test类,生产消息
package cn.zb.publisher;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PublisherTest {
@Test
public void test() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.182.136");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("as");
factory.setPassword("123456");
//建立连接
Connection connection = factory.newConnection();
//创建信道Channel
Channel channel = connection.createChannel();
//创建交换机
channel.exchangeDeclare("simple:exchange","direct",false,false,null);
//创建队列
channel.queueDeclare("simple:queue",false,false,false,null);
//队列绑定交换机,"as" 为routingkey
channel.queueBind("simple:queue","simple:exchange","as");
//发送消息,需要传routingkey
String message="hello,rabbitmq!";
channel.basicPublish("simple:exchange","as",null,message.getBytes());
//关闭通道和连接
channel.close();
connection.close();
}
}
3.4 运行,查看效果
点击队列,进入到队列详情,可以看到队列中的消息详情
3.5 consumer编写test类,用于接收
package cn.zb.consumer;
import com.rabbitmq.client.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerTest {
@Test
public void test() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.182.136");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("as");
factory.setPassword("123456");
//建立连接
Connection connection = factory.newConnection();
//创建信道Channel
Channel channel = connection.createChannel();
//接收消息,true表示,接收到传递过滤的消息后应答服务器
channel.basicConsume("simple:queue",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body);
System.out.println("接收到消息:"+message);
}
});
}
}
3.6 运行,存放到队列中的数据被消费,输出
4. SpringBoot 集成RabbitMq
4.1通过application.yml 配置文件中配置RabbitMq
端口号,用户名,密码自行填写,两个模块都是差不多的配置,顶多改一下服务的端口
server:
port: 8082
spring:
rabbitmq:
host:
port: 5672
virtual-host: /
username:
password:
4.2 编写RabbitMqConfig 配置类,两个模块都需要有
package cn.zb.publisher.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;
@Configuration
public class RabbitMqConfig {
public static final String EXCHANGE_DIRECT="exchange_direct";
public static final String QUEUE_MESSAGE="queue:message";
public static final String ROUTINGKEY = "as";
//声明交换机
@Bean(EXCHANGE_DIRECT)
public Exchange EXCHANGE_TOPIC(){
//durable(true) 持久化,mq重启之后交换机还在
//这里指定了交换机的广播方式为DIRECT默认模式
return ExchangeBuilder.topicExchange(EXCHANGE_DIRECT).durable(false).build();
}
//声明队列
@Bean(QUEUE_MESSAGE)
public Queue QUEUE_MESSAGE(){
return new Queue(QUEUE_MESSAGE);
}
//队列绑定交换机
@Bean
public Binding BINDING_QUEUE_MESSAGE(@Qualifier(QUEUE_MESSAGE) Queue queue,@Qualifier(EXCHANGE_DIRECT) Exchange exchange){
//durable(true) 持久化,mq重启之后交换机还在
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY).noargs();
}
}
4.3 Publisher 创建controller编写生产消息,将消息发送到交换机的代码
package cn.zb.publisher.controller;
import cn.zb.publisher.config.RabbitMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PublisherController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/send")
public void sendMessage() throws InterruptedException {
for (int i = 0; i < 10; i++) {
//指定消息发送到那个交换机,routingkey与配置类定义的一致
rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_DIRECT,"as","这是生产者生产的第"+i+"条消息");
Thread.sleep(1000);
}
}
}
4.4consumer 创建RabbitMqListen 编写监听交换机绑定的队列,接收消息
package cn.zb.consumer.listen;
import cn.zb.consumer.config.RabbitMqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMqListen {
@RabbitListener(queues = {RabbitMqConfig.QUEUE_MESSAGE})
public void getMessage(String msg, Message message){
System.out.println(msg);
}
}
4.5 启动项目,发起send请求生成消息
本博客只做记录,参考了其他文献