大致的业务:通过rabbitmq,接收消息异步发送验证短信(非常简单)
工具:IEDA,springCloud、rabbitmq、阿里云鼎信短信API
实现(很简单):
创建发送短信的微服务
在调用端将消息发送给交换机(调用端,不是短信微服务端)
@Override
public void sendCode(String phone) {
HashMap<String, Object> map = new HashMap<>();
map.put("phone",phone);
map.put("function","验证身份");
map.put("code", new Random().nextInt(8000)+1000);
try {
amqpTemplate.convertAndSend("GMALL-SEND-EXCHANGE","send.code",map);
} catch (AmqpException e) {
e.printStackTrace();
}
}
接收端,接收消息(发送短信的微服务)
package com.atguigu.gmall.send.listener;
import com.atguigu.gmall.send.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class UmsListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "GMALL-SEND-QUEUE",durable = "true"),
exchange = @Exchange(value = "GMALL-SEND-EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
key = {"send.code"}
))
public void sendCode(Map<String,Object>map){
String host = "http://dingxin.market.alicloudapi.com";
String path = "/dx/sendSms";
String method = "POST";
String appcode = "ec6ff90cbd07*****159746b157";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
querys.put("mobile", map.get("phone").toString());
querys.put("param", "code:"+map.get("code").toString());
querys.put("tpl_id", "TP1711063");
Map<String, String> bodys = new HashMap<String, String>();
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
//获取response的body
//System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(map.get("code"));
}
}
具体的工具类,看官方的API下载使用
application.yml配置文件(两个微服务都必须写,调用端和别调用端)
rabbitmq:
virtual-host: /theking #虚拟主机名称以/开头
host: 192.168.232.128 #地址
username: theking #用户名
password: 1217 #密码
短信微服务引入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>