SpringCloud-Alibaba-SMS
SpringCloud-Alibaba-SMS
需要准备的东西
开始短信服务
登录阿里云账号->开启 SMS 服务
获取阿里云访问密钥
为了使用短信发送API-JAVA SDK,您必须申请阿里云的访问密钥。
阿里云访问秘钥是阿里云为用户使用 API(非控制台)来访问其云资源设计的“安全口令”。您可以用它来签名 API 请求内容以通过服务端的安全验证。
该访问秘钥成对(AccessKeyId 与 AccessKeySecret)生成和使用。每个阿里云用户可以创建多对访问秘钥,且可随时启用(Active)、禁用(Inactive)或者删除已经生成的访问秘钥对。
您可以通过阿里云控制台的秘钥管理页面创建、管理所有的访问秘钥对,且保证它处于“启用”状态。由于访问秘钥是阿里云对 API 请求进行安全验证的关键因子,请妥善保管你的访问秘钥。如果某些秘钥对出现泄漏风险,建议及时删除该秘钥对并生成新的替代秘钥对。
更多关于短信的信息,请参考短信官方短信(SendSms)—JAVA]文档。
模板与签名的申请
模板与签名是调用接口必备的参数
短信签名
根据用户属性来创建符合自身属性的签名信息。企业用户需要上传相关企业资质证明,个人用户需要上传证明个人身份的证明。
注意 短信签名需要审核通过后才可以使用。(创建签名限制很多自己慢慢试试)
短信模板
短信模板,即具体发送的短信内容。
短信模板可以支持验证码、短信通知、推广短信、国际/港澳台消息四种模式。验证码和短信通知,通过变量替换实现个性短信定制。推广短信不支持在模板中添加变量。国际/港澳台消息只能使用国际/港澳台短信模版发送短信。
说明 短信模板需要审核通过后才可以使用。 (创建模板限制很多自己慢慢试试)
为了成功发送一条短信通知,您至少需要完成以下步骤
- 在控制台完成短信签名与短信模板的申请,获得调用接口必备的参数。
- 在“短信签名”页面完成签名的申请,获得短信签名的字符串。签名申请手册。
- 在“短信模板”页面完成模板的申请,获得模板ID。模板申请手册。
开启状态报告接收和上行消息接收
进入SMS 服务 控制台->左侧通用设置
队列名称我们后面需要用到
短信参数参考
入参列表
参数名称 | 参数类型 | 必填与否 | 样例取值 | 参数说明 |
---|---|---|---|---|
PhoneNumbers | String | 必须 | 1500000**** | 短信接收号码,支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为:国际区号+号码,如“8520000****”。 |
SignName | String | 必须 | 云通信 | 短信签名。 |
TemplateCode | String | 必须 | SMS_0000 | 短信模板ID,发送国际/港澳台消息时,请使用国际/港澳台短信模版。 |
TemplateParam | String | 可选 | {“code”:“1234”,“product”:“ytx”} | 短信模板变量替换JSON串,友情提示:如果JSON中需要带换行符,请参照标准的JSON协议。 |
SmsUpExtendCode | String | 可选 | 90999 | 上行短信扩展码,无特殊需要此字段的用户请忽略此字段。 |
OutId | String | 可选 | abcdefgh | 外部流水扩展字段。 |
出参列表
出参名称 | 出参类型 | 样例取值 | 参数说明 |
---|---|---|---|
RequestId | String | 8906582E-6722 | 请求ID。 |
Code | String | OK | 状态码。返回OK代表请求成功,其他错误码详见错误码列表。 |
Message | String | 请求成功 | 状态码的描述。 |
BizId | String | 134523^4351232 | 发送回执ID,可根据该ID查询具体的发送状态 |
错误码列表
Code | 描述 |
---|---|
OK | 请求成功。 |
isp.RAM_PERMISSION_DENY | RAM权限DENY。 |
isv.OUT_OF_SERVICE | 业务停机。 |
isv.PRODUCT_UN_SUBSCRIPT | 未开通云通信产品的阿里云客户。 |
isv.PRODUCT_UNSUBSCRIBE | 产品未开通。 |
isv.ACCOUNT_NOT_EXISTS | 账户不存在。 |
isv.ACCOUNT_ABNORMAL | 账户异常。 |
isv.SMS_TEMPLATE_ILLEGAL | 短信模板不合法。 |
isv.SMS_SIGNATURE_ILLEGAL | 短信签名不合法。 |
isv.INVALID_PARAMETERS | 参数异常。 |
isp.SYSTEM_ERROR | 系统错误。 |
isv.MOBILE_NUMBER_ILLEGAL | 非法手机号。 |
isv.MOBILE_COUNT_OVER_LIMIT | 手机号码数量超过限制。 |
isv.TEMPLATE_MISSING_PARAMETERS | 模板缺少变量。 |
isv.BUSINESS_LIMIT_CONTROL | 业务限流。 |
isv.INVALID_JSON_PARAM | JSON参数不合法,只接受字符串值。 |
isv.BLACK_KEY_CONTROL_LIMIT | 黑名单管控。 |
isv.PARAM_LENGTH_LIMIT | 参数超出长度限制。 |
isv.PARAM_NOT_SUPPORT_URL | 不支持URL。 |
isv.AMOUNT_NOT_ENOUGH | 账户余额不足。 |
说明 查询所有错误码及解决办法,请参见短信接口调用错误码
开始Alibab-SMS项目
项目结构
需要的Maven
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring.cloud.alibaba.version>2.2.3.RELEASE</spring.cloud.alibaba.version></properties><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring.cloud.alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- Springboot 版本要高于 spring-cloud-alibaba 否则报错--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.2.RELEASE</version></parent><dependencies><!-- spring-cloud --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR9</version><type>pom</type><scope>import</scope></dependency><!-- web服务--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.4.1</version></dependency><!-- springboot test启动组件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.1.1.RELEASE</version><scope>test</scope></dependency><!-- 将服务注册到Nacos里--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- alicloud-sms 短信服务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alicloud-sms</artifactId><version>2.2.0.RELEASE</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
application.yml
server:port: 30101spring:application:name: nacos-smscloud:nacos:discovery:server-addr: 192.168.81.100:80 #注册中心地址alicloud:# 阿里云访问密钥access-key: LTAI4Fw1zMqtdNBfFN174aA5secret-key: wZW5dDTUa2f29eUuWsnR4VqtmVbjd1sms:#这个是在阿里云短信服务->通用设置里开启->状态报告接收->返回的队列名称report-queue-name: Alicom-Queue-1101184799557765-SmsReport#这个是在阿里云短信服务->通用设置里开启->上行消息接收->返回的队列名称up-queue-name: Alicom-Queue-1101184799557765-SmsUp
SmsApplication
package com.sms;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class SmsApplication {public static void main(String[] args) {SpringApplication.run(SmsApplication.class, args);}}
SmsController
package com.sms.controller;import com.alibaba.alicloud.sms.ISmsService;
import com.aliyun.mns.common.ClientException;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import com.aliyuncs.http.MethodType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class SmsController {@Autowiredprivate ISmsService smsService;//单发短信// http://localhost:30101/sms?code=123@GetMapping("/sms")public SendSmsResponse batchsendCheckCode(@RequestParam(name = "code") String code) {SendSmsRequest request = new SendSmsRequest();// Required:the mobile number 接收短信的号码request.setPhoneNumbers("18155756151");// Required:SMS-SignName-could be found in sms console 签名request.setSignName("回头是岸");// Required:Template-could be found in sms console 模板request.setTemplateCode("SMS_187742421");// Required:The param of sms template.For exmaple, if the template is "Hello,your verification code is ${code}". The param should be like following valuerequest.setTemplateParam("{\"code\":\"" + code + "\"}");SendSmsResponse sendSmsResponse = null;try {sendSmsResponse = smsService.sendSmsRequest(request);}catch (ClientException e) {e.printStackTrace();sendSmsResponse = new SendSmsResponse();} catch (com.aliyuncs.exceptions.ClientException e) {e.printStackTrace();}return sendSmsResponse ;}//批量短信发送// http://localhost:30101/sms_s?code=123@GetMapping("/sms_s")public SendBatchSmsResponse batchsendCheckCodes(@RequestParam(name = "code") String code) {SendBatchSmsRequest request = new SendBatchSmsRequest();request.setMethod(MethodType.GET);//必须为getrequest.setPhoneNumberJson("[\"18155756151\",\"17554271552\"]"); //接收短信的号码request.setSignNameJson("[\"回头是岸\",\"回头是岸\"]"); //签名request.setTemplateCode("SMS_187742421");//使用模板request.setTemplateParamJson("[{\"code\":\"" + code + "\"},{\"code\":\"" + code + "\"}]");SendBatchSmsResponse sendSmsResponse = null;try {sendSmsResponse = smsService.sendSmsBatchRequest(request);return sendSmsResponse;}catch (ClientException e) {e.printStackTrace();sendSmsResponse = new SendBatchSmsResponse();} catch (com.aliyuncs.exceptions.ClientException e) {e.printStackTrace();}return sendSmsResponse ;}//指定时间的,最近30天短信发送记录查询// http://localhost:30101/querySms?tel=18155756151&sendDate=20210626&page=1@GetMapping("/querySms")public QuerySendDetailsResponse querySendDetailsResponse(@RequestParam(name = "tel") String telephone,@RequestParam(name = "sendDate") String sendDate,@RequestParam(value = "page",required = false,defaultValue = "1") Long page) {QuerySendDetailsRequest request = new QuerySendDetailsRequest();request.setPhoneNumber(telephone); //查询指定电话号码//支持查询指定时间最近30天的记录。 比如: 我指定查询 2021626 那么会自动查询2021526~2021626之间的全部短信request.setSendDate(sendDate);//获取最近30天收到的短信request.setPageSize(50L); // (阿里云规定的每页最多每页显示50条 )request.setCurrentPage(page); //当前页码。try {QuerySendDetailsResponse response = smsService.querySendDetails(request);return response;}catch (ClientException | com.aliyuncs.exceptions.ClientException e) {e.printStackTrace();}return new QuerySendDetailsResponse();}}
注意: 把上面的电话,签名,模板 都换成自己的
SmsReportMessageListener1
package com.sms.listener;import com.alibaba.alicloud.sms.SmsReportMessageListener;
import com.aliyun.mns.model.Message;
import org.springframework.stereotype.Component;/*** 通过订阅SmsReport短信状态报告,可以获知每条短信的发送情况,了解短信是否达到终端用户的状态与相关信息* @author Administrator*/
@Component
public class SmsReportMessageListener1 implements SmsReportMessageListener {@Overridepublic boolean dealMessage(Message message) {//do somethingSystem.err.println("短信发送成功内容回执: "+this.getClass().getName() + "; " + message.toString());return true;}}
SmsUpMessageListener1
package com.sms.listener;import com.alibaba.alicloud.sms.SmsUpMessageListener;
import com.aliyun.mns.model.Message;
import org.springframework.stereotype.Component;//通过订阅SmsUp上游短信,您可以了解终端用户回复短信的内容
@Component
public class SmsUpMessageListener1 implements SmsUpMessageListener {@Overridepublic boolean dealMessage(Message message) {//do somethingSystem.err.println("用户回复内容: "+this.getClass().getName() + "; " + message.toString());return true;}
}
测试
自己访问
GIT http://localhost:30101/sms?code=123
{"requestId":"90AB09A1-2F9F-4A68-B837-5C62BA7F86EA","bizId":"489620624764330349^0","code":"OK","message":"OK"}
GIT http://localhost:30101/sms_s?code=123
{"requestId":"1DDEF4E3-F441-4279-9E10-8F6734CF0C8B","bizId":"730510724764349700^0","code":"OK","message":"OK"}
GIT http://localhost:30101/querySms?tel=18155756151&sendDate=20210626&page=1
{"requestId":"90EB0578-63A9-4205-A56B-FF989D92605F","code":"OK","message":"OK","totalCount":"6","smsSendDetailDTOs":[{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码123,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 17:37:10","receiveDate":"2021-06-26 17:37:17","outId":null},{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码123,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 17:28:52","receiveDate":"2021-06-26 17:28:58","outId":null},{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码123,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 17:24:26","receiveDate":"2021-06-26 17:24:32","outId":null},{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码123,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 16:32:42","receiveDate":"2021-06-26 16:32:49","outId":null},{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码gou_bi,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 16:05:14","receiveDate":"2021-06-26 16:05:20","outId":null},{"phoneNum":"18155756151","sendStatus":3,"errCode":"DELIVERED","templateCode":"SMS_187742421","content":"【回头是岸】您的验证码123,该验证码5分钟内有效,请勿泄漏于他人!","sendDate":"2021-06-26 16:04:09","receiveDate":"2021-06-26 16:04:15","outId":null}]}
把电话换成你可以接收短信的号码
点赞 -收藏加 -关注 便于以后复习和收到最新内容 有其他问题在评论区讨论-或者私信我-收到会在第一时间回复 感谢,配合,希望我的努力对你有帮助^_^
SpringCloud-Alibaba-SMS相关推荐
- SpringCloud Alibaba微服务实战(一) - 基础环境搭建
说在前面 Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来 ...
- SpringCloud—07—高级之SpringCloud Alibaba上
文章目录 提前预知 17.SpringCloud Alibaba入门简介 17.1.为什么会出现SpringCloud alibaba 18.Spring Cloud Alibaba Nacos服务注 ...
- 【springcloud alibaba】 一条龙服务实现微服务案例
第一章 微服务介绍 1.1 系统架构演变 1.1.1 SpringCloud Spring Cloud是一系列框架的集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发, ...
- SpringCloud Alibaba 详解
SpringCloud Alibaba 基础详解 SpringCloud Alibaba 概念 SpringCloud Alibaba配置及使用 SpringCloud Alibaba Nacos服务 ...
- SpringCloud Alibaba入门
1.springcloud alibaba 1.1主要功能 服务限流降级:默认支持WebService.WebFlux.OpenFeign.RestTemplate.SpringCloudGatewa ...
- SpringCloud Alibaba微服务实战(七) - 路由网关(Gateway)全局过滤
说在前面 全局过滤器作用于所有的路由,不需要单独配置,我们可以用它来实现很多统一化处理的业务需求,比如权限认证,IP 访问限制,监控,限流等等. 创建路由网关(Gateway)启动服务cloud-ac ...
- SpringCloud Alibaba微服务实战(五) - Sentinel实现限流熔断
什么是Sentinel? 请查看文章:SpringCloud Alibaba微服务实战(一) - 基础环境搭建 构建服务消费者cloud-sentinel进行服务调用 服务创建请查看文章:Spring ...
- SpringCloud Alibaba微服务实战(四) - Nacos Config 配置中心
说在前面 Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.Nacos Config就是一个类似于SpringCloud Config的配置中心. 一.启动N ...
- SpringCloud Alibaba微服务实战(三) - Nacos服务创建消费者(Feign)
什么是Feign Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 ...
- SpringCloud Alibaba微服务实战(二) - Nacos服务注册与restTemplate消费
说在前面 基础环境搭建,理论,请看上一篇,在这就不扯理论了,直接上代码. 项目结构 代码实现 第一步:在父pom的项目中引入dependencyManagement 在引入父pom之前咱们先来回顾下d ...
最新文章
- windows自动关闭无响应程序
- 在eclipse中运行工程时 出现出现ConnectionProperties 的解决方法
- LeetCode 3:无重复字符的最长子串 思考分析
- oem是代工还是贴牌_代加工和贴牌加工的区别是什么
- 数据结构树4-二叉搜索树2
- 选择适合 Rails 开发的操作系统
- 常见的php 字符串函数,php 常用字符串函数总结
- Spring中HibernateCallback的用法(转)
- linux全局启动tomcat,linux下启动tomcat服务
- [AE 表达式]路径跟随动画
- 面向对象(OOP)概念理解,通俗易懂
- 服务器显示无vga电缆怎么回事,无vga电缆是什么意思
- 网页游戏是如何快速推广,寻找真实玩家。提高充值消费的。这里都有教程
- 现实黑镜 | 面对死亡,你愿意将意识上传 获得“永生”吗?
- 世界人工智能大会倒计时30天,这些亮点不容错过
- php如何实现自动加载mp3,PHP中自动加载的几种实现
- 微信小程序入门(1):简单界面的实现
- Android 进阶17:Fragment FragmentManager FragmentTransaction 深入理解
- [ZCMU OJ]5252: 英雄卡/5253: 排队接水/5254: 颜色叠加/5255: 勤劳的老杨/5256: 秘密大厦的访客
- 原型工具axure7.0 正式版免费下载