本文依托于
- SpringCloud Alibaba环境集成之nacos
- SpringCloud Alibaba环境集成之sentinel
在SpringCloud Alibaba的使用过程中,我总结为如下步骤:
- 下载并启动服务端
- 客户端引入spring-cloud-starter-alibaba的jar包
- 客户端properties或yml加入相关配置
- 客户端加上相应的注解开启功能
- 服务端增加相应配置
- 数据持久化,服务端集群部署
在下面github地址找到最新版的seata下载源码和jar包,源码中有配置文件后续有用。
2.修改file.confhttps://github.com/seata/seata/releases
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.batseata客户端 1.引入jar
2.properties加入配置com.alibaba.cloud spring-cloud-starter-alibaba-seata mysql mysql-connector-java 8.0.29 org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0
# 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
@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 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;
}
}
4.商品服务逻辑代码insert into t_order(orderId, userId, amount ) values(null, #{userId,jdbcType=INTEGER}, #{amount,jdbcType=NUMERIC} ) insert into t_orderplu(orderId, pluId, sno, qty, amount ) values (#{orderId,jdbcType=INTEGER}, #{plu.pluId,jdbcType=INTEGER}, #{index}, #{plu.qty,jdbcType=NUMERIC}, #{plu.amount,jdbcType=NUMERIC} )
@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);
}
}
5.用户服务逻辑代码select pluid, pluname, price, qty from t_plu where pluid = #{pluId,jdbcType=INTEGER} update t_plu set qty = qty - #{qty,jdbcType=NUMERIC} where pluid = #{pluId,jdbcType=INTEGER}
@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);
}
}
6.测试 (1)正常场景update t_user set money = money - #{money,jdbcType=NUMERIC} where userId = #{userId,jdbcType=INTEGER}
可以看到事务提交成功 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();
// pluService.reduceQty(qty, pluId);
}
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: Rollbackedseata整合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-server3.创建数据库seata
脚本路径在之前下载的源码seata-1.4.2scriptserverdb下
-- -------------------------------- The script used when storeMode is 'db' --------------------------------
-- the table to store GlobalSession data
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;
-- the table to store BranchSession data
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;
-- the table to store lock data
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.2scriptconfig-centerconfig.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.2scriptconfig-centernacos 目录下,右键鼠标选择 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
这是由于设置的tx-service-group的value太长,修改表字段长度
只需要更换端口启动多个seata即可,之前一直在纠结配置的默认地址只有一个端口的服务,为啥不需要变。这是因为还配置了seata.registry.nacos.application=seata-server,根据服务名称在nacos可以找到多个服务。
(1)8091服务seata-server.bat -p 8091 -n 1
可以看到8091的服务,所有服务都注册进来
seata-server.bat -p 8092 -n 2
nacos上的实例数也有两个



