规划
适用场景:业务量数据量不是很大的情况,满足最终一致性。
服务:service-order,server-buy
实现说明
每个服务里,利用spring的事务管理,都可以保证消息被成功发送和接受。在同一个事务里,注意消息消费方pay中的事件表主键为id,但不是自增,当消息重复消费时,就会因为数据库的主键唯一而失败。
表准备
1,database:service-order
CREATE TABLE `tbl_order_event` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_type` varchar(255) DEFAULT NULL,
`process` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`createTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `idx_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
2,database:service-pay
CREATE TABLE `tbl_order_event` (
`id` int(11) NOT NULL,
`order_type` varchar(255) DEFAULT NULL,
`process` varchar(255) DEFAULT NULL,
`content` varchar(255) DEFAULT NULL,
`createTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
代码实现
消息队列activeMQ配置
activemq.xml中添加死信队列的配置
<destinationPolicy>
<policyMap>
<policyEntries>
<!--死信队列-->
<policyEntry queue=">">
<deadLetterStrategy>
<individualDeadLetterStrategy queuePrefix="DLQ."
useQueueForQueueMessages="true" processNonPersistent="true" />
</deadLetterStrategy>
</policyEntry>
<policyEntry topic=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
2,order代码
pom.xml
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--activemq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--activemq pool-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<!-- mysql:MyBatis相关依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mysql:mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mysql:阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
application.yml
server:
port: 5001
#应用名称及验证账号
spring:
application:
name: service-order
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
pool:
enabled: true
max-connections: 100
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.0.0.127:3306/service-order?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
dbcp2:
initial-size: 5
min-idle: 5
max-total: 5
max-wait-millis: 200
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
mybatis:
mapper-locations:
- classpath:mapper/*.xml
启动类:
package com.example.serviceorder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableJms
@EnableScheduling
public class ServiceOrderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceOrderApplication.class, args);
}
@GetMapping("/test")
public String test() {
return "hello world";
}
}
ActiveMQConfig消息对了配置类
package com.example.serviceorder.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Bean
public Queue queue() {
return new ActiveMQQueue("ActiveMQQueue");
}
@Bean
public ActiveMQConnectionFactory connectionFactory(){
return new ActiveMQConnectionFactory(brokerUrl);
}
}
task 定时任务
package com.example.serviceorder.task;
import com.example.serviceorder.dao.TblOrderEventDao;
import com.example.serviceorder.entity.TblOrderEvent;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.jms.Queue;
import java.util.List;
@Component
public class ProduceTask {
@Autowired
private TblOrderEventDao tblOrderEventDao;
@Autowired
private Queue queue;
@Autowired
JmsMessagingTemplate jmsMessagingTemplate;
@Scheduled(cron="0/5 * * * * ?")
@Transactional(rollbackFor = Exception.class)
public void task(){
System.out.println("定时任务");
List<TblOrderEvent> tblOrderEventList = tblOrderEventDao.selectByOrderType("1");
for (int i = 0; i < tblOrderEventList.size(); i++) {
TblOrderEvent event = tblOrderEventList.get(i);
// 更改这条数据的orderType为2
tblOrderEventDao.updateEvent(event.getOrderType());
System.out.println("修改数据库完成");
jmsMessagingTemplate.convertAndSend(queue,JSONObject.fromObject(event).toString());
}
}
}
entity和dao利用工具代码生成
pay 代码
pom.xml
<dependencies>
<!--activemq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--activemq pool-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>
<!-- mysql:MyBatis相关依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- mysql:mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mysql:阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
yml
server:
port: 5002
spring:
application:
name: service-pay
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
pool:
enabled: true
max-connections: 100
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.0.0.127:3306/service-pay?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
dbcp2:
initial-size: 5
min-idle: 5
max-total: 5
max-wait-millis: 200
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
mybatis:
mapper-locations:
- classpath:mapper/*.xml
启动类:
package com.example.servicebuy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
@SpringBootApplication
@EnableJms
public class ServiceBuyApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBuyApplication.class, args);
}
}
ConsumerQueue 监听消息队列
package com.example.servicebuy.component;
import com.example.servicebuy.dao.TblOrderEventDao;
import com.example.servicebuy.entity.TblOrderEvent;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
@Component
public class ConsumerQueue {
@Autowired
private TblOrderEventDao tblOrderEventDao;
@JmsListener(destination = "ActiveMQQueue",containerFactory = "jmsListenerContainerFactory")
public void receive(TextMessage textMessage, Session session) throws JMSException {
try {
System.out.println("收到的消息:"+textMessage.getText());
String content = textMessage.getText();
TblOrderEvent tblOrderEvent = (TblOrderEvent) JSONObject.toBean(JSONObject.fromObject(content),TblOrderEvent.class);
tblOrderEventDao.insert(tblOrderEvent);
// 业务完成,确认消息 消费成功
textMessage.acknowledge();
}catch (Exception e){
// 回滚消息
e.printStackTrace();
// e.getMessage(); // 放到log中。
System.out.println("异常了");
session.recover();
}
}
/**
* 补偿 处理(人工,脚本)。自己根据自己情况。
* @param text
*/
@JmsListener(destination = "DLQ.ActiveMQQueue")
public void receive2(String text){
System.out.println("死信队列:"+text);
}
}
ActiveMQConfig 消息队列配置类
package com.example.servicebuy.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.Queue;
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
/**
* 连接工厂
* @param redeliveryPolicy
* @return
*/
@Bean
public ActiveMQConnectionFactory connectionFactory(RedeliveryPolicy redeliveryPolicy){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("admin","admin",brokerUrl);
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
return activeMQConnectionFactory;
}
/**
* 重发配置
* @return
*/
@Bean
public RedeliveryPolicy redeliveryPolicy(){
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
return redeliveryPolicy;
}
/**
* 设置消息队列 确认机制
* @param activeMQConnectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory jmsListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(activeMQConnectionFactory);
// 1: 自动确认,2: 客户端手动确认,3:自动批量确认,4 事务提交并确认。
bean.setSessionAcknowledgeMode(2);
return bean;
}
}
测试
正常业务:
order数据库
定时任务5s后更新
pay数据库:
控制台输出:
order
pay
异常时,消息队列会一直重试6次,6次后将消息扔到死信队列。死信队列依据业务需要进行合理的处理。一般都是记录日志或者是记录到数据库表,后续人工去修复数据。