Seata

分布式事务

一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用,就会产生分布式事务的问题。

单体应用被拆分成微服务应用,例如原来的三个模块被拆分成三个独立的应用,分别使用三个独立的数据源。业务操作需要调用这三个服务来完成,此时每个服务内部的数据一致性由本地事务来保证,但是全局的数据一致性问题没法保证。

Seata

是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

Seata主要由分布式事务处理过程的一ID+三组件组成


Transaction ID XID:全局唯一的事务ID

TC (Transaction Coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚。
TC相当于就是Seata服务器

TM (Transaction Manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。
@GlobalTransactional就是事务发起方,相当于就是TM

RM (Resource Manager) - 资源管理器
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
RM相当于事务的参与方
在这里插入图片描述

1.TM向TC申请开启一个全局事务,全局事务创建成功并生成一个全局唯一的XID
2.XID在微服务调用链路的上下文中传播
3.RM向TC注册分支事务,将其纳入XID对应全局事务的管辖
4.TM向TC发起正对XID的全局提交或回滚决议
5.TC调用XID下管辖的全部分支事务完成提交或回滚请求

服务端安装

1.下载解压
在这里插入图片描述
2.修改配置文件,在conf下面有application.yml启动文件和application.example.yml模板文件,修改启动文件中的内容,从模板文件复制,启动文件主要修改以下内容:注册中心、配置中心、和持久化
在这里插入图片描述
修改后的内容如下:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  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:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace:
      group: SEATA_GROUP
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
      data-id: seataServer.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
      #指定注册至nacos注册中心的集群名
      cluster: default
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
  store:
    # support: file 、 db 、 redis
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
      user: root
      password: 123456
      min-conn: 5
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 100
      max-wait: 5000
#  server:
#    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

3.创建Seata数据库
seata-server-1.5.2\seata\script\server\db\mysql.sql
4.先启动Nacos再启动Seata,Seata的启动脚本在bin目录下
默认账号密码:seata
在这里插入图片描述
在这里插入图片描述

服务端配置

1.首先将客户端要用到的配置放入Nacos配置中心,配置文件在seata-server-1.5.2\seata\script\config-center\config.txt主要需要修改数据库连接。上面的配置只是服务端的配置,这个是客户端的配置必须配置。在服务端的配置文件中,指定了seata.config.data-id: seataServer.propertiesseata.config.group=SEATA_GROUP,所以Nacos配置中心的配置如下在这里插入图片描述

2.SpringBoot中导入依赖

<!-- 导入seata的依赖 修改1.5.2的版本为使用的版本 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-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.5.2</version>
        </dependency>

3.添加客户端(SpringBoot项目)配置文件

spring:
  application:
    # 应用名称
    name: seata-storage-service

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.223.128:3306/seata_storage?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    druid:
      test-while-idle: false
  #nacos配置
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

server:
  port: 2002

seata:
  #指定事务分组至集群映射关系(等号右侧的集群名需要与Seata-server注册到Nacos的cluster保持一致)
  service:
    vgroup-mapping:
      default_tx_group: default
  enabled: true
  # 事务分组配置(在v1.5之后默认值为default_tx_group)
  tx-service-group: default_tx_group
  config:
    type: nacos
    nacos:
      # nacos ip地址
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      data-id: seata-server.properties # 读取 nacos seata 配置
  registry:
    type: nacos
    nacos:
      application: seata-server # seata 服务名
      # nacos ip地址
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP

4.添加@GlobalTransactional(rollbackFor = Exception.class)事务开启全局异常

启动后如果一直报错:can not get cluster name in registry config ‘service.vgroupMapping.default_tx_group’, please make sure registry config correct
需要在配置中心添加配置
在这里插入图片描述
官网对于事务分组的解释如下,我的理解是seata.tx-service-group定义事务分组名称,而在配置中心中通过service.vgroupMapping .[事务分组配置项]指定集群名称也就是服务端配置的cluster: default。测试发现如果不在配置i中心配置service.vgroupMapping .[事务分组配置项]客户端项目会报error,但是客户端不加seata.service.vgourp-mapping.事务分组名称功能依然有效,不过还是加上吧。。。。
在这里插入图片描述

相关链接
SpringBoot 整合 SpringCloud Alibab-Seata 详解
seata1.5.1搭建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值