分布式事务Seata1.5.2整合Nacos(二)
-
- 一、客户端搭建
-
- 1、客户端添加seata依赖
- 2、客户端配置文件
- 3、程序中使用
- 4、nacos中seata命名空间下添加配置
- 5、创建undo_log表
一、客户端搭建
1、客户端添加seata依赖
<!--分布式事务 -->
<!--移除alibaba-seata自带的seata-spring-boot-starter,因为自带的版本太低 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加seata-spring-boot-starter依赖,版本需要与服务端版本相对应,此版本使用1.5.2 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</dependency>
2、客户端配置文件
注意事项:
- 每个模块都需要添加一下配置
- 注意事务组名字,以下3处配置需要相互对应
seata.tx-service-group: server1
seata.service.vgroupMapping.xxx: default
服务端的service.vgroupMapping.xxx: default - nacos相关配置需要与服务端配置一样
# spring配置
spring:
datasource:
dynamic:
druid:
initial-size: 5
min-idle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,slf4j
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
datasource:
# 主库数据源
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/netty_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
username: root
password: 123456
# 从库数据源
netty_system_platform:
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/netty_system_platform?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
#schema: db/schema.sql # 配置则生效,自动初始化表结构
#data: db/data.sql # 配置则生效,自动初始化数据
#continue-on-error: true # 默认true,初始化失败是否继续
#separator: ";" # sql默认分号分隔符
seata: true # 开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭
seata:
# 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
enabled: true
# Seata 应用编号,默认为 ${spring.application.name}
application-id: ${spring.application.name}
# Seata 事务组编号,用于 TC 集群名。该配置需要与服务端提到的group相对应,也需要与下面的相对应
tx-service-group: ${spring.application.name}-group
# 关闭自动代理
# enable-auto-data-source-proxy: flase
# 服务配置项
service:
# 虚拟组和分组的映射
vgroupMapping:
netty-seata-test-group: default #该配置需要与服务端提到的group相对应
grouplist:
default: 127.0.0.1:8091
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
serverAddr: 127.0.0.1:8848
namespace: 3370d3f0-f011-4c6b-8808-882c1c8e0ed4 # 需要与服务端添加的配置文件相同
group: SEATA_GROUP
username: nacos
password: nacos
file-extension: yml
shared-configs:
- dataId: seata-server.yml # 需要与服务端添加的配置文件相同
namespace: 3370d3f0-f011-4c6b-8808-882c1 # 需要与服务端添加的配置文件相同
group: SEATA_GROUP # 需要与服务端添加的配置文件相同
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
serverAddr: 127.0.0.1:8848
namespace: 3370d3f0-f011-4c6b-8808-882c1 # 需要与服务端添加的配置文件相同
group: SEATA_GROUP # 需要与服务端添加的配置文件相同
username: nacos
password: nacos
file-extension: yml
shared-configs:
- dataId: seata-server.yml # 需要与服务端添加的配置文件相同
namespace: 3370d3f0-f011-4c6b-8808-882c1 # 需要与服务端添加的配置文件相同
group: SEATA_GROUP # 需要与服务端添加的配置文件相同
mybatis-plus:
global-config:
banner: false
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
type-aliases-package: com.lutw.seata
# swagger配置
swagger:
title: Netty-Seata-Test
license: Powered By lutw
licenseUrl: https://www.lutw.cn
enable: true
3、程序中使用
--------------------测试seata-----------------------
@Autowired
private RemoteSysUserService remoteSysUserService;
@Autowired
private RemoteSpSysUserService remoteSpSysUserService;
@GlobalTransactional(rollbackFor = Exception.class)
@Override
public Boolean testSeata() {
SysUser sysUser = new SysUser();
sysUser.setUsername("李四");
sysUser.setPassword("123456");
// 先模拟调用业务(一)
remoteSysUserService.add(sysUser);
SpSysUser spSysUser = new SpSysUser();
spSysUser.setId(1L);
spSysUser.setName("李四");
// 再模拟调用业务(二)
ResponseResult<Boolean> result = remoteSpSysUserService.updateSpSysUserById(spSysUser);
return result.getData();
}
-------------------模拟业务(一)----------------------
@Override
public boolean add(SysUser sysUser) {
return save(sysUser);
}
-------------------模拟业务(二)----------------------
@NettySystemPlatform // 自定义注解,多数据源配置
@Override
public Boolean updateSpSysUserById(SpSysUser spSysUser) {
// 模拟错误,发生异常进行回滚
System.out.println(1/0);
return updateById(spSysUser);
}
4、nacos中seata命名空间下添加配置
以下配置信息,如果不添加,客户端会找不到服务的group,并且会一直报错:
can not get cluster name in registry config ‘service.vgroupMapping.xxx_service_group’, please make sure registry config correct
文件内容如下:
5、创建undo_log表
每个服务对应的业务数据库都需要创建
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) CHARACTER SET utf8 NOT NULL,
`context` varchar(128) CHARACTER SET utf8 NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4;
相关文章
暂无评论...