docker安装seata1.5.2及其使用

前言:

最近进行了seata的安装和使用,对照网上的文章和文档进行安装配置使用的过程中绕了不少弯路,现在完成工作之后写一篇博文对此进行总结和记录。

直接上案例:

  1. 首先要注意Spring Cloud Alibaba,Spring Cloud ,Spring Boot ,Nacos ,Seata版本的匹配关系,见如下url:
    https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
    我一开始由于客户端版本落后(Spring Cloud Alibaba 2.2.0.RELEASE),即使依赖了1.5.2的客户端依赖,实际上仅仅支持1.0.0版本,导致不兼容无法发现seata服务,实测升级到Spring Cloud Alibaba 2.2.6.RELEASE支持到1.3.0版本可以兼容。
    版本不匹配可能会出现
【SpringCloud】no available service ‘null‘ found, please make sure registry config correct

或者

【SpringCloud】no available service ‘default‘ found, please make sure registry config correct

的错误信息

  1. 由于使用的是db模式,所以需要准备数据库并建表,sql见url:
    https://gitee.com/seata-io/seata/blob/v1.5.2/script/server/db/mysql.sql
    如果版本不是1.5.2需要注意修改版本号,不同版本的表结构可能有所不同
  2. 还需要在需要使用功能的库中建名为undo_log的表(每个用到的库都要):
