模块构成
<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>
https://lovoo.blog.csdn.net/article/details/119174146
<?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>
@Configuration
public class MQConfig {
@Bean
public MessageConverter messageConverter(){
//json字符串转换器
return new Jackson2JsonMessageConverter();
}
}
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";//消息队列
}
@Service
@Slf4j
public class MQService {
@Resource
private AmqpTemplate amqpTemplate;
public boolean sendMessage(String exchange, String routingKey, Object message) {
log.info("发送消息。。。。。。");
amqpTemplate.convertAndSend(exchange, routingKey, message);
return true;
}
}
<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>
#spring:
rabbitmq:
host: 127.0.0.1
port: 5672
virtual-host: /financial
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
);
<dependency>
<groupId>com.koo</groupId>
<artifactId>financial-mq</artifactId>
<version>1.0.0</version>
</dependency>
#spring:
rabbitmq:
host: 127.0.0.1
port: 5672
virtual-host: /financial
@Component
@Slf4j
public class SmsReceiver {
@Resource
private 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);
}
}