前言
本文依托于
在SpringCloud Alibaba的使用过程中,我总结为如下步骤:
- 下载并启动服务端
- 客户端引入spring-cloud-starter-alibaba的jar包
- 客户端properties或yml加入相关配置
- 客户端加上相应的注解开启功能
- 服务端增加相应配置
- 数据持久化,服务端集群部署
seata服务端
1.下载seata
在下面github地址找到最新版的seata下载源码和jar包,源码中有配置文件后续有用。
https://github.com/seata/seata/releases
2.修改file.conf
seata默认是file方式保存数据,设置默认组,还有超时时间。 在文件最下方添加下面的配置,更详细的配置见官网:
https://github.com/seata/seata-samples/blob/master/doc/quick-integration-with-spring-cloud.md
service {
#vgroup->rgroup
#vgroupMapping.my_test_tx_group = "default"
#only support single node
default.grouplist = "127.0.0.1:8091"
#degrade current not support
enableDegrade = false
#disable
disable = false
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
max.commit.retry.timeout = "-1"
max.rollback.retry.timeout = "-1"
}
3.单机启动seata服务
seata-server.bat
seata客户端
1.引入jar
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2.properties加入配置
# seata配置 file
seata.enabled=true
seata.config.type=file
seata.registry.type=file
#seata.service.grouplist.default=192.168.43.11:8091
seata.service.default.grouplist=192.168.43.11:8091
seata.service.enableDegrade=false
seata.service.disableGlobalTransaction=false
# 事务分组
spring.cloud.alibaba.seata.tx-service-group=${spring.application.name}-group
# vgroupMapping后名称修改为上面的事务分组的value值
seata.service.vgroupMapping.springcloud-alibaba-consumer-group=default
# 事务分组
spring.cloud.alibaba.seata.tx-service-group=${spring.application.name}-group
# 连接数据库
spring.datasource.url=jdbc:mysql://192.168.43.11:3306/order?useUnicode=true&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 指定 mapper.xml 的位置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
3.db
实现全局事务,客户端的数据库需要创建undo_log表, 文件在seata-1.4.2\script\client\at\db\mysql.sql下
CREATE TABLE `undo_log` (
`branch_id` bigint(20) NOT NULL COMMENT 'branch transaction id',
`xid` varchar(128) NOT NULL COMMENT 'global transaction id',
`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
`rollback_info` longblob NOT NULL COMMENT 'rollback info',
`log_status` int(11) NOT NULL COMMENT '0:normal status,1:defense status',
`log_created` datetime(6) NOT NULL COMMENT 'create datetime',
`log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='AT transaction mode undo table';
测试seata
实现一个下订单功能,分别消费订单服务,用户服务,商品服务。
1.db
主要有用户表,商品表,订单表,订单商品表
CREATE TABLE `t_order` (
`orderid` int(10) NOT NULL AUTO_INCREMENT COMMENT '订单id',
`amount` decimal(10,2) DEFAULT '0.00' COMMENT '金额',
`userid` int(10) DEFAULT NULL COMMENT '会员id',
PRIMARY KEY (`orderid`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
CREATE TABLE `t_orderplu` (
`orderid` int(10) NOT NULL COMMENT '订单id',
`pluid` int(10) NOT NULL COMMENT '商品id',
`sno` int(10) NOT NULL COMMENT '序号',
`qty` decimal(10,2) NOT NULL COMMENT '数量',
`amount` decimal(10,2) NOT NULL COMMENT '金额',
PRIMARY KEY (`orderid`,`pluid`,`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='订单商品表';
CREATE TABLE `t_plu` (
`pluid` int(10) NOT NULL COMMENT '商品id',
`pluname` varchar(50) DEFAULT NULL COMMENT '商品名称',
`price` decimal(10,2) DEFAULT '0.00' COMMENT '价格',
`qty` decimal(10,2) DEFAULT '0.00' COMMENT '库存数量',
PRIMARY KEY (`pluid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
CREATE TABLE `t_user` (
`userid` int(10) NOT NULL COMMENT '用户id',
`username` varchar(50) DEFAULT NULL COMMENT '用户名',
`money` decimal(10,2) DEFAULT '0.00' COMMENT '余额',
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
2.消费者逻辑代码
1.在接口处通过feign分别调用用户服务,订单服务,商品服务。 2.使用@GlobalTransactional实现全局事务
@RestController
@RequestMapping("/place")
public class PlaceOrderController {
@Autowired
private OrderFeignService orderFeignService;
@Autowired
private PluFeignService pluFeignService;
@Autowired
private UserFeignService userFeignService;
@RequestMapping("/order")
@GlobalTransactional(rollbackFor = {Exception.class, RuntimeException.class})
public boolean placeOrder(@RequestParam("userId") int userId, @RequestParam("pluId") int pluId) {
User user = userFeignService.queryUserById(userId);
if (user == null) {
return false;
}
Plu plu = pluFeignService.queryPluById(pluId);
if (plu == null) {
return false;
}
if (plu.getQty().compareTo(BigDecimal.ONE) < 0) {
return false;
}
if (plu.getPrice().compareTo(user.getMoney()) > 0) {
return false;
}
OrderPlu orderPlu = new OrderPlu();
orderPlu.setQty(BigDecimal.ONE);
orderPlu.setPluId(pluId);
orderPlu.setAmount(plu.getPrice());
Order order = new Order();
order.setUserId(userId);
order.setAmount(plu.getPrice());
List<OrderPlu> pluList = new ArrayList<>();
pluList.add(orderPlu);
order.setPluList(pluList);
boolean flag = orderFeignService.createOrder(order);
if (flag) {
userFeignService.reduceMoney(userId, plu.getPrice());
pluFeignService.reduceQty(BigDecimal.ONE, pluId);
}
return true;
}
}
@FeignClient(value = "springcloud-alibaba-provider1")
public interface OrderFeignService {
@RequestMapping("/order/createOrder")
boolean createOrder(@RequestBody Order order);
}
@FeignClient(value = "springcloud-alibaba-provider2")
public interface PluFeignService {
@RequestMapping("/plu/queryPluById")
Plu queryPluById(@RequestParam("pluId") int pluId);
@RequestMapping("/plu/reduceQty")
void reduceQty(@RequestParam("qty") BigDecimal qty, @RequestParam("pluId") int pluId);
}
@FeignClient(value = "springcloud-alibaba-provider3")
public interface UserFeignService {
@RequestMapping(value = "/user/queryUserById")
User queryUserById(@RequestParam("userId") int userId);
@RequestMapping(value = "/user/reduceMoney")
void reduceMoney(@RequestParam("userId") int userId, @RequestParam("money") BigDecimal money);
}
3.订单服务逻辑代码
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/createOrder")
public boolean createOrder(@RequestBody Order order){
int orderId = orderService.insertOrder(order);
order.setOrderId(orderId);
orderService.insertOrderPlu(orderId, order.getPluList());
return true;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ykq.springcloudalibabaprovider.mapper.OrderMapper">
<insert id="insertOrder" useGeneratedKeys="true" keyProperty="orderId" parameterType="com.ykq.springcloudalibabaentity.entity.Order">
insert into t_order(orderId,
userId,
amount
)
values(null,
#{userId,jdbcType=INTEGER},
#{amount,jdbcType=NUMERIC}
)
</insert>
<insert id="insertOrderPlu">
insert into t_orderplu(orderId,
pluId,
sno,
qty,
amount
)
values
<foreach item="plu" index="index" collection="pluList" separator=",">
(#{orderId,jdbcType=INTEGER},
#{plu.pluId,jdbcType=INTEGER},
#{index},
#{plu.qty,jdbcType=NUMERIC},
#{plu.amount,jdbcType=NUMERIC}
)
</foreach>
</insert>
</mapper>
4.商品服务逻辑代码
@RestController
@RequestMapping("/plu")
public class PluController {
@Autowired
private PluService pluService;
@RequestMapping("/queryPluById")
public Plu queryPluById(@RequestParam("pluId") int pluId) {
return pluService.queryPluById(pluId);
}
@RequestMapping("/reduceQty")
public void reduceQty(@RequestParam("qty") BigDecimal qty, @RequestParam("pluId") int pluId) {
pluService.reduceQty(qty, pluId);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ykq.springcloudalibabaprovider.mapper.PluMapper">
<select id="queryPluById" resultType="com.ykq.springcloudalibabaentity.entity.Plu">
select pluid,
pluname,
price,
qty
from t_plu
where pluid = #{pluId,jdbcType=INTEGER}
</select>
<update id="reduceQty">
update t_plu
set qty = qty - #{qty,jdbcType=NUMERIC}
where pluid = #{pluId,jdbcType=INTEGER}
</update>
</mapper>
5.用户服务逻辑代码
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/queryUserById")
public User queryUserById(@RequestParam("userId") int userId) {
return userService.queryUserById(userId);
}
@RequestMapping("/reduceMoney")
public void reduceMoney(@RequestParam("userId") int userId, @RequestParam("money") BigDecimal money) {
userService.reduceMoney(userId, money);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ykq.springcloudalibabaprovider.mapper.UserMapper">
<select id="queryUserById" resultType="com.ykq.springcloudalibabaentity.entity.User">
select userId,
userName,
money
from t_user
where userid = #{userId,jdbcType=INTEGER}
</select>
<update id="reduceMoney">
update t_user
set money = money - #{money,jdbcType=NUMERIC}
where userId = #{userId,jdbcType=INTEGER}
</update>
</mapper>
6.测试
(1)正常场景
可以看到事务提交成功 commit status: Committed
2022-05-10 14:41:47.884 INFO 26612 --- [nio-9001-exec-1] io.seata.tm.TransactionManagerHolder : TransactionManager Singleton io.seata.tm.DefaultTransactionManager@16f40970
2022-05-10 14:41:47.894 INFO 26612 --- [Send_TMROLE_1_1] i.s.c.r.netty.NettyClientChannelManager : will connect to 192.168.43.11:8091
2022-05-10 14:41:47.895 INFO 26612 --- [Send_TMROLE_1_1] i.s.core.rpc.netty.NettyPoolableFactory : NettyPool create channel to transactionRole:TMROLE,address:192.168.43.11:8091,msg:< RegisterTMRequest{applicationId='springcloud-alibaba-consumer', transactionServiceGroup='springcloud-alibaba-consumer-group'} >
2022-05-10 14:41:47.904 INFO 26612 --- [Send_TMROLE_1_1] i.s.c.rpc.netty.TmNettyRemotingClient : register TM success. client version:1.3.0, server version:1.4.2,channel:[id: 0xe3e04122, L:/192.168.43.11:53659 - R:/192.168.43.11:8091]
2022-05-10 14:41:47.904 INFO 26612 --- [Send_TMROLE_1_1] i.s.core.rpc.netty.NettyPoolableFactory : register success, cost 5 ms, version:1.4.2,role:TMROLE,channel:[id: 0xe3e04122, L:/192.168.43.11:53659 - R:/192.168.43.11:8091]
2022-05-10 14:41:48.189 INFO 26612 --- [nio-9001-exec-1] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [2.0.0.1:8091:261469811311718401]
2022-05-10 14:42:09.028 INFO 26612 --- [nio-9001-exec-1] i.seata.tm.api.DefaultGlobalTransaction : [2.0.0.1:8091:261469811311718401] commit status: Committed
2022-05-10 14:42:38.901 INFO 26612 --- [nio-9001-exec-2] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [2.0.0.1:8091:261469811311718406]
2022-05-10 14:42:39.033 INFO 26612 --- [nio-9001-exec-2] i.seata.tm.api.DefaultGlobalTransaction : [2.0.0.1:8091:261469811311718406] commit status: Committed
(2)异常场景
修改商品服务减库存为抛异常,可以看到 rollback status: Rollbacked
@RequestMapping("/reduceQty")
public void reduceQty(@RequestParam("qty") BigDecimal qty, @RequestParam("pluId") int pluId) {
throw new RuntimeException();
}
2022-05-10 14:53:53.337 INFO 26612 --- [nio-9001-exec-1] i.seata.tm.api.DefaultGlobalTransaction : Begin new global transaction [2.0.0.1:8091:261469811311718414]
2022-05-10 14:53:56.093 INFO 26612 --- [nio-9001-exec-1] i.seata.tm.api.DefaultGlobalTransaction : [2.0.0.1:8091:261469811311718414] rollback status: Rollbacked
seata整合nacos,进行持久化
1.修改服务端registry.conf
修改注册和配置方式为nacos,指定nacos地址。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "192.168.43.11:8000"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.43.11:8000"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
dataId = "seataServer.properties"
}
}
2.修改客户端application.properties配置
修改seata.config.type和seata.registry.type的值为nacos,其他的为新增配置
# 配置中心
seata.config.type=nacos
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.username=nacos
seata.config.nacos.password=nacos
seata.config.nacos.server-addr=192.168.43.11:8000
# 注册中心
seata.registry.type=nacos
seata.registry.nacos.group=SEATA_GROUP
seata.registry.nacos.username=nacos
seata.registry.nacos.password=nacos
seata.registry.nacos.server-addr=192.168.43.11:8000
seata.registry.nacos.application=seata-server
3.创建数据库seata
脚本路径在之前下载的源码seata-1.4.2\script\server\db下
CREATE TABLE IF NOT EXISTS `global_table`
(
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`status` TINYINT NOT NULL,
`application_id` VARCHAR(32),
`transaction_service_group` VARCHAR(32),
`transaction_name` VARCHAR(128),
`timeout` INT,
`begin_time` BIGINT,
`application_data` VARCHAR(2000),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE IF NOT EXISTS `branch_table`
(
`branch_id` BIGINT NOT NULL,
`xid` VARCHAR(128) NOT NULL,
`transaction_id` BIGINT,
`resource_group_id` VARCHAR(32),
`resource_id` VARCHAR(256),
`branch_type` VARCHAR(8),
`status` TINYINT,
`client_id` VARCHAR(64),
`application_data` VARCHAR(2000),
`gmt_create` DATETIME(6),
`gmt_modified` DATETIME(6),
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(128),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
4.上传seata相关配置到nacos
(1)修改config.txt配置文件
配置文件在源码 seata-1.4.2\script\config-center\config.txt 在文件最下方添加下列配置,可以覆盖上面已有的相同配置,或者修改已有的配置的value值。 注意:service.vgroupMapping后的名称是自定义的,没有或错误会导致no available service ‘null’ found, please make sure registry config correct
store.mode=db
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
service.vgroupMapping.springcloud-alibaba-consumer-group=default
service.vgroupMapping.springcloud-alibaba-provider1-group=default
service.vgroupMapping.springcloud-alibaba-provider2-group=default
service.vgroupMapping.springcloud-alibaba-provider3-group=default
(2)上传config.txt文件内容至nacos
在 seata-1.4.2\script\config-center\nacos 目录下,右键鼠标选择 Git Bush Here,在弹出的 Git 命令窗口中执行以下命令,将 config.txt 中的配置上传到 Nacos 配置中心。
sh nacos-config.sh -h 127.0.0.1 -p 8000 -g SEATA_GROUP -u nacos -w nacos
(3)在nacos中查看
5.no available service ‘null’ found, please make sure registry config correct
虽然在application.properties中配置了service.vgroupMapping,但是看源码,可以发现是通过nacos拉取配置的。我们打断点跟踪一下。
(1)NettyClientChannelManager
(2)NacosRegistryServiceImpl
(3)NacosConfiguration
6.begin global request failed. xid=null, msg=Data truncation: Data too long for column ‘transaction_service_group’ at row 1
这是由于设置的tx-service-group的value太长,修改表字段长度
seata集群
只需要更换端口启动多个seata即可,之前一直在纠结配置的默认地址只有一个端口的服务,为啥不需要变。这是因为还配置了seata.registry.nacos.application=seata-server,根据服务名称在nacos可以找到多个服务。
(1)8091服务
seata-server.bat -p 8091 -n 1
可以看到8091的服务,所有服务都注册进来
(2)8092服务
seata-server.bat -p 8092 -n 2
nacos上的实例数也有两个
|