一、RabbitMQ简介
1、什么是RabbitMQ
RabbitMQ 是一个由 Erlang 语言开发的 AMQP 的开源实现。
AMQP :Advanced Message Queue,高级消息队列协议。它是应用层协议的一个开放标准,为面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产品、开发语言等条件的限制。
2、RabbitMQ的特点
RabbitMQ最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。具体特点包括:
可靠性(Reliability)RabbitMQ 使用一些机制来保证可靠性,如持久化、传输确认、发布确认。
灵活的路由(Flexible Routing)在消息进入队列之前,通过 Exchange 来路由消息的。对于典型的路由功能,RabbitMQ已经提供了一些内置的 Exchange 来实现。针对更复杂的路由功能,可以将多个Exchange 绑定在一起,也通过插件机制实现自己的 Exchange 。
消息集群(Clustering)多个 RabbitMQ 服务器可以组成一个集群,形成一个逻辑 Broker 。
高可用(Highly Available Queues)队列可以在集群中的机器上进行镜像,使得在部分节点出问题的情况下队列仍然可用。
多种协议(Multi-protocol)RabbitMQ 支持多种消息队列协议,比如 STOMP、MQTT 等等。
多语言客户端(Many Clients)RabbitMQ 几乎支持所有常用语言,比如 Java、.NET、Ruby 等等。
管理界面(Management UI)RabbitMQ 提供了一个易用的用户界面,使得用户可以监控和管理消息 Broker 的许多方面。
跟踪机制(Tracing)如果消息异常,RabbitMQ 提供了消息跟踪机制,使用者可以找出发生了什么。
插件机制(Plugin System)RabbitMQ 提供了许多插件,来从多方面进行扩展,也可以编写自己的插件。
3、架构图与主要概念
(1)、架构图
(2)、主要概念
RabbitMQ Server: 也叫broker server,它是一种传输服务。 他的角色就是维护一条从Producer到Consumer的路线,保证数据能够按照指定的方式进行传输。
Producer: 消息生产者,如图A、B、C,数据的发送方。消息生产者连接RabbitMQ服务器然后将消息投递到Exchange。
Consumer:消息消费者,如图1、2、3,数据的接收方。消息消费者订阅队列,RabbitMQ将Queue中的消息发送到消息消费者。
Exchange:生产者将消息发送到Exchange(交换器),由Exchange将消息路由到一个或多个Queue中(或者丢弃)。Exchange并不存储消息。RabbitMQ中的Exchange有direct、fanout、topic、headers四种类型,每种类型对应不同的路由规则。
Queue:(队列)是RabbitMQ的内部对象,用于存储消息。消息消费者就是通过订阅队列来获取消息的,RabbitMQ中的消息都只能存储在Queue中,生产者生产消息并最终投递到Queue中,消费者可以从Queue中获取消息并消费。多个消费者可以订阅同一个Queue,这时Queue中的消息会被平均分摊给多个消费者进行处理,而不是每个消费者都收到所有的消息并处理。
RoutingKey:生产者在将消息发送给Exchange的时候,一般会指定一个routing key,来指定这个消息的路由规则,而这个routing key需要与Exchange Type及binding key联合使用才能最终生效。在Exchange Type与binding key固定的情况下(在正常使用时一般这些内容都是固定配置好的),我们的生产者就可以在发送消息给Exchange时,通过指定routing key来决定消息流向哪里。RabbitMQ为routing key设定的长度限制为255bytes
Connection: (连接):Producer和Consumer都是通过TCP连接到RabbitMQ Server的。以后我们可以看到,程序的起始处就是建立这个TCP连接。
Channels: (信道):它建立在上述的TCP连接中。数据流动都是在Channel中进行的。也就是说,一般情况是程序起始建立TCP连接,第二步就是建立这个Channel。
VirtualHost:权限控制的基本单位,一个VirtualHost里面有若干Exchange和MessageQueue,以及指定被哪些user使用
二、RabbitMQ下载安装与使用
1、Windows下的RabiitMQ的下载与安装
(1)、下载
下载地址:https://download.csdn.net/download/wingzhezhe/10541001
(2)、安装Eralng
管理员权限安装 otp_win64_20.2.exe 即可
配置环境变量
(3)、安装RabbitMQ
安装 rabbitmq-server-3.7.4.exe 即可
(4)、安装管理界面(插件)
进入rabbitMQ安装目录的sbin目录下,执行如下命令:
rabbitmq-plugins enable rabbitmq_management
(5)、重新启动服务,浏览器输入地址
控制面板服务中重启rabbitMQ服务,浏览器访问效果如下:
2、Docker环境下安装rabbitMQ
(1)、拉取镜像
docker pull rabbitmq:management
(2)、启动容器
docker run -di --name=tensquare_rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 rabbitmq:management
创建容器,rabbitmq需要有映射以下端口: 5671 5672 4369 15671 15672 25672
15672 : (if management plugin is enabled),管理插件端口
15671 : management监听端口
5672, 5671 : (AMQP 0-9-1 without and with TLS)
4369 : (epmd) epmd 代表 Erlang 端口映射守护进程
25672 : (Erlang distribution)
3、Rabbit的使用环境搭建
(1)、创建Maven project
(2)、pom.xml中加入依赖坐标
<!-- 指定springboot父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入mq依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 引入junit测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
(3)、配置主配置文件和启动引导类
application.yml
RabbitMQApplication.java
package com.rabbit.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQApplicaiton {
public static void main(String[] args) {
SpringApplication.run(RabbitMQApplicaiton.class,args);
}
}
3、直接模式(Direct 点对点模式)
(1)、Direct模式介绍
我们需要将消息发给唯一一个节点时使用这种模式,这是最简单的一种形式。任何发送到Direct Exchange的消息都会被转发到RouteKey中指定的Queue。
一般情况可以使用rabbitMQ自带的Exchange:”"(该Exchange的名字为空字符串,下文称其为default Exchange)。
这种模式下不需要将Exchange进行任何绑定(binding)操作
消息传递时需要一个“RouteKey”,可以简单的理解为要发送到的队列名字。
如果vhost中不存在RouteKey中指定的队列名,则该消息会被抛弃。
(2)、使用管理工具创建队列
(3)、测试代码实现生产者
package com.rabbit.test;
import com.rabbit.demo.RabbitMQApplicaiton;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 点对点模式消息生产者测试类
*/
//测试类标识简写
@RunWith(SpringRunner.class)
//启动引导类
@SpringBootTest(classes = RabbitMQApplicaiton.class)
public class ProcedureTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 测试点对点消息生产者
*/
@Test
public void testQueueSend(){
//参数1:队列名称
rabbitTemplate.convertAndSend("dest_direct", "测试点对点");
}
}
(4)、测试代码实现消费者
package com.rabbit.demo.direct;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 点对点模式消息监听类
*/
@Component
@RabbitListener(queues = "dest_direct")
public class DirectListener {
@RabbitHandler
public void onMassage(String message){
System.out.println("点对点模式接收到生产者的消息是:" + message);
}
}
4、分列模式(Fanout)
(1)、什么是分列模式
当我们需要将消息一次发给多个队列时,需要使用这种模式,任何发送到Fanout Exchange的消息都会被转发到与该Exchange绑定(Binding)的所有的Queue上。
可以理解为路由表的模式
这种模式不需要RouteKey
这种模式需要提前将Exchange与Queue进行绑定,一个Exchange可以绑定多个Queue,一个Queue可以同多个Exchange进行绑定。
如果接受到消息的Exchange没有与任何Queue绑定,则消息会被抛弃。
(2)、创建队列和交换器,并将队列和交换器绑定
使用管理工具添加队列boxuegu和kudingyu
新建交换器 chuanzhiboke
点击进入chuanzhiboke交换器,将boxuegu和kudingyu绑定到chuanzhiboke上
(3)、代码实现生产者
/**
* 测试分列模式消息生产者
*/
@Test
public void testFanoutSend(){
//参数1:交换器name值
rabbitTemplate.convertAndSend("chuanzhiboke", "", "测试分列模式发送");
}
(4)、代码实现消费者
BoxueguFanoutListener.java
package com.rabbit.demo.fanout;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 分列模式消费者监听类
*/
@Component
@RabbitListener(queues = "boxuegu")
public class BoxueguFanoutListener {
@RabbitHandler
public void onMessage(String message){
System.out.println("博学谷接收到的消息是:" + message);
}
}
KudingyuFanoutListener.java
package com.rabbit.demo.fanout;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 分列模式消费者监听类
*/
@Component
@RabbitListener(queues = "kudingyu")
public class KudingyuFanoutListener {
@RabbitHandler
public void onMessage(String message){
System.out.println("酷丁鱼接收到的消息是:" + message);
}
}
(5)、启动测试类,查看控制台输出
5、主题模式(Topic)
(1)、什么是主题模式
任何发送到Topic Exchange的消息都会被转发到所有关心RouteKey中指定话题的Queue上。
如上图所示此类交换器使得来自不同的源头的消息可以到达一个对列,其实说的更明白一点就是模糊匹配的意思,例如:上图中红色对列的routekey为usa.#,#代表匹配任意字符,但是要想消息能到达此对列,usa.必须匹配后面的#好可以随意。图中usa.news usa.weather,都能找到红色队列,符号 # 匹配一个或多个词,符号 * 匹配不多不少一个词。因此 usa.# 能够匹配到 usa.news.XXX ,但是 usa.* 只会匹配到 usa.XXX 。
交换器说到底是一个名称与队列绑定的列表。当消息发布到交换器时,实际上是由你所连接的信道,将消息路由键同交换器上绑定的列表进行比较,最后路由消息。
任何发送到Topic Exchange的消息都会被转发到所有关心RouteKey中指定话题的Queue上。
这种模式较为复杂,简单来说,就是每个队列都有其关心的主题,所有的消息都带有一个“标题”(RouteKey),Exchange会将消息转发到所有关注主题能与RouteKey模糊匹配的队列。
这种模式需要RouteKey,也许要提前绑定Exchange与Queue。
在进行绑定时,要提供一个该队列关心的主题,如“#.log.#”表示该队列关心所有涉及log的消息(一个RouteKey为”MQ.log.error”的消息会被转发到该队列)。
“#”表示0个或若干个关键字,“”表示一个关键字。如“log.”能与“log.warn”匹配,无法与“log.warn.timeout”匹配;但是“log.#”能与上述两者匹配。
同样,如果Exchange没有发现能够与RouteKey匹配的Queue,则会抛弃此消息
(2)、创建队列,绑定交换器
创建一个交换器
绑定路由规则(队列)
(3)、Topic模式生产者代码
/**
* 符合路由goods.#规则的Topic生产者
*/
@Test
public void testTopic1Send(){
//参数1:交换器name值,参数2:路由规则,参数3:发送的内容
rabbitTemplate.convertAndSend("topic_test", "goods.test1", "Topic模式发送的数据");
}
/**
* 符合路由#.log规则的Topic生产者
*/
@Test
public void testTopic2Send(){
//参数1:交换器name值,参数2:路由规则,参数3:发送的内容
rabbitTemplate.convertAndSend("topic_test", "article.content.log", "Topic模式发送的数据");
}
/**
* 符合路由goods.log、goods.#、#.log规则的Topic生产者
*/
@Test
public void testTopic3Send(){
//参数1:交换器name值,参数2:路由规则,参数3:发送的内容
rabbitTemplate.convertAndSend("topic_test", "goods.log", "Topic模式发送的数据");
}
/**
* 同时符合路由goods.#规则和路由#.log规则的Topic生产者
*/
@Test
public void testTopic4Send(){
//参数1:交换器name值,参数2:路由规则,参数3:发送的内容
rabbitTemplate.convertAndSend("topic_test", "goods.content.log", "Topic模式发送的数据");
}
(4)、Topic模式消费者代码
消息监听者同Direct和Fanout
三、用户微服务相关功能
1、搭建环境
(1)、使用代码生成器生成用户微服务项目并导入父工程中,修改主配置文件和启动引导类
(2)、在pom.xml中引入redis和rabbitmq依赖
<!-- 加入redis和rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2、发送短信验证码功能
注意:需要先创建对应的队列,否则无法发送消息
UserService.java
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 发送验证码
*
* @param mobile
*/
public void sendSms(String mobile) {
//1.生成6位数的验证码
Random random = new Random();
int max = 999999;//设置最大数
int min = 100000;//最小数
int code = random.nextInt(max);//随机生成验证码
if (code < min) {
code = code + min;
}
//2.将验证码放入redis中
redisTemplate.opsForValue().set("smscode_" + mobile, code + "", 5, TimeUnit.MINUTES);
//3.将验证码和手机号发送到rabbitmq中
Map<String, String> map = new HashMap<>();
map.put("code", code + "");
map.put("mobile", mobile);
rabbitTemplate.convertAndSend("sms", map);
}
UserController.java
/**
* 发送验证码
* @param mobile
* @return
*/
@RequestMapping(value = "/sendsms/{mobile}", method = RequestMethod.POST)
public Result sendSms(@PathVariable String mobile){
userService.sendSms(mobile);
return new Result(true, StatusCode.OK, "发送成功");
}
3、用户注册功能
UserService.java
/**
* 注册用户
* @param user
* @param code
*/
public void register(User user, String code) {
//判断验证码是否正确
String redisCode = (String) redisTemplate.opsForValue().get("smscode_" + user.getMobile());
if (redisCode == null) {
throw new RuntimeException("请点击获取正确的验证码!");
}
if (!redisCode.equalsIgnoreCase(code)) {
throw new RuntimeException("请输入正确的验证码!");
}
//设置用户属性字段
user.setId(idWorker.nextId() + "");
user.setFollowcount(0);//关注数
user.setFanscount(0);//粉丝数
user.setOnline(0L);//在线时长
user.setRegdate(new Date());//注册时间
user.setUpdatedate(new Date());//修改时间
user.setLastdate(new Date());//最后登录时间
userDao.save(user);
}
UserController.java
/**
* 注册用户
* @param user
*/
@RequestMapping(value = "/register/{code}",method=RequestMethod.POST)
public Result add(@RequestBody User user, @PathVariable String code){
userService.register(user, code);
return new Result(true,StatusCode.OK,"注册成功");
}
四、短信发送微服务
1、工程环境准备
(1)、创建Maven Module ,pom.xml中加入依赖
<dependencies>
<!-- rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--阿里云依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
(2)、编写启动引导类以及主配置文件
SmsApplication.java
package com.scf.sms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class);
}
}
(3)、加入短信发送工具类
package com.scf.sms;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 短信工具类
* @author Administrator
*
*/
@Component
public class SmsUtil {
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
@Autowired
private Environment env;
// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
/**
* 发送短信
* @param mobile 手机号
* @param template_code 模板号
* @param sign_name 签名
* @param param 参数
* @return
* @throws ClientException
*/
public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {
String accessKeyId =env.getProperty("aliyun.sms.accessKeyId");
String accessKeySecret = env.getProperty("aliyun.sms.accessKeySecret");
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(mobile);
//必填:短信签名-可在短信控制台中找到
request.setSignName(sign_name);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(template_code);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(param);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("yourOutId");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public QuerySendDetailsResponse querySendDetails(String mobile,String bizId) throws ClientException {
String accessKeyId =env.getProperty("accessKeyId");
String accessKeySecret = env.getProperty("accessKeySecret");
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-号码
request.setPhoneNumber(mobile);
//可选-流水号
request.setBizId(bizId);
//必填-发送日期 支持30天内记录查询,格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页大小
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
//hint 此处可能会抛出异常,注意catch
QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
return querySendDetailsResponse;
}
}
2、编写发送短信的消息监听
SmsListener.java
package com.scf.sms;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "sms")
public class SmsListener {
@Value("${aliyun.sms.template_code}")
private String template_code;
@Value("${aliyun.sms.sign_name}")
private String sign_name;
@Autowired
private SmsUtil smsUtil;
@RabbitHandler
public void onMessage(Map<String, String> map){
System.out.println("手机号 : " + map.get("mobile"));
System.out.println("验证码 : " + map.get("code"));
try {
smsUtil.sendSms(map.get("mobile"), template_code, sign_name, "{\"code\":\"" + map.get("code") + "\"}");
} catch (Exception e) {
e.printStackTrace();
}
}
}