zoukankan      html  css  js  c++  java
  • 使用Springboot+SpringCloud+Seata1.3.0+Nacos1.2.1进行全局事务管理

    一、官方文档网址

    http://seata.io/zh-cn/docs/overview/what-is-seata.html

    Seata1.3.0开发组提供的开发文档

    二、常见问题

    2.1:网址:

    http://seata.io/zh-cn/docs/overview/faq.html

    2.2:在使用1.1.0以上版本可能遇到以下问题:

    这是因为1.1.0版本以上不支持手动代理:

     这个类,在1.2.0版本被删除了,并且seata的数据源代理改成了AOP拦截代理。1.2.0及以上版本,只需创建普通的数据源bean,然后,启用AT代理功能就行:seata.enableDataSourceProxy=true(默认就是启用的,所以也不需要配置)

    2.2使用seata-server.bat启动seata1.3时Shell窗口出现的乱码问题:

    在seataconflogback.xml文件中修改:

     <property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"/>

    修改为:

    <property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p---[%t] %-40.40logger{39}: %m%n%wEx"/>

    此问题是因为开发者为seata1.3.0添加字体颜色,而在window中的shell脚本内不显示发生的乱码错误,预计将于1.3.1进行修复。

    三、入门

       随着互联化的蔓延,各种项目都逐渐向分布式服务做转换。如今微服务已经普遍存在,本地事务已经无法满足分布式的要求,由此分布式事务问题诞生。 分布式事务被称为世界性的难题,目前分布式事务存在两大理论依据:CAP定律 BASE理论。

    CAP定律

        这个定理的内容是指的是在一个分布式系统中、Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可得兼。

    一致性(C)

        在分布式系统中的所有数据备份,在同一时刻是否同样的值。(等同于所有节点访问同一份最新的数据副本)

    可用性(A)

        在集群中一部分节点故障后,集群整体是否还能响应客户端的读写请求。(对数据更新具备高可用性)

    分区容错性(P)

        以实际效果而言,分区相当于对通信的时限要求。系统如果不能在时限内达成数据一致性,就意味着发生了分区的情况,必须就当前操作在C和A之间做出选择。

    BASE理论

        BASE是Basically Available(基本可用)、Soft state(软状态)和 Eventually consistent(最终一致性)三个短语的缩写。BASE理论是对CAP中一致性和可用性权衡的结果,其来源于对大规模互联网系统分布式实践的总结, 是基于CAP定理逐步演化而来的。BASE理论的核心思想是:即使无法做到强一致性,但每个应用都可以根据自身业务特点,采用适当的方式来使系统达到最终一致性。

    基本可用

        基本可用是指分布式系统在出现不可预知故障的时候,允许损失部分可用性—-注意,这绝不等价于系统不可用。比如:

    (1)响应时间上的损失。正常情况下,一个在线搜索引擎需要在0.5秒之内返回给用户相应的查询结果,但由于出现故障,查询结果的响应时间增加了1~2秒

    (2)系统功能上的损失:正常情况下,在一个电子商务网站上进行购物的时候,消费者几乎能够顺利完成每一笔订单,但是在一些节日大促购物高峰的时候,由于消费者的购物行为激增,为了保护购物系统的稳定性,部分消费者可能会被引导到一个降级页面

    软状态

        软状态指允许系统中的数据存在中间状态,并认为该中间状态的存在不会影响系统的整体可用性,即允许系统在不同节点的数据副本之间进行数据同步的过程存在延时

    最终一致性

        最终一致性强调的是所有的数据副本,在经过一段时间的同步之后,最终都能够达到一个一致的状态。因此,最终一致性的本质是需要系统保证最终数据能够达到一致,而不需要实时保证系统数据的强一致性。

    四、Seata术语

    TC (Transaction Coordinator) - 事务协调者

    维护全局和分支事务的状态,驱动全局事务提交或回滚。

    TM (Transaction Manager) - 事务管理器

    定义全局事务的范围:开始全局事务、提交或回滚全局事务。

    RM (Resource Manager) - 资源管理器

    管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。

     五、Seata配置

    5.1:更改本地server端的file.conf和registry.conf文件:

    service {
      #transaction service group mapping
      #修改,可不改,my_test_tx_group随便起名字。
      vgroup_mapping.my_test_tx_group = "default"
      #only support when registry.type=file, please don't set multiple addresses
      # 此服务的地址
      default.grouplist = "127.0.0.1:8091"
      #disable seata
      disableGlobalTransaction = false
    }
    .........
    }
    registry {
      # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
      type = "nacos"
    
      nacos {
        application = "seata-server"
        serverAddr = "localhost:8848"
        group = "SEATA_GROUP"
        namespace = ""
        cluster = "default"
        username = "nacos"
        password = "nacos"
      }
      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 = "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 = "file"
    
      nacos {
        serverAddr = "127.0.0.1:8848"
        namespace = ""
        group = "SEATA_GROUP"
        username = ""
        password = ""
      }
      consul {
        serverAddr = "127.0.0.1:8500"
      }
      apollo {
        appId = "seata-server"
        apolloMeta = "http://192.168.1.204:8801"
        namespace = "application"
      }
      zk {
        serverAddr = "127.0.0.1:2181"
        sessionTimeout = 6000
        connectTimeout = 2000
        username = ""
        password = ""
      }
      etcd3 {
        serverAddr = "http://localhost:2379"
      }
      file {
        name = "file.conf"
      }
    }

    5.1:使用spring导入的依赖

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
                <version>2.2.2.RELEASE</version>
                <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.3.0</version>
            </dependency>

    5.2:yml配置

    #nacos注册中心新增group的属性配置seata:
    # registry:
    # nacos:
    # group,如果无配置,则默认为DEFAULT_GROUP,Server和Client端需保持一致。
    #seata:
    # tx-service-group: gulimall-order-fescar-service-group(此处名称可自定义,但必须与
    #service:
    # vgroup-mapping:
    # gulimall-order-fescar-service-group相同),gulimall-order-fescar-service-group: default所代表的值与Server端中file.conf里的:
    #vgroup_mapping.my_test_tx_group 的值相同
    #
    seata:
    tx-service-group: gulimall-order-fescar-service-group
    enabled: true
    registry:
    type: nacos
    nacos:
    server-addr: localhost:8848
    namespace:
    userName: "nacos"
    password: "nacos"
    group: SEATA_GROUP #group需要与server端registry.conf里的 group的值相同
    application: seata-server #application需要与server端registry.conf里的application的值相同
    service:
    vgroup-mapping:
    gulimall-order-fescar-service-group: default
    grouplist:
    default: 127.0.0.1:8091
    config:
    type: file #此处使用file类型进行存储,可更改为db,若使用db方法可参考开头开发组提供的文档进行配置
    file:
    name: file.conf
    enable-auto-data-source-proxy: true #开启自动代理数据源
    application-id: gulimall-order

     5.3:开启全局事务管理

    只需要在总事务上加上@GlobalTransactional(rollbackFor = Exception.class),以及在分支事务上加入@Transactional即可

  • 相关阅读:
    开发思路总结
    电脑蓝屏代码丢失,Eclipse这个功能帮我找回了代码
    书籍
    向日葵无法在控制端用键盘输入的原因
    js 多submit 不使其执行其他submit form
    js获取url参数值
    C# 正则表达式 去除HTML标签 C#后台
    读取txt文件 循环操作每行数据 添加到数据库
    Ubuntu中安装XAMPP出错的解决方法(转发,备查)
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/linchenguang/p/13745540.html
Copyright © 2011-2022 走看看