欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

SpringCloudAlibaba之集成seata1.4.2+nacos(docker部署)

时间:2023-05-20
一、前提版本说明: 1、springboot版本pom

org.springframework.boot spring-boot-starter-parent 2.2.4.RELEASE

2、nacos版本

        2.1、server版本:1.4.0 ps:我用的比较老 反正没有出现什么问题

        2.2、client项目引入:

<--项目从nacos动态读取配置需要引入--> com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 2.2.6.RELEASE <--项目注册到nacos需要引入--> com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 2.2.6.RELEASE

3、seata版本

        3.1、seata 服务端版本:1.4.2

        3.2、client项目引入:

com.alibaba.cloud spring-cloud-starter-alibaba-seata 2.2.6.RELEASE

二、进入正文 安装nacos

进入服务器 使用 :docker安装 seata

安装步骤:

1、使用docker 拉取 seata 镜像

docker pull seataio/seata-server:1.4.2

2、启动镜像: 

docker run -d --name seata-server -e SEATA_IP=39.xx.xx.xx -e SEATA_PORT=8091 -p 8091:8091 seataio/seata-server:1.4.2

ps:

--name seata-server     注册到nacos的话,服务名称就是seata-server,个人理解相当于springboot 配置中的(spring.application.name=product)欢迎指正。

SEATA_IP=39.xx.xx.xx  服务器的外网ip

-e SEATA_PORT=8091 seata的服务端口

-p 8091:8091                  宿主机与容器的端口映射

3、查看运行容器

docker ps

红色框内 container id 后面拷贝会用到

ps:如果出现seata-server 代表启动成功

4、修改seata配置文件 4.1、拷贝容器内文件file.conf到容器外的/usr/local/seata径下  

docker cp 2177b74a2b84:/seata-server/resources/file.conf /usr/local/seata/file.conf

4.2、拷贝容器内文件registry.conf到容器外的/usr/local/seata径下

docker cp 2177b74a2b84:/seata-server/resources/registry.conf /usr/local/seata/registry.conf

ps:将 seata的两个配置文件复制到宿主机文件里面-便于更改

4.3、进入 /usr/local/seata文件夹

cd /usr/local/seata

5、更改 registry.conf  重点!!!是1.4.2版本更新的新东西(下面有需要修改的内容以及,各个属性的说明。我真是贴心.....)

5.1、registry.conf文件全部内容:

registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = "nacos" nacos { application = "seata-server" serverAddr = "xx.xx.xx.xx:8848" group = "test" namespace = "57b60655-ab37-48dd-9ab7-xx3c28aa8ab4" cluster = "default" 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" aclToken = "" } 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 = "nacos" nacos { serverAddr = "xx.xx.xx.xx:8848" namespace = "57b60655-ab37-48dd-9ab7-xx3c28aa8ab4" group = "test" username = "nacos" password = "nacos" dataId = "seataServer.properties" } consul { serverAddr = "127.0.0.1:8500" aclToken = "" } apollo { appId = "seata-server" ## apolloConfigService will cover apollometa apollometa = "http://192.168.1.204:8801" apolloConfigService = "http://192.168.1.204:8080" namespace = "application" apolloAccesskeySecret = "" cluster = "seata" } zk { serverAddr = "127.0.0.1:2181" sessionTimeout = 6000 connectTimeout = 2000 username = "" password = "" nodePath = "/seata/seata.properties" } etcd3 { serverAddr = "http://localhost:2379" } file { name = "file.conf" }}

registry.conf文件第一部分修改内容 registry.nacos属性说明:

registry.type =nacos       意思是将服务注册到nacos(registry.type指定nacos后就只设置registry.nacos中的属性就好了)

registry.nacos.application= "seata-server"          seata在nacos中的服务名称

registry.nacos.serverAddr= ""xx.xx.xx.xx:8848"   nacos的访问地址|端口

registry.nacos.group= "test"                                  下面有图

registry.nacos. namespace = "57b60655-ab37-48dd-9ab7-xxxc28aa8ab4"  下面有图

registry.nacos. cluster = "default"                          默认就好了 

registry.nacos.username = "nacos"                       nacos的登录账号

registry.nacos. password = "nacos"                       nacos的登录密码

---------------------------------------------------------------------------------------------------------------------

registry.conf文件第二部分修改内容 属性说明

config.type ="nacos"  seata的配置从nacos读取,config.type ="nacos"  那就只设置config.nacos中的属性就好了

其他 参考registry.nacos (步骤5.1) 中的nacos配置

group = "test"
namespace = "57b60655-ab37-48dd-9ab7-853c28aa8ab4"

dataId = "seataServer.properties"  这个很重要 代表从nacos中 

分组group = "test"   ; 命名空间namespace = "57b60655-ab37-48dd-9ab7-853c28aa8ab4"   seataServer.properties 配置中读取配置

6、更改 file.conf  (后面有更改内容,以及说明)

file.conf文件全部内容:

## transaction log store, only used in seata-serverstore { ## store mode: file、db、redis mode = "db" ## rsa decryption public key publicKey = "" ## 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" driverClassName = "com.mysql.cj.jdbc.Driver" ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param url = "jdbc:mysql://xx.xx.xx.xx:3306/seata?useSSL=true&serverTimezone=GMT" user = "seata" password = "seata" minConn = 5 maxConn = 100 globalTable = "global_table" branchTable = "branch_table" lockTable = "lock_table" queryLimit = 100 maxWait = 5000 } ## redis store property redis { ## redis mode: single、sentinel mode = "single" ## single mode property single { host = "127.0.0.1" port = "6379" } ## sentinel mode property sentinel { masterName = "" ## such as "10.28.235.65:26379,10.28.235.65:26380,10.28.235.65:26381" sentinelHosts = "" } password = "" database = "0" minConn = 1 maxConn = 10 maxTotal = 100 queryLimit = 100 }}

根据个人理解 在registry.conf中的 registry.conf.type=nacos时 其实不用设置这个文件-仅仅个理解,没有去验证,也没有找到说明相关问题的文章。。。。。(得到了证实,只有registry.conf.type = file的时候才会读取这个文件)

---------------------------------------------------------------------------------------------------------

store.mode=db         意思是 seata在使用过程中开启事务时会产生临时数据以及回滚数据 使用数据库保存( 设置了 store.mode=db之后 只设置store.db的属性就可以了)

store.db.datasource = "druid"        

store.db.dbType = "mysql"               

store.db. driverClassName = "com.mysql.cj.jdbc.Driver"    

store.db.url = "jdbc:mysql://xx.xx.xx.xx:3306/seata?useSSL=true&serverTimezone=GMT"

                                            xx.xx.xx.xx:3306/seata        替换成自己的数据库路径

store.db.user = "seata"                                                        数据库账号

store.db. password = "seata"                                              数据库密码

其他的根据需要更改.......

注意:

store.db. driverClassName = "com.mysql.cj.jdbc.Driver" 我使用的数据库是mysql8如果使用mysql5的话 改成的 store.db. driverClassName = "com.mysql.jdbc.Driver"

-----------------------------------------------------------

7、将修改好的文件复制回docker容器内 

docker cp file.conf 2177b74a2b84:/seata-server/resources/file.confdocker cp registry.conf 2177b74a2b84:/seata-server/resources/registry.conf

8、上传seata配置文件到nacos上传配置

这一步的目的说明:

docker中运行的 seata-server 会从nacos中读取配置 ,如果想修改seata的配置就从nacos修改,为了方便

创建配置时要与registry.conf文件中的

----------重点-------------

registry.conf.group = "test";

registry.conf.namespace =  ""57b60655-ab37-48dd-9ab7-xx3c28aa8ab4"";

dataId = "seataServer.properties"

值一定要 一 一对应起来

----------重点-------------

8.1、nacos和seata配置文件说明 8.2、seataServer.properties配置 8.3、 配置内容

client.tm.defaultGlobalTransactionTimeout=60000client.tm.degradeCheck=falseclient.tm.degradeCheckAllowTimes=10client.tm.degradeCheckPeriod=2000client.tm.interceptorOrder=-2147482648store.mode=dbstore.lock.mode=dbstore.session.mode=filestore.publicKey=store.file.dir=file_store/datastore.file.maxBranchSessionSize=16384store.file.maxGlobalSessionSize=512store.file.fileWriteBufferCacheSize=16384store.file.flushDiskMode=asyncstore.file.sessionReloadReadSize=100store.db.datasource=druidstore.db.dbType=mysqlstore.db.driverClassName=com.mysql.cj.jdbc.Driverstore.db.url=jdbc:mysql://xx.xx.xx.xx.com:3306/seata?useSSL=true&serverTimezone=GMTstore.db.user=seatastore.db.password=seatastore.db.minConn=5store.db.maxConn=30store.db.globalTable=global_tablestore.db.branchTable=branch_tablestore.db.distributedLockTable=distributed_lockstore.db.queryLimit=100store.db.lockTable=lock_tablestore.db.maxWait=5000store.redis.mode=singlestore.redis.single.host=127.0.0.1store.redis.single.port=6379store.redis.sentinel.masterName=store.redis.sentinel.sentinelHosts=store.redis.maxConn=10store.redis.minConn=1store.redis.maxTotal=100store.redis.database=0store.redis.password=store.redis.queryLimit=100server.recovery.committingRetryPeriod=1000server.recovery.asynCommittingRetryPeriod=1000server.recovery.rollbackingRetryPeriod=1000server.recovery.timeoutRetryPeriod=1000server.maxCommitRetryTimeout=-1server.maxRollbackRetryTimeout=-1server.rollbackRetryTimeoutUnlockEnable=falseserver.distributedLockExpireTime=10000client.undo.dataValidation=trueclient.undo.logSerialization=jacksonclient.undo.onlyCareUpdateColumns=trueserver.undo.logSaveDays=7server.undo.logDeletePeriod=86400000client.undo.logTable=undo_logclient.undo.compress.enable=trueclient.undo.compress.type=zipclient.undo.compress.threshold=64klog.exceptionRate=100transport.serialization=seatatransport.compressor=nonemetrics.enabled=falsemetrics.registryType=compactmetrics.exporterList=prometheusmetrics.exporterPrometheusPort=9898tcc.fence.logTableName=tcc_fence_logtcc.fence.cleanPeriod=1hserver.session.branchAsyncQueueSize=5000server.session.enableBranchAsyncRemove=trueservice.vgroupMapping.default_tx_group=defaultservice.vgroupMapping.uat_tx_group=defaultservice.vgroupMapping.test_tx_group=defaultservice.vgroupMapping.prod_tx_group=default

说明:

service.vgroupMapping.default_tx_group=default
service.vgroupMapping.uat_tx_group=default
service.vgroupMapping.test_tx_group=default
service.vgroupMapping.prod_tx_group=default

这几个配置分别对应不同的项目环境使用的事务组;配置内容很重要

事务组说明链接

9、重启 seata

docker restart 2177b74a2b84

10、创建表         10.1、将你在file.conf的中设置的数据库中创建3张表

CREATE TABLE `lock_table` ( `row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `transaction_id` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL, `branch_id` mediumtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL, `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `gmt_create` datetime(0) NULL DEFAULT NULL, `gmt_modified` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`row_key`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '存储锁表' ROW_FORMAT = Dynamic;CREATE TABLE `global_table` ( `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `transaction_id` bigint(20) NULL DEFAULT NULL, `status` tinyint(4) NOT NULL, `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `timeout` int(11) NULL DEFAULT NULL, `begin_time` bigint(20) NULL DEFAULT NULL, `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `gmt_create` datetime(0) NULL DEFAULT NULL, `gmt_modified` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`xid`) USING BTREE, INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE, INDEX `idx_transaction_id`(`transaction_id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '分支事务表' ROW_FORMAT = Dynamic;CREATE TABLE `branch_table` ( `branch_id` bigint(20) NOT NULL, `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `transaction_id` bigint(20) NULL DEFAULT NULL, `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `lock_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `status` tinyint(4) NULL DEFAULT NULL, `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `gmt_create` datetime(0) NULL DEFAULT NULL, `gmt_modified` datetime(0) NULL DEFAULT NULL, PRIMARY KEY (`branch_id`) USING BTREE, INDEX `idx_xid`(`xid`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '全局事务表' ROW_FORMAT = Dynamic;

        10.2在你项目里连接的数据库中创建一张表

CREATE TABLE `undo_log` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `branch_id` bigint(20) NOT NULL, `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `rollback_info` longblob NOT NULL, `log_status` int(11) NOT NULL, `log_created` datetime(0) NOT NULL, `log_modified` datetime(0) NOT NULL, `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

三、项目内配置文件: 1、yml配置

这是application-dev.yml 

seata.service.vgroupMapping.default_tx_group: defaultseata.tx-service-group: default_tx_group 必须是一个值如果uat环境可以更改值--前提必须 seataServer.properties 文件内有这个值(8.3步骤后面红色说明!)seata.service.vgroupMapping.uat_tx_group: defaultseata.tx-service-group: uat_tx_group

#分布式事务 seata 配置seata: service: vgroupMapping: default_tx_group: default #default_tx_group分组名称必须要跟tx-service-group的值一样 #指定事务分组 分组必须在 nacos seataServer.properties配置中有 tx-service-group: default_tx_group config: type: nacos nacos: #nacos地址 serverAddr: xx.xx.xx.xx:8848 group: test namespace: 57b60655-ab37-48dd-9ab7-853c28aa8ab4 userName: nacos password: nacos #读取配置文件的 dataId 重要!!!! dataId: "seataServer.properties" registry: type: nacos nacos: application: seata-server server-addr: xx.xx.xx.xx:8848 group: test namespace: 57b60655-ab37-48dd-9ab7-853c28aa8ab4 userName: nacos password: nacos

2、@GlobalTransactional 添加注解开启事务

@RestController@RequestMapping("/locationTest")public class LocationTest { @RequestMapping("/testEmail") @UnLogin @GlobalTransactional public int testEmail() { System.out.println(1); return 1; }}

3、测试事务开启

 

4、docker内seata日志文件打印正常

查看日志命令:docker logs 2177b74a2b84(容器id-container Id) 

 5、数据库配置的全局事务id保存

作者说明:

当做笔记写的,如有不对的地方,欢迎批评指正

项目路径:alibaba-cloud-demo: 微服务demo

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。