快捷搜索: 王者荣耀 脱发

使用rabbitmq实现短信验证码的的发送

短信生产者,使用rabbitmq发送消息,达到应用的解耦

, 采用直连模式 需要指定一个routingkey 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> 配置rabbitmq文件

server.port=8777
spring.rabbitmq.host=xx
spring.rabbitmq.port=xx
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
@RestController
@RequestMapping("user")
public class MessageController {
          
   
    @Autowired
    //注入rabbitTemplate模板方便使用api
    private RabbitTemplate rabbitTemplate;
    @PostMapping("sendMsg")
    public String sendMsg(String phone){
          
   
        //生成6位验证码
        Random random = new Random();
        int code = random.nextInt(999999);
        if(code<100000){
          
   
            code = code + 100000;
        }
        HashMap<Object, Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code+"");

        //直连模式,将电话号码和验证码发送出去。
        rabbitTemplate.convertAndSend("queueSms", JSON.toJSONString(map));
        return phone;
    }
}

接收消息方

首先得在阿里云上注册短信服务

配置文件

accessKeyId=xx
accessKeySecret=xx
smsCode=xx
param={
          
   "code":"[value]"}

写一个短信工具类

@Component
public class SmsUtil {
          
   
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${accessKeySecret}")
    private String accessKeySecret;

    public CommonResponse sendSms(String phone, String smsCode, String param) {
          
   
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        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("RegionId", "cn‐hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "小蚂蚁");
        request.putQueryParameter("TemplateCode", smsCode);
        request.putQueryParameter("TemplateParam", param);
        try {
          
   
            CommonResponse response = client.getCommonResponse(request);
            return response;
        } catch (ServerException e) {
          
   
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
          
   
            e.printStackTrace();
            return null;
        }
    }
}

接收方代码

@Component
@PropertySource("classpath:application-sms.properties")
public class MessageReceive {
          
   
    @Value("${smsCode}")
    private String smsCode;
    @Value("${param}")
    private String param;
    @Autowired
    private SmsUtil smsUtil;
    //监听queueSms队列
    @RabbitListener(queuesToDeclare = @Queue("queueSms"))
    public void receiveMessage(Message message) throws UnsupportedEncodingException {
          
   
        String msg = new String(message.getBody(), "utf-8");
        Map<String,String> map = JSON.parseObject(msg, Map.class);
        String phone = map.get("phone");
        String code = map.get("code");
        String params = param.replace("[value]", code);
        try {
          
   
            CommonResponse commonResponse = smsUtil.sendSms(phone, smsCode, params);
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }
        System.out.println(map.get("phone")+"----"+map.get("code"));
    }
}
经验分享 程序员 微信小程序 职场和发展