JAVA全系列 教程
3762个小节阅读:7090.4k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建名为shopping_message_service
的SpringBoot工程,添加相关依赖。
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 阿里短信平台 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.9</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- 操作zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
设置该工程的父工程为shopping
。
xxxxxxxxxx
<parent>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
给shopping
工程设置子模块
xxxxxxxxxx
<!-- 子模块 -->
<modules>
<!-- 短信服务 -->
<module>shopping_message_service</module>
</modules>
编写配置文件application.yml
xxxxxxxxxx
# 端口号
server:
port: 9007
# 日志格式
logging:
pattern:
console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
dubbo:
application:
name: shopping_message_service # 项目名
registry:
address: zookeeper://192.168.25.100 # 注册中心地址
port: 2181 # 注册中心的端口
timeout: 10000 # 注册到zk上超时时间,ms
protocol:
name: dubbo # dubbo使用的协议
port: -1 # dubbo自动分配端口
scan:
base-packages: com.itbaizhan.shopping_message_service.service # 包扫描
message:
accessKeyId: LTAI5tBUnRMTgKmRR92yxrFf
accessKeySecret: RN7umfy3mMA2xlGa8rP0IzwQdTc6Pb
启动类忽略数据源自动配置
xxxxxxxxxx
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class ShoppingMessageServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingMessageServiceApplication.class, args);
}
}
在通用模块编写发送短信服务接口
xxxxxxxxxx
/**
* 短信服务
*/
public interface MessageService {
/**
* 发送短信
* @param phoneNumber 手机号
* @param code 验证码
* @return 返回结果
*/
BaseResult sendMessage(String phoneNumber,String code);
}
在短信服务模块编写发送短信实现类
xxxxxxxxxx
@DubboService
public class MessageServiceImpl implements MessageService {
@Value("${message.accessKeyId}")
private String accessKeyId;
@Value("${message.accessKeySecret}")
private String accessKeySecret;
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
@SneakyThrows
private Client createClient(String accessKeyId, String accessKeySecret) {
Config config = new Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
@SneakyThrows
@Override
public BaseResult sendMessage(String phoneNumber,String code) {
Client client = createClient(accessKeyId, accessKeySecret);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers(phoneNumber)
.setTemplateParam("{\"code\":\""+code+"\"}");
RuntimeOptions runtime = new RuntimeOptions();
// 复制代码运行请自行打印API的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
SendSmsResponseBody body = sendSmsResponse.getBody();
if ("OK".equals(body.getCode())){
return new BaseResult(200,body.getCode(),body.getMessage());
}else{
return new BaseResult(500,body.getCode(),body.getMessage());
}
}
}
测试该方法