docker部署seata1.6.0
Seata 是 阿里巴巴 开源的 分布式事务中间件,解决 微服务 场景下面临的分布式事务问题。需要先搭建seata服务端然后与springcloud的集成以实现分布式事务控制的过程 ,项目中只需要在远程调用APi服务的方法上使用注解 @GlobalTransactional,就能实现跨模块事务回滚。
一、安装seata1.6.0
docker pull seataio/seata-server:1.6.0
# 启动容器
docker run -d --name seata-server -p 8091:8091 -p 7091:7091 seataio/seata-server:1.6.0
# 拷贝文件到宿主机目录/opt/seata/seata-server(这个目录需要先手动创建)
cd /opt
mkdir seata
cd /opt/seata
mkdir seata-server
docker cp seata-server:/seata-server/resources /opt/seata/seata-server/
# 停止并删除容器
docker stop seata-server
docker rm seata-server
# 修改/opt/seata/seata-server/resources目录下的文件application.yml
# 重新启动容器
docker run -i -t -d --restart always --name seata-server \
-p 7091:7091 -p 8091:8091 \
-e SEATA_IP=192.168.124.11 \
-e SEATA_PORT=8091 \
-e TZ=Asia/Shanghai \
-v /opt/seata/seata-server/resources:/seata-server/resources \
-v /etc/localtime:/etc/localtime \
seataio/seata-server:1.6.0# -e SEATA_IP=[公网IP] 这个ip必须是公网ip,否则会访问失败
二、修改配置文件application.yml
- 修改控制台登录账号密码
console:user:username: seata11password: 123456
- 修改type=nacos
- 修改nacos连接的账号密码
- 修改数据库连接
# Copyright 1999-2019 Seata.io Group.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.server:port: 7091spring:application:name: seata-serverlogging:config: classpath:logback-spring.xmlfile:path: ${user.home}/logs/seataextend:logstash-appender:destination: 127.0.0.1:4560kafka-appender:bootstrap-servers: 127.0.0.1:9092topic: logback_to_logstashconsole:user:username: seata11password: 123456
seata:config:# support: nacos, consul, apollo, zk, etcd3type: nacosnacos:server-addr: 192.168.124.11:8848namespace: eca990b0-5481-4f9e-b4d0-f2a517bb26778group: SEATA_GROUPusername: rootpassword: 123456#context-path:##if use MSE Nacos with auth, mutex with username/password attribute#access-key: ""#secret-key: ""data-id: seataServer.propertiesregistry:# support: nacos, eureka, redis, zk, consul, etcd3, sofatype: nacospreferred-networks: 30.240.*nacos:application: seata-serverserver-addr: 192.168.124.11:8848group: SEATA_GROUPnamespace: eca990b0-5481-4f9e-b4d0-f2a517bb26778cluster: defaultusername: rootpassword: 123456#context-path:##if use MSE Nacos with auth, mutex with username/password attribute#access-key: ""#secret-key: ""store:# support: file 、 db 、 redismode: dbsession:mode: dblock:mode: dbfile:dir: sessionStoremax-branch-session-size: 16384max-global-session-size: 512file-write-buffer-cache-size: 16384session-reload-read-size: 100flush-disk-mode: asyncdb:datasource: druiddb-type: mysqldriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://192.168.124.11:3306/seata_db?rewriteBatchedStatements=trueuser: rootpassword: 123456min-conn: 5max-conn: 100global-table: global_tablebranch-table: branch_tablelock-table: lock_tabledistributed-lock-table: distributed_lockquery-limit: 100max-wait: 5000server:service-port: 8091 #If not configured, the default is '${server.port} + 1000security:secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017tokenValidityInMilliseconds: 1800000ignore:urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
三、nacos中创建配置文件seataServer.properties
- seataServer.properties
- 修改service.vgroupMapping.mygroup=default
#For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html
#Transport configuration, for client and server
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableTmClientBatchSendRequest=false
transport.enableRmClientBatchSendRequest=true
transport.enableTcServerBatchSendResponse=false
transport.rpcRmRequestTimeout=30000
transport.rpcTmRequestTimeout=30000
transport.rpcTcRequestTimeout=30000
transport.threadFactory.bossThreadPrefix=NettyBoss
transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker
transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler
transport.threadFactory.shareBossWorker=false
transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector
transport.threadFactory.clientSelectorThreadSize=1
transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread
transport.threadFactory.bossThreadSize=1
transport.threadFactory.workerThreadSize=default
transport.shutdown.wait=3
transport.serialization=seata
transport.compressor=none#Transaction routing rules configuration, only for the client
# 此处的mygroup名字可以自定义,只修改这个值即可,
# 需要在nacos中配置dataId为service.vgroupMapping.mygroup,配置格式:为txt,内容为default
service.vgroupMapping.mygroup=default
#If you use a registry, you can ignore it
service.default.grouplist=192.168.124.11:8091
service.enableDegrade=false
service.disableGlobalTransaction=false#Transaction rule configuration, only for the client
client.rm.asyncCommitBufferLimit=10000
client.rm.lock.retryInterval=10
client.rm.lock.retryTimes=30
client.rm.lock.retryPolicyBranchRollbackOnConflict=true
client.rm.reportRetryCount=5
client.rm.tableMetaCheckEnable=true
client.rm.tableMetaCheckerInterval=60000
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.rm.sagaJsonParser=fastjson
client.rm.tccActionInterceptorOrder=-2147482648
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
client.tm.defaultGlobalTransactionTimeout=60000
client.tm.degradeCheck=false
client.tm.degradeCheckAllowTimes=10
client.tm.degradeCheckPeriod=2000
client.tm.interceptorOrder=-2147482648
client.undo.dataValidation=true
client.undo.logSerialization=jackson
client.undo.onlyCareUpdateColumns=true
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.undo.compress.enable=true
client.undo.compress.type=zip
client.undo.compress.threshold=64k
#For TCC transaction mode
tcc.fence.logTableName=tcc_fence_log
tcc.fence.cleanPeriod=1h#Log rule configuration, for client and server
log.exceptionRate=100#Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional.
# 默认为file,一定要改为db,我们自己的服务启动会连接不到seata
store.mode=db
store.lock.mode=db
store.session.mode=db
#Used for password encryption#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
# 修改mysql的配置# 数据存储方式,db代表数据库
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.124.11:3306/seata_db?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
# 事务、日志等配置#Transaction rule configuration, only for the server
server.recovery.committingRetryPeriod=1000
server.recovery.asynCommittingRetryPeriod=1000
server.recovery.rollbackingRetryPeriod=1000
server.recovery.timeoutRetryPeriod=1000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.distributedLockExpireTime=10000
server.xaerNotaRetryTimeout=60000
server.session.branchAsyncQueueSize=5000
server.session.enableBranchAsyncRemove=false
server.enableParallelRequestHandle=false#Metrics configuration, only for the server
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898
四、nacos中配置service.vgroupMapping.mygroup
五、项目中配置seata
- bootstrap.yml中配置
seata:application-id: ${spring.application.name}#数据源代理模式使用AT模式(可以不用配置,seata默认使用AT模式)data-source-proxy-mode: ATtx-service-group: mygroup # 事务分组名称,要和服务端对应#事务群组(可以每个应用独立取名,也可以使用相同的名字),#要与服务端nacos配置seataServer.properties中service.vgroupMapping.mygroup=default中存在,#此处的mygroup名字可以自定义,与 tx-service-group: mygroup中的mygroup保持一致service:vgroup-mapping:mygroup: defaultenabled: falseregistry:type: nacosnacos:username: rootpassword: 123456namespace: @nacos.config.namespace@server-addr: @nacos.server-addr@group: SEATA_GROUP#seata服务名application: seata-serverconfig:type: nacosnacos:username: rootpassword: 123456namespace: @nacos.config.namespace@server-addr: @nacos.server-addr@group: SEATA_GROUP
六、导入seata数据库
-
seata-server-1.6.0下载地址:https://github.com/apache/incubator-seata/tags
-
数据库脚本目录: \seata-server-1.6.0\seata\script\server\db
-
配置完后重启seata容器 docker restart seata-server
-
项目中就可以使用分布式事务了