当前位置:首页 » 《随便一记》 » 正文

Docker单点部署Seata(2.0.0) + Nacos(v2.3.0) + Mysql(5.7)

9 人参与  2024年03月04日 08:26  分类 : 《随便一记》  评论

点击全文阅读


文章目录

一、部署Nacos二、部署Mysql三、Seata准备工作1. 记住nacos、mysql、宿主机的ip2. 建立数据库3. Nacos远程配置文件 四、部署Seata五、初步检验Seata部署情况六、微服务使用Seata1.引入依赖2. application.yml配置 七、遇到的坑1. Nacos显示Seata服务的ip为容器内网ip导致微服务无法访问2. 使用host宿主机网络3. seata The distribute lock table is not config, please create the target table and config it4. 高版本中BusinessActionContextParameter和TwoPhaseBusinessAction推荐都放在实现类中,接口上的做法后续将会废除

系统环境
docker desktop for windows v4.23.0
nacosmysqlseata三者都在bridge网络中

一、部署Nacos

docker run -itd \-e MODE=standalone -e NACOS_SERVER_PORT=8848 -p 8848:8848 --name=nacos_standalone nacos/nacos-server:v2.3.0

二、部署Mysql

docker run -itd \-e MYSQL_ROOT_PASSWORD=1009-p 3306:3306--name=mysql_itcastmysql:5.7

三、Seata准备工作

1. 记住nacos、mysql、宿主机的ip

$ docker network inspect bridge

假设这里nacos172.17.0.3mysql172.17.0.2

ipconfig /all

这里假设宿主机ip为192.168.1.102

之后遇到上述三个ip,记得写成自己的

2. 建立数据库

mysql_itcast中新建seata数据库,然后导入以下脚本

