# 短信网关

针对阿里云、华为云、腾讯云和我们的用户中心colibri,为了使上层开发与具体应用解耦,所以提供SDK适配各个云厂商,供上层服务调用。可以通过简单的切换配置文件,达到切换不同厂商的功能。

# 功能介绍

sms-core 是为了适配多云的短信服务而开发的 SDK,用于支持产品在多个云的环境部署运行。产品可以通过简单的修改配置,不用修改代码就能完成在多云之间的平滑部署迁移。

  • 适配了阿里云、 华为云、腾讯云以及我们的用户中心的短信服务colibri。
  • 当前版本支持短信发送一个接口。

基于sms-core提供的便捷,为了在使用上可以更加方便,我们提供了sms-spring-boot-starter,其内部是基于sms-core的实现,为上层提供了自动配置的功能,也就是starter包。

# 使用流程

# 1.配置settings.xml私仓地址

<!-- scg maven仓库 -->
<repository>
    <id>scg-private</id>
    <name>maven-scg-private</name>
    <url>http://packages.glodon.com/artifactory/maven-scg-private/</url>
</repository>
1
2
3
4
5
6

# 2.项目中添加 pom 依赖

<dependency>
    <groupId>com.glodon.cloud</groupId>
    <artifactId>sms-spring-boot-starter</artifactId>
    <version>1.0.1</version>
</dependency>
1
2
3
4
5

如果结合spring-cloud-glodon使用,则通过引入spring-cloud-glodon-dependencies后管理依赖版本即可

<dependency>
    <groupId>com.glodon.cloud</groupId>
    <artifactId>sms-spring-boot-starter</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.glodon.cloud</groupId>
            <artifactId>spring-cloud-glodon-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 3.配置yaml

glodon:
  sms:
    clientType: ali # huawei/tencent/colibri
    accessKey: xxxxxx
    accessSecret: xxxxxx
    templateName: xxxxxx
    # smsSdkAppid: xxxxxx # 腾讯云需要配置该参数
    # channelId: xxxxxx   # 华为云需要配置该参数
1
2
3
4
5
6
7
8

# 4.注入对象

配置完成后,就可以在代码中注入smsClient来使用。

@Resource
private SmsClient smsClient;
1
2

# 5.调用发送短信接口

// 阿里云平台短信服务示例

// 模板id
String templateCode = "SMS_187940161";
// 模板对应的请求参数
List<Pair<String, String>> requestParams = new ArrayList<Pair<String, String>>() {{
    add(new Pair<>("member", "小明"));
    add(new Pair<>("membername", "小明"));
    add(new Pair<>("phonenum", "18210449300"));
    add(new Pair<>("ExhibitionName", "浏览"));
    add(new Pair<>("time", "2020-7-31"));
    add(new Pair<>("num", "5"));
}};
// 下发号码
Set<String> phoneNumbers = new HashSet<String>() {{
    add("18210449300");
}};
// 构建发送短信消息体,阿里云只需要请求参数和下发号码
SendSmsRequest sendSmsRequest = new SendSmsRequest.Builder()
                    .templateId(templateCode)
                    .templateParam(requestParams)
                    .phoneNumbers(phoneNumbers)
                    .build();
// 调用发送接口,获取调用结果
SendSmsResponse sendSmsResponse = smsClient.sendSms(templateCode, sendSmsRequest);
System.out.println(sendSmsResponse.toString());

if ("SUCCESS".equals(sendSmsResponse.getCode())) {
    // 表示调用成功
    System.out.println("sendSms接口调用成功");
} else {
    // 表示调用失败
    System.out.println("sendSms接口调用失败,错误码:" + sendSmsResponse.getCode() + ", 描述信息:" + sendSmsResponse.getMessage());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 腾讯云平台短信服务示例

// 模板id
String templateIdWithParams = "677745"; // 您本次的验证码为{1},有效期{2}分钟请妥善保管。

// 模板对应的请求参数 
// 为了做多平台适配,此处使用 List<Pair<String, String>> 数据结构来存放模板参数。在类似腾讯云这种类型的参数时,需要通过key为null的方式进行参数填充
List<Pair<String, String>> requestParams = new ArrayList<Pair<String, String>>() {{
    add(new Pair<>(null, "136954"));
    add(new Pair<>(null, "5"));
}};
// 下发号码
Set<String> phoneNumbers = new HashSet<String>() {{
    add("18210449300");
}};
SendSmsRequest sendSmsRequest = new SendSmsRequest.Builder()
    .templateId(templateIdWithParams)
    .templateParam(requestParams)
    .phoneNumbers(phoneNumbers)
    .build();
System.out.println(sendSmsRequest.toString());
SendSmsResponse sendSmsResponse = tencentSmsClient.sendSms(sendSmsRequest);
System.out.println(sendSmsResponse.toString());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  • 在线客服

  • 意见反馈