1. 添加阿里大于SDK依赖
在pom.xml
文件中添加以下依赖:
<!-- 阿里大于SDK -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.3.2</version>
</dependency>
2. 配置阿里大于短信服务
在application.properties
文件中添加以下配置:
# 阿里大于短信服务配置
aliyun.sms.accessKeyId=your_accessKeyId
aliyun.sms.accessKeySecret=your_accessKeySecret
aliyun.sms.signName=your_signName
aliyun.sms.templateCode=your_templateCode
其中,accessKeyId
和accessKeySecret
是阿里云控制台上的AccessKey,signName
是短信签名,templateCode
是短信模板ID。
3. 发送短信
在Spring Boot中,我们可以使用MQ来异步发送短信,提高系统的响应速度。这里以ActiveMQ为例,演示如何整合阿里大于和ActiveMQ发送短信。
首先,在pom.xml
文件中添加ActiveMQ依赖:
<!-- ActiveMQ -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
4、在application.properties文件中添加ActiveMQ的配置:
# ActiveMQ配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
5、定义一个发送短信的Service:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.sms.model.v20170525.SendSmsRequest;
import com.aliyuncs.sms.model.v20170525.SendSmsResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
@Service
public class SmsService {
@Value("${aliyun.sms.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.sms.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.sms.signName}")
private String signName;
@Value("${aliyun.sms.templateCode}")
private String templateCode;
@Resource
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 发送短信
*
* @param mobile 手机号
* @param paramMap 短信参数
*/
public void sendSms(String mobile, Map<String, String> paramMap) {
jmsMessagingTemplate.convertAndSend("sms.queue", new SmsMessage(mobile, paramMap));
}
/**
* 发送短信
*
* @param message 短信消息
*/
public void sendSms(SmsMessage message) {
DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(message.getMobile());
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setTemplateParam(message.getParamJson());
try {
SendSmsResponse response = client.getAcsResponse(request);
if (response.getCode() != null && response.getCode().equals("OK")) {
System.out.println("短信发送成功:" + message);
} else {
System.out.println("短信发送失败:" + message + ", " + response.getMessage());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
SmsMessage
类用于封装短信消息:
public class SmsMessage implements Serializable {
private static final long serialVersionUID = 1L;
private String mobile;
private Map<String, String> paramMap;
public SmsMessage() {
}
public SmsMessage(String mobile, Map<String, String> paramMap) {
this.mobile = mobile;
this.paramMap = paramMap;
}
// getter/setter
}
6、在启动类中配置消息队列和监听器:
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.jms.Queue;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Queue smsQueue() {
return new ActiveMQQueue("sms.queue");
}
@Bean
public SmsMessageListener smsMessageListener() {
return new SmsMessageListener();
}
}
SmsMessageListener
类用于监听消息队列并发送短信:文章来源:https://www.toymoban.com/news/detail-712517.html
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class SmsMessageListener {
@Autowired
private SmsService smsService;
@JmsListener(destination = "sms.queue")
public void receiveMessage(SmsMessage message) {
smsService.sendSms(message);
}
}
这样,当我们调用SmsService
的sendSms
方法时,短信消息会被发送到消息队列中。SmsMessageListener
监听消息队列并处理消息,调用SmsService
的sendSms
方法发送短信。因为短信发送是一个IO密集型操作,把它异步处理可以提高系统的响应速度。文章来源地址https://www.toymoban.com/news/detail-712517.html
到了这里,关于springboot整合阿里大于并结合mq发送短信的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!