当前位置: 首页 > news >正文

使用RabbitMQ发送短信

1、在项目中分别创建模块financial-core、financial-mq、financial-sms,如图:

在这里插入图片描述

模块构成

  <modules><module>financial-common</module><module>financial-base</module><module>financial-core</module><module>financial-gateway</module>       <module>financial-sms</module><module>financial-mq</module>        </modules>

2、搭建RabbitMQ服务,参考下面文章

https://lovoo.blog.csdn.net/article/details/119174146

3、MQ模块financial-mq

在这里插入图片描述

3.1 在pom.xml添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>koo-financial-parent</artifactId><groupId>com.koo</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>financial-mq</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

3.2 MQ Json字符串转换 配置

@Configuration
public class MQConfig {@Beanpublic MessageConverter messageConverter(){//json字符串转换器return new Jackson2JsonMessageConverter();}
}

3.3 定义交换机、路由、队列常量

public class MQConst {public static final String EXCHANGE_TOPIC_SMS = "exchange.topic.sms";//交换机public static final String ROUTING_SMS_ITEM = "routing.sms.item";//路由public static final String QUEUE_SMS_ITEM = "queue.sms.item";//消息队列}

3.4 定义发送方法

@Service
@Slf4j
public class MQService {@Resourceprivate AmqpTemplate amqpTemplate;public boolean sendMessage(String exchange, String routingKey, Object message) {log.info("发送消息。。。。。。");amqpTemplate.convertAndSend(exchange, routingKey, message);return true;}
}

4、业务模块financial-core

4.1 在pom.xml中引入financial-mq模块

<dependencies><dependency><groupId>com.koo</groupId><artifactId>financial-base</artifactId><version>1.0.0</version></dependency><dependency><groupId>com.koo</groupId><artifactId>financial-mq</artifactId><version>1.0.0</version></dependency>     </dependencies>

4.2 在application.yml配置rabbitMQ信息

  #spring:rabbitmq:host: 127.0.0.1port: 5672virtual-host: /financial

4.3 发送信息

String mobile = userInfoService.getMobileByBindCode(bindCode);
SmsDTO smsDTO = new SmsDTO();
smsDTO.setMobile(mobile);
smsDTO.setMessage("充值成功");
mqService.sendMessage(MQConst.EXCHANGE_TOPIC_SMS,MQConst.ROUTING_SMS_ITEM,smsDTO
);

5、短信发送模块financial-sms

5.1 引入依赖

 <dependency><groupId>com.koo</groupId><artifactId>financial-mq</artifactId><version>1.0.0</version></dependency>

5.2 在application.yml配置rabbitMQ信息

  #spring:rabbitmq:host: 127.0.0.1port: 5672virtual-host: /financial

5.3 监听MQ消息,并发送短信

@Component
@Slf4j
public class SmsReceiver {@Resourceprivate SmsService smsService;@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true"),exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS),key = {MQConst.ROUTING_SMS_ITEM}))public void send(SmsDTO smsDTO) {log.info("SmsReceiver消息监听。。。。。。");HashMap<String, Object> param = new HashMap<>();param.put("code", smsDTO.getMessage());try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}smsService.send(smsDTO.getMobile(), SmsProperties.TEMPLATE_CODE, param);}
}
http://www.lryc.cn/news/35344.html

相关文章:

  • 10Wqps评论中台,如何架构?B站是这么做的!!!
  • 浅谈Linux下的shell--BASH
  • 邻桌为何一天就学完了SQL基础语法,数据分析必学的SQL,满满硬货
  • 机器视觉工程师国内出差必备神器
  • ReentrantLock 源码解读
  • 【算法】六大排序 插入排序 希尔排序 选择排序 堆排序 冒泡排序 快速排序
  • 类和对象万字详解
  • 如何使用码匠连接 CouchDB
  • MySQL对表操作
  • springboot3整合mybatis遇到的坑
  • SpringBoot+Spring常用注解总结
  • 优化UnRaid容器的WebUI端口设置实现应用快捷访问的方法
  • Android Framework-管理Activity和组件运行状态的系统进程—— ActivityManagerService(AMS)
  • 【C语言】结构体和共用体
  • 微搭低代码从入门到实战
  • AM5728(AM5708)开发实战之安装Debian 10桌面操作系统
  • ip-guardip-guard如何通过准入网关对指定的服务器进行通讯加密保护?
  • JavaScript基础语法
  • 《SQL基础》17. InnoDB引擎
  • api接口详解大全
  • 为什么要用VR全景?5个答案告诉你
  • 常用的深度学习优化方式
  • 全面吃透Java Stream流操作,让代码更加的优雅
  • 机器学习学习记录1:假设空间
  • 开源工具系列5:DependencyCheck
  • JDBC知识点全面总结2:JDBC实战编写CRUD
  • java - 数据结构,算法,排序
  • 二叉树经典14题——初学二叉树必会的简单题
  • 基于NMOSFET的电平转换电路设计
  • mongoDB搭建集群