docker 部署分布式事务框架 seata1.5.2
1 可以参考官方文档:
https://seata.apache.org/zh-cn/docs/ops/deploy-by-docker
https://seata.apache.org/zh-cn/docs/ops/deploy-ha/
版本:
seata-server1.5.2
版本说明:
配置文件:
seata 从1.5开始配置的目录结构就变为了application.yml的形式,将1.4版本的 file.conf和registry.conf配置合并了
和spring cloud alibaba的对应关系
我们在安装部署的时候,对版本也要留意一下和spring cloud alibaba的对应关系,版本不对可能会有问题
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
2 docker部署
2.1 启动容器
#拉取镜像 docker pull seataio/seata-server:1.5.2 #新建本地目录 mkdir -p /mydata/seata/config mkdir -p /mydata/seata/sessionStore # 启动临时容器 docker run -d -p 8091:8091 -p 7091:7091 --name seata-server seataio/seata-server:1.5.2 # 查看启动日志 docker logs -f seata-server # 启动成功后,拷贝临时容器中的配置到宿主机 docker cp seata-server:/seata-server/resources /mydata/seata/ # 删除临时容器 docker rm -f seata-server # 启动seata docker run -d --restart=always --name seata-server \ -p 8091:8091 \ -p 7091:7091 \
-e SEATA_IP=192.168.56.22 \ -v /mydata/seata/resources:/seata-server/resources \ -v /mydata/seata/sessionStore:/seata-server/sessionStore \ seataio/seata-server:1.5.2
-e SEATA_IP= 这里指定的是seata宿主机的地址,seata的docker容器的宿主机
2.2 修改配置:
1.5开始使用application.yml
application.yml 这个可以参考同级目录下的application.example.yml
1 配置注册和发现使用 nacos
修改 nacos 的地址和登录信息
2 数据库使用 mysql
修改数据库连接信息
server: port: 7091 spring: application: name: seata-server logging: config: classpath:logback-spring.xml file: path: ${user.home}/logs/seata extend: logstash-appender: destination: 127.0.0.1:4560 kafka-appender: bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstash console: user: username: admin password: admin seata: config: # 将type修改为nacos 并且修改下面nacos的登陆信息
# support: nacos 、 consul 、 apollo 、 zk 、 etcd3 type: nacos nacos: server-addr: 192.168.56.30:8848 group: SEATA_GROUP username: nacos password: nacos data-id: seataServer.properties registry: # 将type修改为nacos 并且修改下面的nacos的登陆信息
# support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa type: nacos preferred-networks: 30.240.* nacos: application: seata-server server-addr: 192.168.56.30:8848 group: SEATA_GROUP cluster: default username: nacos password: nacos server: service-port: 8091 #If not configured, the default is '${server.port} + 1000' max-commit-retry-timeout: -1 max-rollback-retry-timeout: -1 rollback-retry-timeout-unlock-enable: false enableCheckAuth: true retryDeadThreshold: 130000 recovery: committing-retry-period: 1000 asyn-committing-retry-period: 1000 rollbacking-retry-period: 1000 timeout-retry-period: 1000 undo: log-save-days: 7 log-delete-period: 86400000 store: # 将mode修改为db使用数据库,并且修改db:下面的数据库配置信息,数据库名称是 seata,用户名root,密码root
# support: file 、 db 、 redis mode: db session: mode: file lock: mode: file file: dir: sessionStore max-branch-session-size: 16384 max-global-session-size: 512 file-write-buffer-cache-size: 16384 session-reload-read-size: 100 flush-disk-mode: async db: datasource: druid db-type: mysql driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://192.168.56.30:3306/seata?rewriteBatchedStatements=true user: root password: root 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 metrics: enabled: false registry-type: compact exporter-list: prometheus exporter-prometheus-port: 9898 security: secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017 tokenValidityInMilliseconds: 1800000 ignore: urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
2.3 新建数据库表
2.3.1 新建数据库
先在数据库中新建 seata 数据库,在前面配置的名称
2.3.2 新建表
db模式需要在数据库创建 global_table
, branch_table
, lock_table
表
高可用部署
https://seata.apache.org/zh-cn/docs/ops/deploy-ha/
数据库建表语句支持:mysql,oracle,postgresql
https://github.com/apache/incubator-seata/tree/develop/script/server/db
使用mysql.sql建表
https://github.com/apache/incubator-seata/blob/v1.5.2/script/server/db/mysql.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_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);
2.4 重启容器
以上配置完成后重启 seata-server容器
#重启容器 docker restart seata-server #查看启动日志 docker logs -f seata-server
2.5 查看
启动成功后,登录页面
密码:admin
在nacos中查看
是否有名称为 seata-server 的服务注册成功
3 微服务项目中整合使用seata
不同模式的建表语句:
at, saga, tcc
这里使用常用的AT模式建undo_log的语句如下:
-- for AT mode you must to init this sql for you business database. the seata server not need it. CREATE TABLE IF NOT EXISTS `undo_log` ( `branch_id` BIGINT NOT NULL COMMENT 'branch transaction id', `xid` VARCHAR(128) 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(6) NOT NULL COMMENT 'create datetime', `log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime', UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';