Seata入门与常见问题

前言:

事务是项目开发过程中经常遇到的问题,分布式的环境中,分布式式事务也尤为重要,之前我是用的RabbitMQ+定时任务来实现最终一致性的分布式事务模拟,详细的流程与内容,下一篇博客去讲,本次我们使用seata来实现分布式事务。

本次使用的环境:dubbo+nacos1.2.1+seata1.2.0

Seata简介

Seata入门与常见问题_第1张图片

seat官网:https://seata.io/zh-cn/index.html

seata 下载地址:https://seata.io/zh-cn/blog/download.html

Seata是什么:

     Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。在 Seata 开源之前,Seata 对应的内部版本在阿里经济体内部一直扮演着分布式一致性中间件的角色,帮助经济体平稳的度过历年的双11,对各BU业务进行了有力的支撑。经过多年沉淀与积累,商业化产品先后在阿里云、金融云进行售卖。2019.1 为了打造更加完善的技术生态和普惠技术成果,Seata 正式宣布对外开源,未来 Seata 将以社区共建的形式帮助其技术更加可靠与完备。

Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。

AT、TCC、SAGA 和 XA 事务模式详细介绍:https://seata.io/zh-cn/docs/overview/what-is-seata.html

Seata服务端部署

本次使用的版本为Seata1.2.0

因为我们用nacos 来作为注册中心,为了可以把Seata成功的注册的到nacos 上,我们下载Seata 时需要下载源码和集成软件

Seata入门与常见问题_第2张图片

步骤1>把seata的配置信息放到Nacos上

   1.通过source下载得到seata-1.2.0.zip,解压。

   2.进入目录\seata-1.2.0\script\server\db,根据不同的数据库进行sql语句执行。

Seata入门与常见问题_第3张图片

注: xxx.sql 中有很多表的名字容易和项目开发的表的名字冲突,

        个人建议另起一个数据库(项目名_nacos)

3. 进入目录\seata-1.2.0\script\config-center,修改config.txt文件。

以MySQL为例,需要修改内容

  • 01.存储模式为数据库模式store.mode=db,
  • 02.修改数据库信息     URL,用户名,密码:store.db.url= xxx,store.db.user=xxx,store.db.password=xxx
transport.type=TCP
transport.server=NIO
transport.heartbeat=true
transport.enableClientBatchSendRequest=false
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
service.vgroupMapping.my_test_tx_group=default
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false
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=false
client.rm.sqlParserType=druid
client.rm.reportSuccessEnable=false
client.rm.sagaBranchRegisterEnable=false
client.tm.commitRetryCount=5
client.tm.rollbackRetryCount=5
store.mode=file
store.file.dir=file_store/data
store.file.maxBranchSessionSize=16384
store.file.maxGlobalSessionSize=512
store.file.fileWriteBufferCacheSize=16384
store.file.flushDiskMode=async
store.file.sessionReloadReadSize=100
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=username
store.db.password=password
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
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
client.undo.dataValidation=true
client.undo.logSerialization=jackson
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
client.undo.logTable=undo_log
client.log.exceptionRate=100
transport.serialization=seata
transport.compressor=none
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

4、进入目录\seata-1.2.0\script\config-center\nacos,发现windows下只能通过python把seata配置文件注册到Nacos。

Seata入门与常见问题_第4张图片

下载Python服务:https://www.python.org/downloads/,选择相应版本下载,下载完成后,双击执行python-3.8.2.exe

 详细的安装过程见:Python3.8.1版本Windows安装全过程

安装完后,配置环境变量,打开Cmd,执行python,如果出来版本号,安装成功。

Seata入门与常见问题_第5张图片

5.再次进入目录\seata-1.2.0\script\config-center\nacos,然后打开Cmd,输入python nacos-config.py localhost:8848

    如果出现init nacos config finished, please start seata-server.说明初始化Seata配置到Nacos成功。

此时我们登陆Nacos也可以看到有信息了

Seata入门与常见问题_第6张图片

 步骤2>设置 seata服务的配置文件

1.通过binary下载得到seata-server-1.2.0.zip,解压。

2.进入conf目录,修改file.conf文件和registry.conf文件。

    修改file.conf文件,修改mode为db,修改db的数据库账号密码。


