Seata 1.5.0 + Nacos 作为注册中心和配置中心 + docker
官网地址: seata.io
1.搭建Seata-server (采用 docker 搭建)
1.1镜像搜索 :
docker search seata
1.2 拉取镜像 :
docker pull seataio/seata-server:1.5.0
1.3 拷贝配置文件
## 先启动一个临时容器
docker run -d --name seata -p 8091:8091 seataio/seata-server:1.5.0
## copy 配置文件
docker cp seata:/seata-server /docker-seata-data
## 停止并删除容器
docker stop seata
docker rm seata
1.4 Seata server配置
查看拷贝出的文件:
进入resources 修改 application.yaml 配置
# 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: 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: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3 此次采用的Nacos作为配置中心
type: nacos
nacos:
server-addr: 192.168.1.57:8848
# 命名空间
namespace: 2c54c7bb-3e81-417a-8138...
group: DEFAULT_GROUP
username: nacos
password: nacos
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
## seataServer.properties配置文件内容见下文,该配置文件放置的地方必须在你所指定的namespace下
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa 此次注册中心也采用Nacos
type: nacos
# preferred-networks: 30.240.*
nacos:
application: seata-server
server-addr: 192.168.1.57:8848
group: DEFAULT_GROUP
namespace: 2c54c7bb-3e81-417a-8138...
# 指定集群名称 就默认使用default了
cluster: default
username: nacos
password: nacos
context-path:
##if use MSE Nacos with auth, mutex with username/password attribute
#access-key:
#secret-key:
store:
# support: file 、 db 、 redis 存储类型采用Mysql
mode: db
session:
mode: db
lock:
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://数据库地址:3306/seata?rewriteBatchedStatements=true
user: root
password: 密码
min-conn: 10
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 1000
max-wait: 5000
# 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
seataServer.properties 配置:(参考官网 config.txt 文件)
地址: https://github.com/seata/seata/blob/2.x/script/config-center/config.txt
经过修改后的配置:
#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 事务组与集群配置: service.vgroupMapping.事务组名=集群名,事务组名在后面有配置
service.vgroupMapping.search-service-group=default
#If you use a registry, you can ignore it
#service.default.grouplist=127.0.0.1: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.(store改为db)
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.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://数据库地址:3306/seata?rewriteBatchedStatements=true
store.db.user=root
store.db.password=密码
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指定的namespace 创建data-id 为: seataServer.properties 分组默认为:DEFAULT_GOUP ,复制该配置信息并保存
1.5 导入数据库表
地址:https://github.com/seata/seata/tree/2.x/script
client目录为spring微服务需要创建的表
server目录为seata-server需要创建的表
具体就不过多介绍了…
1.6创建和启动容器
# 容器创建并启动
docker run -d --name seata \
-p 8091:8091 \
-p 7091:7091 \
-e SEATA_PORT=8091 \
-e SEATA_IP=192.168.1.57 \
-v /docker-seata-data:/seata-server \
seataio/seata-server:1.5.0
#查看启动日志
docker logs seata
注意:SEATA_IP=192.168.1.57 为指定seata的ip,如果不指定,会随机分配一个ip作为seata的ip,可能会导致seata的ip和java服务注册的ip无法通信,当你启动java程序时会报连接不上seata的错误
Nacos上查看seata服务
Nacos上查看search服务(java微服务)
IP和分组需保持一致
2.微服务相关配置
前提: 已有Nacos 作为注册中心和配置中心
2.1导入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2021.1</version>
</dependency>
最终的Seata版本为1.5.1
2.2 yaml 配置
seata:
registry:
type: nacos
nacos:
server-addr: huoting-server:8848
application: seata-server
group: DEFAULT_GROUP
namespace: 2c54c7bb-3e81-417a-8138-b3cd7ee4d047
config:
type: nacos
nacos:
server-addr: huoting-server:8848
group: DEFAULT_GROUP
username: nacos
password: nacos
data-id: seataServer.properties
namespace: 2c54c7bb-3e81-417a-8138-b3cd7ee4d047
# 指定事务组名(本次采用的方式是:微服务名-group)
tx-service-group: search-service-group
附加Nacos配置
spring:
cloud:
nacos:
# 用于连接 Nacos 注册中心的地址
server-addr: 172.18.45.26:8848
discovery:
# 用于进行服务发现的地址
server-addr: 172.18.45.26:8848
namespace: 2c54c7bb-3e81-417a-8138...
config:
# 指定读取 seataServer.properties配置文件
name: seataServer.properties
namespace: 2c54c7bb-3e81-417a-8138...
至此seata已基本搭建完成, 使用 @GlobalTransactional 全局事务注解即可