搭建短信微服务
创建子工程
- GroupId:
com.leyou.service
- ArtifactId:
ly-sms
编写pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>com.leyou</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.service</groupId>
<artifactId>ly-sms</artifactId>
<dependencies>
<dependency>
<groupId>com.leyou.common</groupId>
<artifactId>ly-common</artifactId>
<version>${leyou.latest.version}</version>
</dependency>
<!-- 阿里云通信-SMS -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写application.yml
server:
port: 8005
spring:
application:
name: sms-service
rabbitmq:
# 主机地址
host: 192.168.136.103
# 用户名、密码
username: leyou
password: leyou
# 虚拟主机
virtual-host: /leyou
ly:
sms:
# 下面这些参数阿里云通信的一些参数
accessKeyID: xxxxxxxxxxxxxx
accessKeySecret: xxxxxxxxxxxxxx
signName: xx
verifyCodeTemplate: xxxxxxxxx
编写启动类
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmsService {
public static void main(String[] args) {
SpringApplication.run(SmsService.class, args);
}
}
短信发送
参考阿里云文档
Java SDK安装
OpenAPI获取代码
SMS配置类
package com.leyou.sms.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "ly.sms")
public class SmsProperties {
private String accessKeyID;
private String accessKeySecret;
private String signName;
private String verifyCodeTemplate;
}
SMS短信发送工具类
package com.leyou.sms.util;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsUtil {
@Autowired
private SmsProperties smsProperties;
/**
* 发送短信
*
* @param phoneNumbers 收信人号码
* @param signName 签名
* @param templateCode 模板code
* @param templateParam 发送的数据(JSON格式)
*/
public CommonResponse sendSms(String phoneNumbers, String signName,
String templateCode, String templateParam) {
try {
DefaultProfile profile = DefaultProfile
.getProfile("default", smsProperties.getAccessKeyID(), smsProperties.getAccessKeySecret());
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("PhoneNumbers", phoneNumbers);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", templateParam);
CommonResponse response = client.getCommonResponse(request);
log.info("[短信微服务] 短信发送。发送结果:{}", response.getData());
return response;
} catch (ClientException e) {
log.error("[短信微服务] 发送短信失败, phone = [{}]", phoneNumbers, e);
}
return null;
}
}
接收发送短信消息
package com.leyou.sms.listener;
import com.leyou.common.util.JsonUtils;
import com.leyou.sms.config.SmsProperties;
import com.leyou.sms.util.SmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Map;
@Slf4j
@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsListener {
@Autowired
private SmsUtil smsUtil;
@Autowired
private SmsProperties smsProperties;
/**
* 短信验证码
*
* @param msg 数据
*/
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "sms.verify.code.queue", durable = "true"),
exchange = @Exchange(name = "ly.sms.exchange"),
key = "sms.verify.code"
))
public void verifyCode(Map<String, String> msg) {
if (CollectionUtils.isEmpty(msg)) {
return;
}
String phoneNumber = msg.remove("phoneNumber");
if (StringUtils.isBlank(phoneNumber)) {
return;
}
// 发送短信
smsUtil.sendSms(phoneNumber, smsProperties.getSignName(), smsProperties.getVerifyCodeTemplate(), JsonUtils.serialize(msg));
}
}
测试
package com.leyou.sms.util;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SmsUtilTest {
@Autowired
private AmqpTemplate amqpTemplate;
@Test
public void sendSms() {
Map<String, String> msg = new HashMap<>();
msg.put("phoneNumber", "${输入手机号替换此内容}");
msg.put("code", "456789");
amqpTemplate.convertAndSend("ly.sms.exchange", "sms.verify.code", msg);
}
}
测试前,需要在
pom
文件中加入test
依赖。