-- -------------------------------- The script used when storeMode is 'db' ---------------------------------- the table to store GlobalSession dataCREATE 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 dataCREATE 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 dataCREATE 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` (`xid`)) 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);

3. Nacos远程配置文件

访问Nacos网页,一般是http://localhost:8848/nacos/,新建一个配置seataServer.properties
在这里插入图片描述
具体内容如下:

store.mode=db#-----db-----store.db.datasource=druidstore.db.dbType=mysql# 需要根据mysql的版本调整driverClassName# mysql8及以上版本对应的driver:com.mysql.cj.jdbc.Driver# mysql8以下版本的driver:com.mysql.jdbc.Driverstore.db.driverClassName=com.mysql.jdbc.Driverstore.db.url=jdbc:mysql://172.17.0.2:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=falsestore.db.user= rootstore.db.password=1009# 数据库初始连接数store.db.minConn=1# 数据库最大连接数store.db.maxConn=20# 获取连接时最大等待时间 默认5000,单位毫秒store.db.maxWait=5000# 全局事务表名 默认global_tablestore.db.globalTable=global_table# 分支事务表名 默认branch_tablestore.db.branchTable=branch_table# 全局锁表名 默认lock_tablestore.db.lockTable=lock_tablestore.db.distributedLockTable=distributed_lock# 查询全局事务一次的最大条数 默认100store.db.queryLimit=100# undo保留天数 默认7天,log_status=1(附录3)和未正常清理的undoserver.undo.logSaveDays=7# undo清理线程间隔时间 默认86400000,单位毫秒server.undo.logDeletePeriod=86400000# 二阶段提交重试超时时长 单位ms,s,m,h,d,对应毫秒,秒,分,小时,天,默认毫秒。默认值-1表示无限重试# 公式: timeout>=now-globalTransactionBeginTime,true表示超时则不再重试# 注: 达到超时时间后将不会做任何重试,有数据不一致风险,除非业务自行可校准数据,否者慎用server.maxCommitRetryTimeout=-1# 二阶段回滚重试超时时长server.maxRollbackRetryTimeout=-1# 二阶段提交未完成状态全局事务重试提交线程间隔时间 默认1000,单位毫秒server.recovery.committingRetryPeriod=1000# 二阶段异步提交状态重试提交线程间隔时间 默认1000,单位毫秒server.recovery.asynCommittingRetryPeriod=1000# 二阶段回滚状态重试回滚线程间隔时间  默认1000,单位毫秒server.recovery.rollbackingRetryPeriod=1000# 超时状态检测重试线程间隔时间 默认1000,单位毫秒,检测出超时将全局事务置入回滚会话管理器server.recovery.timeoutRetryPeriod=1000

四、部署Seata

宿主机新建一个application.yml文件,内容如下

server:  port: 7091spring:  application:    name: seata-serverlogging:  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_logstashconsole:  user:    username: seata    password: seataseata:  config:    # support: nacos, consul, apollo, zk, etcd3    type: nacos    nacos:      server-addr: 172.17.0.3:8848      namespace:      group: DEFAULT_GROUP      username: nacos      password: nacos      data-id: seataServer.properties  registry:    # support: nacos, eureka, redis, zk, consul, etcd3, sofa    type: nacos    nacos:      application: seata-tc-server      server-addr: 172.17.0.3:8848      group: DEFAULT_GROUP      namespace:      # tc集群名称      cluster: SH      username: nacos      password: nacos  #  server:  #    service-port: 8091 #If not configured, the default is '${server.port} + 1000'  security:    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017    tokenValidityInMilliseconds: 1800000    ignore:      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

然后用以下命令运行seata容器

docker run --name seata-server \-itd \        -p 8091:8091 \        -p 7091:7091 \        -e STORE_MODE=db \        -e SEATA_IP="192.168.1.102" \        -e SEATA_PORT=8091 \        -v "path/to/application.yml:/seata-server/resources/application.yml" \        seataio/seata-server:2.0.0

五、初步检验Seata部署情况

访问Seata网页,这里是http://192.168.1.102:7091/,输入两个seata后进入系统。
在这里插入图片描述
Nacos网页上查看Seata服务详情,ip为宿主机ip,不要是docker容器内网ip就行。
在这里插入图片描述

六、微服务使用Seata

1.引入依赖

        <!--seata-->        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>            <exclusions>                <!--版本较低,1.3.0,因此排除-->                <exclusion>                    <artifactId>seata-spring-boot-starter</artifactId>                    <groupId>io.seata</groupId>                </exclusion>            </exclusions>        </dependency>        <!--seata starter 采用1.4.2版本-->        <dependency>            <groupId>io.seata</groupId>            <artifactId>seata-spring-boot-starter</artifactId>            <version>1.4.2</version>        <!--2.0.0貌似有点问题,TCC模式BusinessActionContextParameter无效-->        </dependency>

2. application.yml配置

seata:  registry:    type: nacos    nacos: # tc      server-addr: localhost:8848      namespace: ""      group: DEFAULT_GROUP      application: seata-tc-server # tc服务在nacos中的服务名称      cluster: SH      username: nacos      password: nacos  tx-service-group: seata-demo # 事务组,根据这个获取tc服务的cluster名称  service:    vgroup-mapping: # 事务组与TC服务cluster的映射关系      seata-demo: SH

启动微服务后,除了可以看微服务的日志外,还可以看Seata容器日志,出现类似以下日志即为正常

2023-12-30 21:37:35 digest=seata-demo,192.168.222.1,17039434536432023-12-30 21:37:35 timestamp=17039434536432023-12-30 21:37:35 authVersion=V42023-12-30 21:37:35 vgroup=seata-demo2023-12-30 21:37:35 ip=192.168.222.12023-12-30 21:37:35 '},channel:[id: 0x7f82356a, L:/172.17.0.4:8091 - R:/172.17.0.1:35092],client version:2.0.02023-12-30 21:37:36 21:37:36.389  INFO --- [rverHandlerThread_1_6_500] [rocessor.server.RegRmProcessor] [      onRegRmMessage]  [] : RM register success,message:RegisterRMRequest{resourceIds='jdbc:mysql://localhost:3306/seata_demo', version='2.0.0', applicationId='order-service', transactionServiceGroup='seata-demo', extraData='null'},channel:[id: 0x3a9f4e29, L:/172.17.0.4:8091 - R:/172.17.0.1:35096],client version:2.0.0

七、遇到的坑

1. Nacos显示Seata服务的ip为容器内网ip导致微服务无法访问

网上看到以下各种方法均无效

使用host网络application.yml指定spring.cloud.nacos.discovery.ip

以下方法有效

容器创建时使用 -e SEATA_IP="宿主机ip"

2. 使用host宿主机网络

一开始为了图方便,给Nacos用过host网络,结果容器程序运行正常,打不开网页,玄学的一批。
也给Seata使用host网络,为了配置文件里面不用自己手动查询nacos和mysql的ip,结果然并卵。

3. seata The distribute lock table is not config, please create the target table and config it

这个是因为很多文档,都只有3张表,少了一张。
官方文档说store.db.distributedLockTable1.5.1版本新增的参数。
https://seata.io/zh-cn/docs/user/configurations
但是很多文档和博客,都只有3张表,第4张在哪里呢?
在这里
https://seata.io/zh-cn/docs/ops/deploy-by-docker-compose.html
里面写到nacos注册中心,db存储时会提供 [建表脚本]
以及最后最重要的是,要在Nacos配置中心配置seataServer.properties时,要多加一行

store.db.distributedLockTable=distributed_lock

这点在官网文档都没有提及。

4. 高版本中BusinessActionContextParameter和TwoPhaseBusinessAction推荐都放在实现类中,接口上的做法后续将会废除

具体参考这个Issue
2.0.0 TCC模式@BusinessActionContextParameter修饰的参数失效,无法在BusinessActionContext获取


点击全文阅读


本文链接:http://zhangshiyu.com/post/73283.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1