在pom.xml
文件中添加以下依赖:
<dependency>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-dysmsapiartifactId>
<version>1.3.2version>
dependency>
在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。
在Spring Boot中,我们可以使用MQ来异步发送短信,提高系统的响应速度。这里以ActiveMQ为例,演示如何整合阿里大于和ActiveMQ发送短信。
首先,在pom.xml
文件中添加ActiveMQ依赖:
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-allartifactId>
<version>5.15.9version>
dependency>
application.properties
文件中添加ActiveMQ的配置:# ActiveMQ配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
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
}
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
类用于监听消息队列并发送短信:
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密集型操作,把它异步处理可以提高系统的响应速度。