seata注册中心:nacos
seata配置中心:nacos(在配置中心中设置,store = db)
sql语句:
-- -------------------------------- 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(96),
`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;
CREATE TABLE IF NOT EXISTS `undo_log`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) 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 NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
版本信息:
docker: 19.03.13
nacos: 1.4.0
seata: 1.4.0
步骤:
由于我们使用nacos作为配置中心,所以在nacos中创建存放配置信息的命名空间(也可以用默认的)
把seata源码中的config.txt配置文件的内容上传到nacos去
配置文件地址:
https://github.com/seata/seata/blob/1.4.0/script/config-center/config.txt
下载后修改配置内容(也可以上传后在nacos里面去修改):
配置上传脚本地址:
https://github.com/seata/seata/blob/1.4.0/script/config-center/nacos/nacos-config.sh
下载后,上传(注意:nacos-config.sh 要在config.txt的一下层,不然找不到文件,或者自己去修改sh脚本)
在文件夹中 右键,选择 git bash here
复制下面的上传命令执行:
sh nacos-config.sh -h 192.168.1.201 -p 8850 -g SEATA_GROUP -t seata -u nacos -w nacos
# 参数说明:
-h:配置中心地址
-p:配置中心端口
-g:配置中心的分组
-t:配置中心的命名空间id
-u:配置中心naocs用户名
-w:配置中心Naocs密码
执行成功:
下载seata源码
# 创建操作目录
mkdir -p /usr/local/work/docker_compose/redis-cluster
cd /usr/local/work/docker_compose/redis-cluster
# 下载
wget https://github.com/seata/seata/releases/download/v1.4.2/seata-server-1.4.2.tar.gz
# 解压
tar -zxvf seata-server-1.4.2.tar.gz
复制解压后的seata目录的conf/file/conf 和 conf/registry/conf 到 /usr/local/work/docker_compose/redis-cluster/conf
修改registry.conf配置
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
# 使用nacos作为注册中心
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "nacos地址"
group = "SEATA_GROUP"
namespace = ""
cluster = "seata集群名称"
username = "nacos用户名"
password = "nacos密码"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
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
# 使用nacos作为配置中心
type = "nacos"
nacos {
serverAddr = "nacos地址"
namespace = "nacos中配置的命名空间"
group = "SEATA_GROUP"
username = "nacos用户名"
password = "nacos密码"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
apolloAccesskeySecret = ""
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
修改file.conf配置文件(mode如果不改为db的话,在nacos配置中心里面去修改mode值也可以(未测试))
## transaction log store, only used in seata-server
store {
## store mode: file、db、redis
## 修改为db 数据库存储
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## database store property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
## 如果是mysql8 要切换 驱动名称为: com.mysql.cj.jdbc.Driver
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://数据库地址:3306/seata_config"
user = "数据库用户名"
password = "数据库密码"
minConn = 5
maxConn = 100
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
## redis store property
redis {
host = "127.0.0.1"
port = "6379"
password = ""
database = "0"
minConn = 1
maxConn = 10
maxTotal = 100
queryLimit = 100
}
}
编辑docker-compose.yml文件
(版本一)
version: "3"
services:
seata1:
container_name: seata1
image: seataio/seata-server:1.4.2
ports:
- "8091:8091"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker_compose/seata-cluster/8091/logs:/root/logs
environment:
SEATA_PORT: 8091
seata2:
container_name: seata2
image: seataio/seata-server:1.4.2
ports:
- "8092:8091"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker_compose/seata-cluster/8092/logs:/root/logs
environment:
SEATA_PORT: 8091
seata3:
container_name: seata3
image: seataio/seata-server:1.4.2
ports:
- "8093:8091"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker_compose/seata-cluster/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker_compose/seata-cluster/8093/logs:/root/logs
environment:
SEATA_PORT: 8091
启动后效果:
(版本二)
version: "3"
services:
seata1:
container_name: seata1
image: seataio/seata-server:1.4.0
ports:
- "8091:8091"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker/seata/logs/8091:/root/logs
environment:
SEATA_PORT: 8091
SEATA_IP: 192.168.1.201
seata2:
container_name: seata2
image: seataio/seata-server:1.4.0
ports:
- "8092:8092"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker/seata/logs/8092:/root/logs
environment:
SEATA_PORT: 8092
SEATA_IP: 192.168.1.201
seata3:
container_name: seata3
image: seataio/seata-server:1.4.0
ports:
- "8093:8093"
volumes:
- /etc/localtime:/etc/localtime
- /usr/local/work/docker/seata/conf/registry.conf:/seata-server/resources/registry.conf
- /usr/local/work/docker/seata/logs/8093:/root/logs
environment:
SEATA_PORT: 8093
SEATA_IP: 192.168.1.201
seata_port:指定端口
seata_ip:指定容器的ip
启动后效果:
为什么有个版本二?
在一台机器上安装了3个seata的docker 容器,每个容器自己有一个生成的ip,当注册到nacos的时候,也是注册的这个ip + port
这就导致了 自己的seata客户端启动 连接seata服务器的时候,连接的地址是docker内部地址,肯定连接不上,报错
can not connect to 172.25.0.3:8091 cause:can not register RM,err:can not connect to services-server
所以,在版本二中,seata_ip都设置为了宿主机ip(宿主机能ping通,同时放开8091 8092 8093端口),这样就好了
启动
docker-compose up -d
关闭
docker-compose down
客户端配置(不需要在resources目录下加 file.conf 和 registry.conf 文件):
seata:
enabled: true
# Seata 应用编号,默认为 ${spring.application.name}
application-id: ${spring.application.name}
# Seata 事务组编号,用于 TC 集群名
tx-service-group: my_test_tx_group
# 自动代理
enable-auto-data-source-proxy: true
# 服务配置项
service:
vgroup-mapping:
my_test_tx_group: default
config:
type: nacos
nacos:
serverAddr: 192.168.1.201:8850
group: SEATA_GROUP
namespace: seata
registry:
type: nacos
nacos:
application: seata-server
server-addr: 192.168.1.201:8850
namespace:
cluster: default
group: SEATA_GROUP
项目依赖:
<!-- seata 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.4.0</version>
</dependency>
ps: 上面 有可能会报错:
Caused by: io.seata.common.exception.ShouldNeverHappenException: Can't find any object of class org.springframework.context.ApplicationContext
换成下面的依赖即可
<!-- seata 依赖 -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.0</version>
</dependency>
完成,如有错误,请指出~~~