1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

ans starter suport eureka/consule registry and add sms module

This commit is contained in:
得少
2019-01-09 18:43:23 +08:00
parent d7fea8dfc3
commit 90b21480d7
62 changed files with 3292 additions and 100 deletions

View File

@@ -28,6 +28,7 @@
<module>ans-example/ans-provider-example</module>
<module>acm-example/acm-local-example</module>
<module>rocketmq-example</module>
<module>sms-example</module>
<module>spring-cloud-bus-rocketmq-example</module>
<module>schedulerx-example/schedulerx-simple-task-example</module>
</modules>

View File

@@ -0,0 +1,51 @@
<?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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>0.1.2.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>alibaba.com</groupId>
<artifactId>sms-example</artifactId>
<packaging>jar</packaging>
<name>sms-example</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<!--Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-sms</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,13 @@
package org.springframework.cloud.alibaba.cloud.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class, args);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alibaba.cloud.example;
/**
* @author pbting
* @date 2019-01-09 2:12 PM
*/
public class SmsCode {
private String code;
public SmsCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}

View File

@@ -0,0 +1,133 @@
package org.springframework.cloud.alibaba.cloud.example;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alicloud.sms.ISmsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.aliyun.mns.model.Message;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.google.gson.Gson;
@RestController
public class SmsController {
@Autowired
private ISmsService smsService;
@Autowired
private SmsReportMessageListener smsReportMessageListener;
/**
* 短信发送 Example
* @param code
* @return
*/
@RequestMapping("/batch-sms-send.do")
public SendBatchSmsResponse batchsendCheckCode(
@RequestParam(name = "code") String code) {
// 组装请求对象
SendBatchSmsRequest request = new SendBatchSmsRequest();
// 使用post提交
request.setMethod(MethodType.GET);
// 必填:待发送手机号。支持JSON格式的批量调用批量上限为100个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumberJson("[\"****\",\"****\",\"****\"]");
// 必填:短信签名-支持不同的号码发送不同的短信签名
request.setSignNameJson("[\"企业级分布式应用服务\",\"企业级分布式应用服务\",\"企业级分布式应用服务\"]");
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode("*****");
// 必填:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
// 友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
List<SmsCode> smsCodes = new ArrayList<>();
smsCodes.add(new SmsCode(code));
smsCodes.add(new SmsCode(code));
smsCodes.add(new SmsCode(code));
request.setTemplateParamJson(new Gson().toJson(smsCodes));
// 可选-上行短信扩展码(扩展码字段控制在7位或以下无特殊需求用户请忽略此字段)
// request.setSmsUpExtendCodeJson("[\"90997\",\"90998\"]");
try {
SendBatchSmsResponse sendSmsResponse = smsService
.sendSmsBatchRequest(request);
return sendSmsResponse;
}
catch (ClientException e) {
e.printStackTrace();
}
return new SendBatchSmsResponse();
}
/**
* 短信发送 Example
* @param code
* @return
*/
@RequestMapping("/sms-send.do")
public SendSmsResponse sendCheckCode(@RequestParam(name = "code") String code) {
// 组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
// 必填:待发送手机号
request.setPhoneNumbers("****");
// 必填:短信签名-可在短信控制台中找到
request.setSignName("企业级分布式应用服务");
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode("*****");
// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam("{\"code\":\"" + code + "\"}");
// 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
// request.setSmsUpExtendCode("90997");
// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("****TraceId");
try {
SendSmsResponse sendSmsResponse = smsService.sendSmsRequest(request);
return sendSmsResponse;
}
catch (ClientException e) {
e.printStackTrace();
}
return new SendSmsResponse();
}
/**
*
* 短信查询 Example
* @param telephone
* @return
*/
@RequestMapping("/query.do")
public QuerySendDetailsResponse querySendDetailsResponse(
@RequestParam(name = "tel") String telephone) {
// 组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
// 必填-号码
request.setPhoneNumber(telephone);
// 必填-短信发送的日期 支持30天内记录查询可查其中一天的发送数据格式yyyyMMdd
request.setSendDate("20190103");
// 必填-页大小
request.setPageSize(10L);
// 必填-当前页码从1开始计数
request.setCurrentPage(1L);
try {
QuerySendDetailsResponse response = smsService.querySendDetails(request);
return response;
}
catch (ClientException e) {
e.printStackTrace();
}
return new QuerySendDetailsResponse();
}
@RequestMapping("/sms-report.do")
public List<Message> smsReport() {
return smsReportMessageListener.getSmsReportMessageSet();
}
}

View File

@@ -0,0 +1,29 @@
package org.springframework.cloud.alibaba.cloud.example;
import com.aliyun.mns.model.Message;
import org.springframework.stereotype.Component;
import java.util.LinkedList;
import java.util.List;
/***
*
* @author 如果需要监听短信是否被对方成功接收,只需实现这个接口并初始化一个 Spring Bean 即可。
*/
@Component
public class SmsReportMessageListener
implements org.springframework.cloud.alicloud.sms.SmsReportMessageListener {
private List<Message> smsReportMessageSet = new LinkedList<>();
@Override
public boolean dealMessage(Message message) {
smsReportMessageSet.add(message);
System.err.println(this.getClass().getName() + "; " + message.toString());
return true;
}
public List<Message> getSmsReportMessageSet() {
return smsReportMessageSet;
}
}

View File

@@ -0,0 +1,20 @@
package org.springframework.cloud.alibaba.cloud.example;
import org.springframework.stereotype.Component;
import com.aliyun.mns.model.Message;
/***
*
* @author 如果发送的短信需要接收对方回复的状态消息,只需实现该接口并初始化一个 Spring Bean 即可。
*/
@Component
public class SmsUpMessageListener
implements org.springframework.cloud.alicloud.sms.SmsUpMessageListener {
@Override
public boolean dealMessage(Message message) {
System.err.println(this.getClass().getName() + "; " + message.toString());
return true;
}
}

View File

@@ -0,0 +1,12 @@
spring.application.name=sca-sms-example
server.port=9051
# config management
#config sms
spring.cloud.alicloud.access-key=*****
spring.cloud.alicloud.secret-key=******
spring.cloud.alicloud.sms.report-queue-name=*****
spring.cloud.alicloud.sms.up-queue-name=*****
#config endpoint
management.port=19391
management.context-path=/test
management.security.enabled=false