Seata安装及SpringCloudAlibaba整合Seata

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。当然我们一般使用 AT模式即可

公众号 : 倔强小狮子

在这里插入图片描述



不写原理,因官网文档描述的很清楚, 所以建议查看官方文档 : http://seata.io/zh-cn/

1. Seata安装(该教程使用 1.4.1)

1.1 地址 : https://github.com/seata/seata/releases

在这里插入图片描述

2. Seata配置

2.1 seata/conf.file.conf 事务日志配置

  • 该文件事务日志存储,仅在seata服务器中配置
  • 该文件提供 三种配置
  • 第一种使用 file 文件存储, 这种适用于单机环境并且测试环境即可
  • 第二种使用 DB 模式, 个人建议生产一定使用该模式
  • 第二种使用 redis模式, 这种为了解决 DB 模式下性能问题, 个人建议使用这种模式
  • 为了方便测试我使用了 DB模式
store {
  mode = "db" # 配置使用哪种事务日志模式,默认使用 file模式
  file {  # 文件模式
    dir = "sessionStore"
    maxBranchSessionSize = 16384
    maxGlobalSessionSize = 512
    fileWriteBufferCacheSize = 16384
    sessionReloadReadSize = 100
    flushDiskMode = async
  }
  db { # DB 模式
    datasource = "druid"
    dbType = "mysql"
    # 注意: 如果使用Mysql8.x以上需要将 com.mysql.jdbc.Driver 
    # 修改成 com.mysql.cj.jdbc.Driver 即可
    driverClassName = "com.mysql.cj.jdbc.Driver" 
    # Mysql 地址
    url = "jdbc:mysql://localhost:3306/seata"
    user = "root"
    password = "root"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
  redis { # redis 模式
    host = "127.0.0.1"
    port = "6379"
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    maxTotal = 100
    queryLimit = 100
  }
}

2.2 seata/conf.registry.conf Seata 注册服务及配置中心化

  • registry.conf 这个文件下包含了, Seata 服务注册(registry )及配置中心(config)以上两项的配置
  • seata 支持多种注册服务,我这里使用 alibaba开源的 nacos,个人建议使用 nacos
  • 注意 : serverAddr 该配置项不要听取某些文章说明需要修改该配置名
registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos" # seata 支持多种注册服务, 我是用了 nacos
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10
 
  nacos { 
    application = "seata-server" # 注册服务应用名
    serverAddr = "http://localhost:8848" # naocs 地址
    group = "SEATA_GROUP" # nacos 分组
    namesnapace = "" # 可以配置 nacos namesnapace 
    cluster = "default" 
    username = "lionet"
    password = "123456"
  }
  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"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "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 作为 Seata的配置中心
 
  nacos {
    serverAddr = "localhost:8848" # nacos 地址
    namespace = "" 
    group = "SEATA_GROUP" 
    username = "lionet"
    password = "123456"
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    appId = "seata-server"
    apolloMeta = "http://192.168.1.204:8801"
    namespace = "application"
    apolloAccesskeySecret = ""
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    sessionTimeout = 6000
    connectTimeout = 2000
    username = ""
    password = ""
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

3. 将 Seata需要配置上传 Nacos

3.1 下载源码

3.2 上传 Nacos配置

  • 当然该 config-center 目录下提供的 config.txt 文件中包含需要配置项, 其实使用 nacos 作为配置中心只需要简单几项

    # service.vgroup_mapping.your-server-group = default,中 间 的 {your-service-gruop}=default,服务中的application.properties文件里配置服务组名称后面说明
    service.vgroupMapping.lionet_tx_group=default  #  自己定义的服务组名称 
    store.mode=db
    store.db.datasource=druid
    store.db.dbType=mysql
    # MySQL8.x 需要将 com.mysql.jdbc.Driver 修改为 com.mysql.cj.jdbc.Driver
    store.db.driverClassName=com.mysql.cj.jdbc.Driver
    # 数据库地址
    store.db.url=jdbc:mysql://localhost:3306/seata?useUnicode=true
    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.queryLimit=100
    store.db.lockTable=lock_table
    store.db.maxWait=5000
    
  • 进入 /config-center/nacos 目录执行 nacos-config.sh 上传配置

  • ./nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t e651af00-158b-4105-a8fc-818b577c6daa -u nacos -w 123456

选项说明
-hNacos 地址
-pNaocs 端口
-g上面说到的nacos作为配置中心的配置分组 (config.nacos.group)
-t上面说到的nacos作为配置中心的名命空间 (config.nacos.namespace )
-unacos 用户名
-wnacos 密码
  • 以上上传完成后 刷新 nacos配置(注意 : store.db.password及store.db.user 是否是你配置的值, 如果不是请修改正确)
    在这里插入图片描述

4. 启动 Seata

  • 进入 seata/bin 目录 ./seata-server.sh,出现图下图演示即可
  • 如果使用服务器需要在 Seata启动时 暴露公网IP ./bin/seata-server.sh -p 8091 -h 公网IP
    在这里插入图片描述

4. SpringCloudAlibaba 整合 Seata

4.1 pom

		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-seata</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.4.0</version>
        </dependency>

application.yml

seata:
  enabled: true
  # 服务名称
  application-id: bl-blog-ucenter
  # 与上面说明 service.vgroup_mapping.your-server-group = default
  tx-service-group: lionet_tx_group
  # 开启代理
  enable-auto-data-source-proxy: true
  # 将 nacso 作为配置中心
  config: 
    type: nacos 
    nacos:
      namespace: ""
      # nacos 地址 config.nacos.serverAddr
      serverAddr: http://localhost:8848
      # 上述 config.nacos.group 配置
      group: SEATA_GROUP
      userName: "lionet"
      password: "123456"
  # 将 Nacos作为 Seata注册中心, 客户端需向Nacos获取服务地址
  registry:
    type: nacos
    nacos:
      # 注册服务名称  上述说明 nacos.application
      application: seata-server
      # registry.nacos.serverAddr 一致
      server-addr: http://localhost:8848
      namespace: ""
      userName: "lionet"
      password: "123456"

在这里插入图片描述

  • 如果使用 服务器启动会出现以上错误不要慌, 使因为 Seata默认不暴露公网IP, .sh默认使用 localhost:8091
  • 需要在 Seata启动时 暴露公网IP
  • 关掉 Seata ./bin/seata-server.sh -p 8091 -h 公网IP 再次启动即可
    在这里插入图片描述
  • Seata 出现以上注册信息代表整合完成

期待你的关注,和我一起学习
转载说明:转载携带原文链接

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值