## transaction log store, only used in seata-server
store {
  ## store mode: file、db
  mode = "db"

  ## 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) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://xxxx/xxx_seata?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true"
    user = "user"
    password = "password"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

 修改registry.conf文件,type改为nacos

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    serverAddr = "localhost"
    namespace = ""
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  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 = "localhost:8848"
    namespace = ""
    group = "SEATA_GROUP"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
    namespace = "application"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

3.进入bin目录,双击seata-server.bat即可启动Seata服务。

只有看到这个,然后去nacos 上检查一下,Seata 服务就启动成功了

Seata入门与常见问题_第7张图片

Seata入门与常见问题_第8张图片

项目集成



    io.seata
    seata-spring-boot-starter

注: 一定要注意版本一致问题,不然后序项目的服务无法订阅与发布

1>进入目录\seata-1.2.0\script\client\spring,复制application.yml中内容到项目中的application.yml中。

seata:
  enabled: true
  application-id: applicationName
  tx-service-group: my_test_tx_group
  enable-auto-data-source-proxy: true
  use-jdk-proxy: false
  excludes-for-auto-proxying: firstClassNameForExclude,secondClassNameForExclude
  client:
    rm:
      async-commit-buffer-limit: 1000
      report-retry-count: 5
      table-meta-check-enable: false
      report-success-enable: false
      saga-branch-register-enable: false
      lock:
        retry-interval: 10
        retry-times: 30
        retry-policy-branch-rollback-on-conflict: true
    tm:
      commit-retry-count: 5
      rollback-retry-count: 5
    undo:
      data-validation: true
      log-serialization: jackson
      log-table: undo_log
    log:
      exceptionRate: 100
  service:
    vgroupMapping:
      my_test_tx_group: default
    grouplist:
      default: 127.0.0.1:8091
    enable-degrade: false
    disable-global-transaction: false
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
      worker-thread-size: default
      boss-thread-size: 1
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true
  config:
    type: nacos
    consul:
      server-addr: 127.0.0.1:8500
    apollo:
      apollo-meta: http://192.168.1.204:8801
      app-id: seata-server
      namespace: application
    etcd3:
      server-addr: http://localhost:2379
    nacos:
      namespace:
      serverAddr: localhost:8848
      group: SEATA_GROUP
      userName: ""
      password: ""
    zk:
      server-addr: 127.0.0.1:2181
      session-timeout: 6000
      connect-timeout: 2000
      username: ""
      password: ""
  registry:
    type: nacos
    consul:
      server-addr: 127.0.0.1:8500
    etcd3:
      serverAddr: http://localhost:2379
    eureka:
      weight: 1
      service-url: http://localhost:8761/eureka
    nacos:
      application: seata-server
      server-addr: localhost:8848
      namespace:
      userName: ""
      password: ""
    redis:
      server-addr: localhost:6379
      db: 0
      password:
      timeout: 0
    sofa:
      server-addr: 127.0.0.1:9603
      region: DEFAULT_ZONE
      datacenter: DefaultDataCenter
      group: SEATA_GROUP
      addressWaitTime: 3000
      application: default
    zk:
      server-addr: 127.0.0.1:2181
      session-timeout: 6000
      connect-timeout: 2000
      username: ""
      password: ""

注:Seata 自1.0版本后 service.vgroup-mapping 就变成了service.vgroupMapping
  

测试

在需要分布式事务的方法上加@GlobalTransactional(rollbackFor = Exception.class)注解,即可实现该方法的分布式事务

常见问题

Seata入门与常见问题_第9张图片

如果出现这种问题,

ERROR 6000 --- [ main] i.s.c.r.netty.NettyClientChannelManager  : no available service 'default' found, please make sure registry config correct

一定要检查你的seata配置,一般大多数情况下都是因为配置不匹配导致的

1.你现在使用的seata服务和项目maven中seata的版本一致吗?

2.你的application.yml中的参数(如下图)和Nacos中的配置一致吗?(application.yml下面的图)

一般常见的问题就是下图所展现出来的

Seata入门与常见问题_第10张图片

Seata入门与常见问题_第11张图片

你可能感兴趣的:(Seata入门与常见问题)