一、新建短信微服務
1、在service模塊下創建子模塊service-msm
2.創建controller和service代碼
3.配置application.properties
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
|
# 服務端口 server.port= 8006 # 服務名 spring.application.name=service-msm # mysql數據庫連接 spring.datasource.driver- class -name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql: //localhost:3306/guli?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.redis.host= 192.168 . 44.131 spring.redis.port= 6379 spring.redis.database= 0 spring.redis.timeout= 1800000 spring.redis.lettuce.pool.max-active= 20 spring.redis.lettuce.pool.max-wait=- 1 #最大阻塞等待時間(負數表示沒限制) spring.redis.lettuce.pool.max-idle= 5 spring.redis.lettuce.pool.min-idle= 0 #最小空閑 #返回json的全局時間格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+ 8 #配置mapper xml文件的路徑 mybatis-plus.mapper-locations=classpath:com/atguigu/cmsservice/mapper/xml/*.xml #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl |
4、創建啟動類
1
2
3
4
5
6
7
|
@ComponentScan ({ "com.south" }) @SpringBootApplication (exclude = DataSourceAutoConfiguration. class ) //取消數據源自動配置 public class ServiceMsmApplication { public static void main(String[] args) { SpringApplication.run(ServiceMsmApplication. class , args); } } |
二、阿里云短信服務
幫助文檔:
https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73
三、編寫發送短信接口
1.在service-msm的pom中引入依賴
1
2
3
4
5
6
7
8
9
10
|
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> </dependency> </dependencies> |
2.編寫controller,根據手機號發送短信
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
|
@CrossOrigin //跨域 public class MsmApiController { @Autowired private MsmService msmService; @Autowired private RedisTemplate<String, String> redisTemplate; @GetMapping (value = "/send/{phone}" ) public R code( @PathVariable String phone) { String code = redisTemplate.opsForValue().get(phone); if (!StringUtils.isEmpty(code)) return R.ok(); code = RandomUtil.getFourBitRandom(); Map<String,Object> param = new HashMap<>(); param.put( "code" , code); boolean isSend = msmService.send(phone, "SMS_180051135" , param); if (isSend) { redisTemplate.opsForValue().set(phone, code, 5 ,TimeUnit.MINUTES); return R.ok(); } else { return R.error().message( "發送短信失敗" ); } } } |
3.編寫service
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
35
36
37
38
|
@Service public class MsmServiceImpl implements MsmService { /** * 發送短信 */ public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) { if (StringUtils.isEmpty(PhoneNumbers)) return false ; DefaultProfile profile = DefaultProfile.getProfile( "default" , "LTAIq6nIPY09VROj" , "FQ7UcixT9wEqMv9F35nORPqKr8XkTF" ); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); //request.setProtocol(ProtocolType.HTTPS); request.setMethod(MethodType.POST); request.setDomain( "dysmsapi.aliyuncs.com" ); request.setVersion( "2017-05-25" ); request.setAction( "SendSms" ); request.putQueryParameter( "PhoneNumbers" , PhoneNumbers); request.putQueryParameter( "SignName" , "我的谷粒在線教育網站" ); request.putQueryParameter( "TemplateCode" , templateCode); request.putQueryParameter( "TemplateParam" , JSONObject.toJSONString(param)); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); return response.getHttpResponse().isSuccess(); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return false ; } } |
到此這篇關于SpringBoot整合阿里云短信服務的文章就介紹到這了,更多相關SpringBoot阿里云短信服務內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_43118617/article/details/120898762