zoukankan      html  css  js  c++  java
  • SpringCloud集成Seata并使用Nacos做注册中心与配置中心

    本文为博主原创,未经允许不得转载:

    目录:

      1. 下载并启动Seata Server,并指定nacos作为配置中心和注册中心

      2. 同步seata server的配置到nacos

      3. 启动Seata Server

      4. Seata整合到Spring Cloud微服务

      5. 接入问题总结:

      6.demo项目gitee 链接: https://gitee.com/xiangbaxiang/seata-nacos-demo

    1. 下载并启动Seata Server,并指定nacos作为配置中心和注册中心

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

         

      1.2 通过 seata 的 conf 目录下的 file.conf 指定server 端存储模式:

        支持 file ,db , redis 三种方式:

          file:(默认)单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高(默认)

          db:(5.7+)高可用模式,全局事务会话信息通过db共享,相应性能差些

          redis:Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置适合当前场景的redis持久化配置

      1.3 在 register.conf 指定注册中心:

                                 

       1.4 在 register.conf 指定配置中心:

           

       注意:客户端配置registry.conf使用nacos时也要注意group要和seata server中的 group 一致,默认group是"DEFAULT_GROUP"

    2. 同步seata server的配置到nacos

      先启动本地的nacos  

      获取/seata/script/config-center/config.txt,修改配置信息

        

       seata 从1.4 之后,seata 的安装包中去除了 同步的配置文件,可以从这个目录中获取: https://github.com/seata/seata/tree/1.3.0/script 

       配置事务分组, 要与客户端配置的事务分组一致

        (客户端properties配置:spring.cloud.alibaba.seata.tx‐service‐group=my_test_tx_group)

                             

      通过脚本配置参数同步到Nacos
     sh ${SEATAPATH}/script/config‐center/nacos/nacos‐config.sh ‐h localhost ‐p 8848 ‐g SEATA_GROUP ‐t 5a3c7d6c‐f497‐4d68‐a71a‐2e5e3340b3ca

        参数说明:

          -h: host,默认值 localhost
          -p: port,默认值 8848
          -g: 配置分组,默认值为 'SEATA_GROUP'
          -t: 租户信息,对应 Nacos 的命名空间ID字段, 默认值为空。
       也可以直接使用以下命令进行同步 
    sh nacos‐config.sh ‐h localhost ‐p 8848 ‐g SEATA_GROUP

    3. 启动Seata Server

        启动Seata Server命令

    bin/seata‐server.sh

        启动成功,默认端口8091

    4. Seata整合到Spring Cloud微服务

      业务场景:
        用户下单,整个业务逻辑由三个微服务构成:
        仓储服务:对给定的商品扣除库存数量。
        订单服务:根据采购需求创建订单。
        帐户服务:从用户帐户中扣除余额。

       4.1导入依赖:

    <!‐‐ seata‐‐> 
     <dependency>
        <groupId>com.alibaba.cloud</groupId> 
        <artifactId>spring‐cloud‐starter‐alibaba‐seata</artifactId> 
        <exclusions> 
            <exclusion> 
                <groupId>io.seata</groupId> 
                <artifactId>seata‐all</artifactId> 
            </exclusion> 
        </exclusions> 
    </dependency> 
     <dependency> 
        <groupId>io.seata</groupId> 
        <artifactId>seata‐all</artifactId> 
        <version>1.4.0</version> 
     </dependency> 
     <!‐‐nacos 注册中心‐‐> 
     <dependency> 
        <groupId>com.alibaba.cloud</groupId> 
        <artifactId>spring‐cloud‐starter‐alibaba‐nacos‐discovery</artifactId> 
     </dependency> 
     <dependency>
        <groupId>org.springframework.cloud</groupId> 
        <artifactId>spring‐cloud‐starter‐openfeign</artifactId> 
        </dependency> 
    <dependency> 
        <groupId>com.alibaba</groupId> 
        <artifactId>druid‐spring‐boot‐starter</artifactId> 
        <version>1.1.21</version> 
    </dependency> 
    <dependency> 
        <groupId>mysql</groupId> 
        <artifactId>mysql‐connector‐java</artifactId> 
        <scope>runtime</scope> 
        <version>8.0.16</version> 
    </dependency> 
    <dependency> 
        <groupId>org.mybatis.spring.boot</groupId> 
        <artifactId>mybatis‐spring‐boot‐starter</artifactId> 
        <version>2.1.1</version> 
    </dependency>

    <dependency>
      <groupId>io.seata</groupId>
      <artifactId>seata‐spring‐boot‐starter</artifactId>
      <version>1.4.0</version>
    </dependency>

      4.2 微服务对应数据库中添加undo_log

    CREATE TABLE `undo_log` ( 
     `id` bigint(20) NOT NULL AUTO_INCREMENT, 
     `branch_id` bigint(20) NOT NULL, 
     `xid` varchar(100) NOT NULL, 
     `context` varchar(128) NOT NULL, 
     `rollback_info` longblob NOT NULL, 
     `log_status` int(11) NOT NULL, 
     `log_created` datetime NOT NULL, 
     `log_modified` datetime NOT NULL, 
     0 PRIMARY KEY (`id`), 
     UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) 
     ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

      4.3 在yml中配置

    seata:
      enabled: true
      application-id: ${spring.application.name}
      # seata 服务分组,要与服务端nacos‐config.txt中service.vgroup_mapping的后缀对应 
      tx-service-group: my_test_tx_group
      config:
       # 指定nacos作为配置中心 
        type: nacos
        nacos:
          namespace:
          serverAddr: 127.0.0.1:8848
          group: SEATA_GROUP
    
      registry:
        # 指定nacos作为注册中心 
        type: nacos
        nacos:
          application: seata-server
          server-addr: 127.0.0.1:8848
          namespace:
        

      在事务发起者中添加@GlobalTransactional注解

    @GlobalTransactional

    5. 接入问题总结:

      一般大多数情况下都是因为配置不匹配导致的:
      1.检查现在使用的seata服务和项目maven中seata的版本是否一致
      2.检查tx-service-group,nacos.cluster,nacos.group参数是否和Seata Server中的配置一致
      跟踪源码:seata/discover包下实现了RegistryService#lookup,用来获取服务列表

     6.demo项目gitee 链接: https://gitee.com/xiangbaxiang/seata-nacos-demo

      在查看并使用该项目时,请先查看 ReadMe.md中的相关介绍。

  • 相关阅读:
    postgresql字符串函数
    ruby中的设计模式--策略模式
    (转)MySQL 性能优化的最佳20多条经验分享
    (转)ruby中的设计模式--模板方法
    观察者模式的应用
    postgresql的ARRAY_TO_STRING
    ruby和javascript的观察者模式
    mysql表连接的时候注意事项
    checkbox记忆功能的实现
    order by的注意事项
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/15221388.html
Copyright © 2011-2022 走看看