JAVA全系列 教程
3762个小节阅读:7091.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在通用模块编写发送短信服务接口
xxxxxxxxxx
/**
* 短信服务
*/
public interface MessageService {
/**
* 发送短信
* @param phoneNumber 手机号
* @param code 验证码
* @return 返回结果
*/
BaseResult sendMessage(String phoneNumber,String code);
}
在短信服务模块编写发送短信实现类
xxxxxxxxxx
@DubboService
@Service
public class MessageServiceImpl implements MessageService {
@Value("${message.accessKeyId}")
private String accessKeyId;
@Value("${message.accessKeySecret}")
private String accessKeySecret;
@SneakyThrows
@Override
public BaseResult sendMessage(String phoneNumber, String code) {
// Configure Credentials authentication information, including ak, secret, token
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
// Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
.accessKeyId(accessKeyId)
.accessKeySecret(accessKeySecret)
//.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
.build());
// Configure the Client
AsyncClient client = AsyncClient.builder()
.region("cn-hangzhou") // Region ID
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
.credentialsProvider(provider)
//.serviceConfiguration(Configuration.create()) // Service-level configuration
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
.overrideConfiguration(
ClientOverrideConfiguration.create()
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
.setEndpointOverride("dysmsapi.aliyuncs.com")
//.setConnectTimeout(Duration.ofSeconds(30))
)
.build();
// Parameter settings for API request
SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
.signName("阿里云短信测试")
.templateCode("SMS_154950909")
.phoneNumbers(phoneNumber)
.templateParam("{\"code\":\"" + code + "\"}")
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
// Asynchronously get the return value of the API request
CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
// Synchronously get the return value of the API request
SendSmsResponse resp = response.get();
// Finally, close the client
client.close();
SendSmsResponseBody respBody = resp.getBody();
if ("OK".equals(respBody.getCode())) {
return new BaseResult(200, respBody.getCode(), respBody.getMessage());
} else {
return new BaseResult(500, respBody.getCode(), respBody.getMessage());
}
}
}
测试该方法