Method of SpringBoot Integrating Alibaba Cloud SMS Service
- 2021-11-30 00:00:02
- OfStack
Directory 1. Create a new SMS microservice 1. Create sub-modules service-msm under service module
3. Configure application. properties4, create startup class 2. Alibaba Cloud SMS Service 3. Write SMS Sending Interface 1. Introduce dependency in pom of service-msm 2. Write controller, send SMS according to mobile phone number 3. Write service
1. Create a new SMS microservice
1. Create sub-modules service-msm under service module
2. Create controller and service codes
3. Configure application. properties
# Service port
server.port=8006
# Service name
spring.application.name=service-msm
# mysql Database connection
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
# Maximum blocking waiting time ( Negative numbers mean no limit )
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
# Minimum idle
# Return json Global time format of
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# Configure mapper xml The path of the file
mybatis-plus.mapper-locations=classpath:com/atguigu/cmsservice/mapper/xml/*.xml
#mybatis Journal
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4. Create a startup class
@ComponentScan({"com.south"})
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)// Cancel automatic configuration of data source
public class ServiceMsmApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceMsmApplication.class, args);
}
}
2. Alibaba Cloud SMS Service
Help Documentation:
https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73
3. Write an interface to send short messages
1. Introducing dependencies into pom of service-msm
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
2. Write controller and send short messages according to mobile phone number
@CrossOrigin // Cross-domain
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(" Sending SMS failed ");
}
}
}
3. Write service
@Service
public class MsmServiceImpl implements MsmService {
/**
* Send a text message
*/
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", " My Grain Online Education Website ");
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;
}
}