Java之品優購課程講義_day14(4)
阿新 • • 發佈:2018-12-15
簡訊微服務 3.1 需求分析 構建一個通用的簡訊傳送服務(獨立於品優購的單獨工程),接收 activeMQ 的訊息(MAP 型別) 訊息包括手機號(mobile)、簡訊模板號(template_code)、簽名(sign_name)、引數字串(param ) 3.2 程式碼實現 3.2.1 工程搭建 (1)建立工程 itcast_sms (JAR 工程),POM 檔案引入依賴
<properties> <java.version>1.7</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.2.5</version> </dependency> </dependencies>
(2)建立引導類
package cn.itcast.sms; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
(3)建立配置檔案 application.properties
server.port=9003
spring.activemq.broker-url=tcp://192.168.25.135:61616
accessKeyId=不告訴你
accessKeySecret=不告訴你
3.1.1 簡訊工具類 參照之前的簡訊 demo 建立簡訊工具類
package cn.itcast.sms; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; /** *簡訊工具類 *@author Administrator * */ public class SmsUtil { //產品名稱:雲通訊簡訊 API 產品,開發者無需替換 static final String product = "Dysmsapi"; //產品域名,開發者無需替換 static final String domain = "dysmsapi.aliyuncs.com"; @Autowired private Environment env; // TODO 此處需要替換成開發者自己的 AK(在阿里雲訪問控制檯尋找) /** *傳送簡訊 *@param mobile 手機號 *@param template_code 模板號
*@param sign_name 簽名
*@param param 引數
*@return
*@throws ClientException
*/
public SendSmsResponse sendSms(String mobile,String template_code,String sign_name,String param) throws ClientException {
String accessKeyId =env.getProperty("accessKeyId");
String accessKeySecret = env.getProperty("accessKeySecret");
//可自助調整超時時間System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化 acsClient,暫不支援 region 化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile);
//組裝請求物件-具體描述見控制檯-文件部分內容SendSmsRequest request = new SendSmsRequest();
//必填:待發送手機號
request.setPhoneNumbers(mobile);
//必填:簡訊簽名-可在簡訊控制檯中找到request.setSignName(sign_name);
//必填:簡訊模板-可在簡訊控制檯中找到request.setTemplateCode(template_code);
//可選:模板中的變數替換 JSON 串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
request.setTemplateParam(param);
//選填-上行簡訊擴充套件碼(無特殊需求使用者請忽略此欄位)
//request.setSmsUpExtendCode("90997");
//可選:outId 為提供給業務方擴充套件欄位,最終在簡訊回執訊息中將此值帶回給呼叫者
request.setOutId("yourOutId");
//hint 此處可能會丟擲異常,注意 catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public QuerySendDetailsResponse querySendDetails(String mobile,String bizId)
throws ClientException {
String accessKeyId =env.getProperty("accessKeyId");
String accessKeySecret = env.getProperty("accessKeySecret");
//可自助調整超時時間System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化 acsClient,暫不支援 region 化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile);
//組裝請求物件
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-號碼request.setPhoneNumber(mobile);
//可選-流水號request.setBizId(bizId);
//必填-傳送日期 支援 30 天內記錄查詢,格式 yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); request.setSendDate(ft.format(new Date()));
//必填-頁大小
request.setPageSize(10L);
//必填-當前頁碼從 1 開始計數
request.setCurrentPage(1L);
//hint 此處可能會丟擲異常,注意 catch QuerySendDetailsResponse querySendDetailsResponse =
acsClient.getAcsResponse(request);
return querySendDetailsResponse;
}
}
3.1.1 訊息監聽類 建立 SmsListener.java
package cn.itcast.sms;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
/**
*訊息監聽類
*@author Administrator
*/ @Component
public class SmsListener { @Autowired
private SmsUtil smsUtil;
@JmsListener(destination="sms")
public void sendSms(Map<String,String> map){
try {
SendSmsResponse response = smsUtil.sendSms( map.get("mobile"), map.get("template_code"), map.get("sign_name"), map.get("param") );
System.out.println("Code=" + response.getCode()); System.out.println("Message=" + response.getMessage()); System.out.println("RequestId=" + response.getRequestId()); System.out.println("BizId=" + response.getBizId());
} catch (ClientException e) { e.printStackTrace();
}
}
}
3.1 程式碼測試
修改 springboot-demo 的 QueueController.java
@RequestMapping("/sendsms")
public void sendSms(){
Map map=new HashMap<>();
map.put("mobile", "13900001111"); map.put("template_code", "SMS_85735065");
map.put("sign_name", "黑馬");
map.put("param", "{\"number\":\"102931\"}"); jmsMessagingTemplate.convertAndSend("sms",map);
}
啟動 itcast_sms
啟動 springboot-demo