根据阿昌的版本,我的nacos是1.4.2的版本,所以我选择seata1.3.0的版本

Java敏捷开发面试 java敏捷开发模式_Java敏捷开发面试

1、添加 UNDO_LOG 表

每一个要使用分布式事务的数据库都需要一个 UNDO_LOG 表。

CREATE TABLE undo_log (
id bigint(20) NOT NULL AUTO_INCREMENT,
branch_id bigint(20) NOT NULL,
xid varchar(100) NOT NULL,
context varchar(128) 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) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid,branch_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2、配置seata

  • D:\application\seata\seata1.3.0\seata\conf\registry.conf

我们这里使用的是nacos作为注册中心,所以就去配置nacos

Java敏捷开发面试 java敏捷开发模式_开发语言_02

下面的配置内容我们也可以使用对应的服务,但是我们这里使用本地file文件配置

Java敏捷开发面试 java敏捷开发模式_Java_03

  • D:\application\seata\seata1.3.0\seata\conf\file.conf

本地文件配置内容,

下面是transaction log store,选择持久化到哪里,我们这里就使用本地文件持久化

如果是选择其他的就配置对应的配置

Java敏捷开发面试 java敏捷开发模式_Java_04

  • 配置完,我们就可以启动我们的seata了

D:\application\seata\seata1.3.0\seata\bin

Java敏捷开发面试 java敏捷开发模式_开发语言_05

  • 访问nacos注册中心,查看是否注册成功

Java敏捷开发面试 java敏捷开发模式_d3_06


3、导入依赖
• achangmall-common/pom.xml
com.alibaba.cloud
spring-cloud-starter-alibaba-seata
4、代理数据源
因为seata需要对数据源进行代理,所以我们需要用seata来封装代理我们的数据源
• achangmall-order、achangmall-product、achangmall-ware
@Configuration
public class SeataConfig {
@Autowired
DataSourceProperties dataSourceProperties;
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties){
HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
if (StringUtils.hasText(dataSourceProperties.getName())) {
dataSource.setPoolName(dataSourceProperties.getName());
}
return new DataSourceProxy(dataSource);
}
}
5、添加配置文件
每个要使用分布式事务的微服务服务中都要添加这两个文件
registry.conf
registry {
file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = “nacos”
nacos {
serverAddr = “127.0.0.1:8848”
namespace = “public”
cluster = “default”
}
eureka {
serviceUrl = “http://localhost:1001/eureka”
application = “default”
weight = “1”
}
redis {
serverAddr = “localhost:6379”
db = “0”
}
zk {
cluster = “default”
serverAddr = “127.0.0.1:2181”
session.timeout = 6000
connect.timeout = 2000
}
consul {
cluster = “default”
serverAddr = “127.0.0.1:8500”
}
etcd3 {
cluster = “default”
serverAddr = “http://localhost:2379”
}
sofa {
serverAddr = “127.0.0.1:9603”
application = “default”
region = “DEFAULT_ZONE”
datacenter = “DefaultDataCenter”
cluster = “default”
group = “SEATA_GROUP”
addressWaitTime = “3000”
}
file {
name = “file.conf”
}
}
config {
file、nacos 、apollo、zk、consul、etcd3
type = “file”
nacos {
serverAddr = “localhost”
namespace = “public”
cluster = “default”
}
consul {
serverAddr = “127.0.0.1:8500”
}
apollo {
app.id = “seata-server”
apollo.meta = “http://192.168.1.204:8801”
}
zk {
serverAddr = “127.0.0.1:2181”
session.timeout = 6000
connect.timeout = 2000
}
etcd3 {
serverAddr = “http://localhost:2379”
}
file {
name = “file.conf”
}
}
file.conf
vgroup_mapping需要修改
举例:
在vgroup_mapping.后面追加你的服务名achangmall-ware再加-fescar-service-group
transport {
tcp udt unix-domain-socket
type = “TCP”
#NIO NATIVE
server = “NIO”
#enable heartbeat
heartbeat = true
#thread factory for netty
thread-factory {
boss-thread-prefix = “NettyBoss”
worker-thread-prefix = “NettyServerNIOWorker”
server-executor-thread-prefix = “NettyServerBizHandler”
share-boss-worker = false
client-selector-thread-prefix = “NettyClientSelector”
client-selector-thread-size = 1
client-worker-thread-prefix = “NettyClientWorkerThread”
netty boss thread size,will not be used for UDT
boss-thread-size = 1
#auto default pin or 8
worker-thread-size = 8
}
shutdown {
when destroy server, wait seconds
wait = 3
}
serialization = “seata”
compressor = “none”
}
service {
#vgroup->rgroup
#改这里对应你的服务模块名家伙是哪个 -fescar-service-group
vgroup_mapping.gulimall-ware-fescar-service-group = “default”
#only support single node
default.grouplist = “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”
}
client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
report.retry.count = 5
}
transaction log store
store {
store mode: file、db
mode = “file”
file store
file {
dir = “sessionStore”
branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
max-branch-session-size = 16384
globe session size , if exceeded throws exceptions
max-global-session-size = 512
file buffer size , if exceeded allocate new buffer
file-write-buffer-cache-size = 16384
when recover batch read size
session.reload.read_size = 100
async, sync