CREATE TABLE `undo_log`  (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `branch_id` bigint NOT NULL,
  `xid` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `context` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB;
  1. 使用的是nacos模式,所以需要准备nacos相关配置
    为seata新建单独的命名空间“seata”记下命名空间id(例:bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9)之后要用
    在seata命名空间下新建seataServer.properties配置,GROUP为SEATA_GROUP,内容见下例:
store.mode=db

store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=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.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
 
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

在seata命名空间下新建 service.vgroupMapping.test_tx_group配置,GROUP:SEATA_GROUP,内容为default

  1. 在部署seata-sever的服务器上新建docker-compose.yml文件
version: "3"
services:
  seata-server:
    image: seataio/seata-server:1.5.2
    hostname: seata-server
    container_name: seata-server
    restart: always
    ports:
      - "8091:8091"
      - "7091:7091"
    environment:
      - SEATA_PORT=8091
      - SEATA_IP=127.0.0.1#这里需要填入能被访问到的ip,注册到nacos的就是这个ip
      - SEATA_CONFIG_NAME=file:/root/seata-config/registry
    volumes:
      - /opt/aaa/dev-env/config:/root/seata-config
      - /opt/aaa/dev-env/logs:/root/logs
      - /opt/aaa/dev-env/application.yml:/seata-server/resources/application.yml
      - seata-server-1.5.2-data:/seata-server/resources:rw
volumes:
  seata-server-1.5.2-data: { }

新建application.yml文件

server:
  port: 7091
spring:
  application:
    name: seata-server
logging:
  level:
    com.alibaba.nacos.client.config.*: WARN
  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:
  tx-service-group: test_tx_group
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
      group: SEATA_GROUP
      data-id: seataServer.properties
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
      cluster: default
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

docker-compose up 启动!


███████╗███████╗ █████╗ ████████╗ █████╗
██╔════╝██╔════╝██╔══██╗╚══██╔══╝██╔══██╗
███████╗█████╗  ███████║   ██║   ███████║
╚════██║██╔══╝  ██╔══██║   ██║   ██╔══██║
███████║███████╗██║  ██║   ██║   ██║  ██║
╚══════╝╚══════╝╚═╝  ╚═╝   ╚═╝   ╚═╝  ╚═╝
09:54:31.811  INFO --- [                     main] io.seata.server.ServerApplication        : Starting ServerApplication using Java 1.8.0_212 on seata-server with PID 1 (/seata-server/classes started by root in /seata-server)
09:54:31.819  INFO --- [                     main] io.seata.server.ServerApplication        : No active profile set, falling back to default profiles: default
09:54:35.367  INFO --- [                     main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7091 (http)
09:54:35.401  INFO --- [                     main] o.a.coyote.http11.Http11NioProtocol      : Initializing ProtocolHandler ["http-nio-7091"]
09:54:35.406  INFO --- [                     main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
09:54:35.407  INFO --- [                     main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
09:54:35.624  INFO --- [                     main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
09:54:35.627  INFO --- [                     main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3679 ms
09:54:37.376  INFO --- [                     main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
09:54:37.866  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/'] with []
09:54:37.870  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.css'] with []
09:54:37.885  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.js'] with []
09:54:37.888  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.html'] with []
09:54:37.890  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.map'] with []
09:54:37.893  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.svg'] with []
09:54:37.893  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.png'] with []
09:54:37.893  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/**/*.ico'] with []
09:54:37.893  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/console-fe/public/**'] with []
09:54:37.893  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure Ant [pattern='/api/v1/auth/login'] with []
09:54:37.948  INFO --- [                     main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@35563e4c, org.springframework.security.web.context.SecurityContextPersistenceFilter@289f15e9, org.springframework.security.web.header.HeaderWriterFilter@447521e, org.springframework.security.web.authentication.logout.LogoutFilter@7911cc15, io.seata.console.filter.JwtAuthenticationTokenFilter@6dd79214, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6af87130, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3711c71c, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@ef86c72, org.springframework.security.web.session.SessionManagementFilter@3344d163, org.springframework.security.web.access.ExceptionTranslationFilter@6f3bd37f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7af327e3]
09:54:38.024  INFO --- [                     main] o.a.coyote.http11.Http11NioProtocol      : Starting ProtocolHandler ["http-nio-7091"]
09:54:38.061  INFO --- [                     main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7091 (http) with context path ''
09:54:38.085  INFO --- [                     main] io.seata.server.ServerApplication        : Started ServerApplication in 9.081 seconds (JVM running for 10.376)
09:54:39.687  INFO --- [                     main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
09:54:40.194  INFO --- [                     main] i.s.core.rpc.netty.NettyServerBootstrap  : Server started, service listen port: 8191
09:54:40.221  INFO --- [                     main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
09:54:40.285  INFO --- [                     main] com.alibaba.nacos.client.naming          : [BEAT] adding beat: BeatInfo{port=8091, ip='192.168.2.7', weight=1.0, serviceName='SEATA_GROUP@@seata-server', cluster='default', metadata={}, scheduled=false, period=5000, stopped=false} to beat map.
09:54:40.296  INFO --- [                     main] com.alibaba.nacos.client.naming          : [REGISTER-SERVICE] bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9 registering service SEATA_GROUP@@seata-server with instance: Instance{instanceId='null', ip='192.168.2.7', port=8091, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='default', serviceName='null', metadata={}}
09:54:40.304  INFO --- [                     main] io.seata.server.ServerRunner             : seata server started in 2213 millSeconds

启动成功,成功注册到了nacos

客户端使用

  1. 在nacos创建客户端的配置seata-config.properties,并被客户端引用,和客户端其他配置放一个命名空间和分组即可
seata.application-id=${spring.application.name}
seata.tx-service-group=test_tx_group
seata.registry.type=nacos
seata.registry.nacos.server-addr=127.0.0.1:8848
seata.registry.nacos.group=SEATA_GROUP
seata.registry.nacos.namespace=bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
seata.registry.nacos.application=seata-server
seata.registry.nacos.cluster=default
seata.config.type=nacos
seata.config.nacos.server-addr=127.0.0.1:8848
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.namespace=bd9db9e4-c709-4706-a3c7-9f0a5b4bb3f9
seata.service.vgroup-mapping.test_tx_group=default
  1. 在客户端需要用到的模块加入com.alibaba.cloud:spring-cloud-starter-alibaba-seata,不需要再额外引入seata-starter模块,都加入可能会出现分布式事务失效的bug。这里注意SpringBoot和SpringCloud相关版本,是否兼容(查看1.中的文档),选择合适的版本或者适当调整seata版本。不需要额外配置,spring-cloud-starter-alibaba-seata会自动进行数据源代理。如果需要进行测试可以使用RootContext.getXID()方法在各个服务中获取xid,如果xid不一致或者为null,则会出现分布式事务失效的情况,需要排查问题。
  2. seata.service.vgroup-mapping.test_tx_group的值实际上就是服务端配置中cluster的值,即default
  3. 使用上只需要在最外层添加注解@GlobalTransactional即可生效,至于生效的模式和下层包含@Transactional注解时的情况,可以通过查看seata-server日志以及开发环境打断点调试进入seata 7091端口的后台查看了解。

结尾

在安装配置过程中查询了不少文章和文档,也踩了不少坑,但是实际完成之后却发现其实整个过程中也不存在什么难点,都是一些细节的地方可能会出现各种奇怪的问题。自己记录了一遍权当作为个人的工作记录,如果其中一言两语能对该文读者有所帮助,也不甚荣幸。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Docker安装Seata 1.5.2,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装DockerDocker Compose。如果还没有安装,可以按照官方文档进行安装。 2. 创建一个新的目录,用于存放Seata的配置文件和Docker Compose文件。例如,你可以在命令行中执行以下命令创建一个名为seata的目录: ``` mkdir seata cd seata ``` 3. 在seata目录下创建一个名为docker-compose.yml的文件,并将以下内容复制到该文件中: ``` version: '3' services: seata-server: image: seataio/seata-server:1.5.2 container_name: seata-server ports: - "8091:8091" volumes: - ./config:/root/seata-config ``` 4. 在seata目录下创建一个名为config的目录,用于存放Seata的配置文件。你可以在命令行中执行以下命令创建该目录: ``` mkdir config ``` 5. 在config目录下创建一个名为registry.conf的文件,并将以下内容复制到该文件中: ``` registry { type = "nacos" nacos { serverAddr = "nacos:8848" namespace = "" cluster = "default" } } config { type = "nacos" nacos { serverAddr = "nacos:8848" namespace = "" group = "SEATA_GROUP" } } ``` 6. 在config目录下创建一个名为file.conf的文件,并将以下内容复制到该文件中: ``` service { vgroupMapping.my_test_tx_group = "default" default.grouplist = "127.0.0.1:8091" enableDegrade = false disableGlobalTransaction = false } ## transaction log store store { mode = "db" db { datasource = "dbcp" dbType = "mysql" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://127.0.0.1:3306/seata" user = "root" password = "root" } } ``` 请确保根据你的实际情况修改上述配置文件中的数据库连接信息。 7. 在seata目录下执行以下命令来启动Seata Server: ``` docker-compose up -d ``` 8. 等待一段时间,直到Seata Server成功启动。你可以通过访问 http://localhost:8091 来验证Seata Server是否正常工作。 现在,你已经成功在Docker安装Seata 1.5.2。你可以根据自己的需求配置Seata并将其集成到你的应用程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值