RabbitMQ 的基本使用和验证码
- 1. 基本使用
- 1.1 导包和添加配置
- 1.2 直接模式
- 1.2.1 建立序列
- 1.2.2 代码测试
- 1.3 分裂模式
- 1.3.1 添加序列和交换机
- 1.3.2 代码测试
- 1.4 主题模式
- 1.4.1 添加序列和交换机
- 1.4.2 代码测试
- 2. 验证码
- 2.1 创建一个代理模块
- 2.1.1 导包
- 2.1.2 配置
- 2.1.3 建立 util 类 SendSms
- 2.1.4 建立 RabbitMQ 监听
- 2.2 User 模板
- 2.2.1 发送手机验证码的代码
- 2.2.2 用户注册得到验证码的代码
- 3. 阿里云验证码的参数
1. 基本使用
1.1 导包和添加配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
1.2 直接模式
1.2.1 建立序列
添加序列, 只需添加序列名, 其他默认点击 Add queue
1. Durability:
Durable 持久化
Transient 暂时的
2. Auto delete:
No 不自动删除
Yes 自动删除
1.2.2 代码测试
发送者
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 直接模式
*/
@Test
public void sendMessage1() {
// 序列, 内容
rabbitTemplate.convertAndSend("itCast", "直接模式测试");
}
接收者
package com.springCloud.rabbit.customer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* queues: 序列名
*/
@RabbitListener(queues = "itCast")
@Component
public class Customer1 {
@RabbitHandler
public void getMessage(String message) {
System.out.println("直接模式消费消息: " + message);
}
}
1.3 分裂模式
1.3.1 添加序列和交换机
添加序列 (略)
添加交换机, 填入名称, 和选择 Type 为 fanout, 其他的不变.
Type:
direct 直接模式
fanout 分裂模式
headers 头模式
topic 主题模式
1.3.2 代码测试
发送者
/**
* 分裂模式
*/
@Test
public void sendMessage2() {
// 序列, 规则, 内容
rabbitTemplate.convertAndSend("itChange", "", "分裂模式测试");
}
多添几个接受者, 记得改序列名.
1.4 主题模式
1.4.1 添加序列和交换机
添加序列 (略)
添加交换机, 填入名称, 和选择 Type 为 topic, 其他的不变. 添加完之后
绑定规则:
#
代表多个,$
代表一个 (几个十一.
1.4.2 代码测试
发送者
/**
* 主题模式
*/
@Test
public void sendMessage3() {
rabbitTemplate.convertAndSend("itTheme", "good.abc.d", "主题模式测试");
}
接收者和上面一样
规则为: good.abc.d
规则为: abc.life
规则为: good.abc.life
规则为: good.life
2. 验证码
运用的是 阿里云 的验证码.
需要登录阿里云账户.
2.1 创建一个代理模块
2.1.1 导包
pom.xml 文件
<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>4.5.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.1.2 配置
配置含义在 util 类中有叙述.
aliyun.sms.accessKeyId=accessKeyId
aliyun.sms.accessKeySecret=accessKeySecret
aliyun.sms.signName=signName
aliyun.sms.templateCode=templateCode
配置完后发现有警告, 可以点击 Alt+Enter, 一直 Enter 会自动创建一个文件 additional-spring-configuration-metadata.json.
文件内容:
{ "properties": [ {
"name": "aliyun.sms.accessKeyId",
"type": "java.lang.String",
"description": "Description for aLiYun.sms.accessKeyId.",
"defaultValue": "accessKeyId"
},
{
"name": "aliyun.sms.accessKeySecret",
"type": "java.lang.String",
"description": "Description for aLiYun.sms.accessKeySecret.",
"defaultValue": "accessKeySecret"
},
{
"name": "aliyun.sms.signName",
"type": "java.lang.String",
"description": "Description for aLiYun.sms.signName.",
"defaultValue": "signName"
},
{
"name": "aliyun.sms.templateCode",
"type": "java.lang.String",
"description": "Description for aLiYun.sms.templateCode.",
"defaultValue": "templateCode"
}
]
}
警告便消失.
2.1.3 建立 util 类 SendSms
package com.portal.sms.util;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* @author Administrator
*/
@Component
public class SendSms {
private Environment environment;
public SendSms() {
}
@Autowired
private SendSms(Environment environment) {
this.environment = environment;
}
/**
* 主函数
*
* @param phone 手机号
* @param code 验证码
*/
public void sendSms(String phone, String code) {
// 阿里云账户的 ACCESS_KEY_ID
String accessKeyId = environment.getProperty("aliyun.sms.accessKeyId");
// 阿里云账户的 ACCESS_KEY_SECRET
String accessKeySecret = environment.getProperty("aliyun.sms.accessKeySecret");
// 阿里云的签名名称
String signName = environment.getProperty("aliyun.sms.signName");
// 阿里云的模板 CODE
String templateCode = environment.getProperty("aliyun.sms.templateCode");
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
// 请求方式
request.setSysMethod(MethodType.POST);
// 请求地址
request.setSysDomain("dysmsapi.aliyuncs.com");
// 请求版本
request.setSysVersion("2017-05-25");
// 请求行为
request.setSysAction("SendSms");
// 请求手机号
request.putQueryParameter("PhoneNumbers", phone);
// 请求签名
request.putQueryParameter("SignName", signName);
// 请求模板 CODE
request.putQueryParameter("TemplateCode", templateCode);
// 请求验证码
request.putQueryParameter("TemplateParam", "{'code': '" + code + "'}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.1.4 建立 RabbitMQ 监听
package com.springCloud.SPC.listener;
import com.springCloud.SPC.util.SendSms;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
@RabbitListener(queues = "sendPhoneCode")
@Component
public class SPCListener {
private SendSms sendSms;
@Autowired
private SPCListener(SendSms sendSms) {
this.sendSms = sendSms;
}
@RabbitHandler
public void executeSPC(Map<String, String> map) {
System.out.println("手机号: " + map.get("phone"));
System.out.println("验证码: " + map.get("checkCode"));
sendSms.sendSms(map.get("phone"), map.get("checkCode"));
}
}
2.2 User 模板
需要加一个产生随机数的包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
2.2.1 发送手机验证码的代码
/**
* 手机验证码
*
* @param phone 手机
* @return Result
*/
@GetMapping("/send/{phone}")
public Result sendPhoneCode(@PathVariable("phone") String phone) {
userService.sendPhoneCode(phone);
return new Result(StatusCode.OK, true, "发送成功");
}
/**
* 发送验证码
*
* @param phone 手机号
*/
@Override
public void sendPhoneCode(String phone) {
// 生成 6 位随机数
String checkCode = RandomStringUtils.randomNumeric(6);
// 向缓存放一份
redisTemplate.opsForValue().set("checkCode_" + phone, checkCode, 1, TimeUnit.MINUTES);
Map<String, String> map = new HashMap<>();
map.put("phone", phone);
map.put("checkCode", checkCode);
// 给用户放一份
rabbitTemplate.convertAndSend("sendPhoneCode", map);
// 在控制台显示一份
System.out.println("查看验证码:" + checkCode);
}
2.2.2 用户注册得到验证码的代码
/**
* 注册用户
*
* @param code 验证码
* @return Result
*/
@PostMapping("/register/{code}")
public Result register(@PathVariable("code") String code, @RequestBody User user) {
String checkCode_redis = redisTemplate.opsForValue().get("checkCode_" + user.getPhone());
if (checkCode_redis != null) {
if (checkCode_redis.isEmpty()) {
return new Result(StatusCode.ERROR, false, "请先获取验证码");
}
if (!checkCode_redis.equals(code)) {
return new Result(StatusCode.ERROR, false, "请输入正确的验证码");
} else {
// 添加用户
userService.save(user);
return new Result(StatusCode.OK, true, "注册成功");
}
}
return new Result(StatusCode.ERROR, false, "请先获取验证码");
}
3. 阿里云验证码的参数
阿里云的短信验证码是一条 0.045 元.