Seata 环境搭建 - 小码code
source link: https://www.cnblogs.com/jeremylai7/p/16832440.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Seata 环境搭建
在使用微服务中,单体事务注解
@Transactional
就不适用了,需要采用分布式事务解决方案,本文介绍分布式事务Seata
的安装。Seata
一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。
seata
版本: 1.5.2
Seata
是一个分布式事务,seata
服务端也是一个微服务
,需要和其他微服务一样需要注册中心
和配置中心
。同时事务回滚,需要数据库日志记录。
- 注册中心和配置中心:
nacos
- 数据库:
mysql
进入Seata官网下载,下载版本是1.5.2
,找到seata-server-1.5.1.tar.gz
下载。解压文件后进入seata
文件。
新建数据库seata
,然后在seata
文件夹里面的script
文件,找到server
—> db
—> mysql.sql
,在数据库中执行sql
语句:
-- 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_status_gmt_modified` (`status` , `gmt_modified`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
-- 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 = utf8mb4;
-- 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),
`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_status` (`status`),
KEY `idx_branch_id` (`branch_id`),
KEY `idx_xid_and_branch_id` (`xid` , `branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
CREATE TABLE IF NOT EXISTS `distributed_lock`
(
`lock_key` CHAR(20) NOT NULL,
`lock_value` VARCHAR(20) NOT NULL,
`expire` BIGINT,
primary key (`lock_key`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4;
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
全局事务会话由:全局事务、分支事务、全局锁,对应表分别为
global_table
、branch_table
、lock_table
。
3.配置 nacos
在nacos
控制台添加新的命名空间:
添加一条seata
,命名空间ID
在后面需要用到:
3.1 上传配置至Nacos配置中心
进到seata
目录中,找到nacos-config.sh
文件,路径:script
-> config-center
-> nacos
-> nacos-config.sh
。执行nacos-config.sh
脚本:
sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -t xxxx -u username -w password
参数详解:
- -h
nacos
服务IP - -p
nacos
服务端口 - -u
nacos
登录名 - -w
nacos
登录密码 - -g
nacos
配置的分组名称,默认设置SEATA_GROUP
- -t 上一步配置的
命名空间ID
执行脚本之后,输出以下脚本:
Set server.maxCommitRetryTimeout=-1 successfully
Set server.maxRollbackRetryTimeout=-1 successfully
Set server.rollbackRetryTimeoutUnlockEnable=false successfully
Set server.distributedLockExpireTime=10000 successfully
Set server.xaerNotaRetryTimeout=60000 successfully
Set server.session.branchAsyncQueueSize=5000 successfully
Set server.session.enableBranchAsyncRemove=false successfully
Set server.enableParallelRequestHandle=false successfully
Set metrics.enabled=false successfully
Set metrics.registryType=compact successfully
Set metrics.exporterList=prometheus successfully
Set metrics.exporterPrometheusPort=9898 successfully
再去nacos
控制台查看配置:
说明配置上传成功。上传不成功,大部分原因是配置没有配置成功,比如
命名空间ID
没配置正确。
4.修改 appplication.yml
找到appplication.yml
文件,路径为:seata
-> conf
-> application.yml
,以下三个小节分别配置store
、config
、registry
:
4.1 seata.store
seata.store
配置seata
的存储,修改store.mode="db"
:
seata:
store:
# support: file 、 db 、 redis
mode: db
修改数据库连接,将 seata
-> conf
-> application.example.yml
中附带额外配置,将其db
相关配置复制至application.yml
,修改store.db
相关属性。数据库是步骤一
配置的数据库:
seata:
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxx:3306/seata?rewriteBatchedStatements=true
user: xxxx
password: xxx
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
4.2 seata.config
seata.config
是配置nacos配置中心
相关的配置。将seata.config.type
修改成nacos
:
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
然后添加seata.config.nacos
相关的配置:
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group : "SEATA_GROUP"
namespace: "xxxxx"
username: "xxx"
password: "xxx"
其中
namespace
是步骤3中的命名空间ID
。
4.3 seata.registry
seata.registry
是配置注册中心相关字段,将seata
服务作为一个微服务注册到注册中心
。将registry.type
改成nacos
,配置如下:
seata:
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: "seata-server"
serverAddr: 127.0.0.1:8848
group: "SEATA_GROUP"
namespace: "xxxxxx"
username: "xxxx"
password: "xxx"
namespace
也是步骤3中的命名空间ID
。
找到seata
文件中的bin
目录,执行启动命令:
seata-server.sh -h 127.0.0.1 -p 8091 -m db
控制台输出:
apm-skywalking not enabled
seata-server is starting, you can check the /opt/seata/logs/start.out
打开start.out
日志:
系统启动成功,再登录 http://127.0.0.1:7091,就能看到seata
控制台信息。
nacos
控制台服务列表新增了一个服务,说明seata
服务成功注册到了nacos
注册中心:
seata
安装版本是1.5.2
,版本不同,安装流程也可能不同,这里的版本需要保持一致- 执行
sql
创建数据表 - 使用脚本添加配置到
nacos
配置中心 - 修改
application.yml
文件,分别修改store
、config
、registry
相关配置。 - 启动服务,成功登陆
seata
控制台。 - 查看
nacos
控制台,服务列表新增seata
服务。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK