Seata分布式事务环境搭建

2022-03-30 21:19:29 704

下载seata服务端

https://github.com/seata/seata/releases

修改registry.conf

这里使用nacos做注册中心和配置中心, 也就不需要服务端的file.conf了

但是使用nacos时, nacos的密码不能有特殊符号, 否则seata可能连接不上

registry {
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "e794b575-4231-4935-8271-145c5840d392"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "e794b575-4231-4935-8271-145c5840d392"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
    dataId = "seataServer.properties"
  }
}

seata服务端需要的几个表: https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

其他的一些相关的脚本https://github.com/seata/seata/tree/develop/script

nacos建立命名空间

新增配置文件

Data ID: seataServer.properties

Group: SEATA_GROUP

seataServer.properties配置内容

### seata
store.mode=db
store.publicKey=
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
store.db.datasource=druid
## mysql/oracle/postgresql/h2/oceanbase etc.
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
store.db.url=jdbc:mysql://192.168.101.128:3309/seata?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&&serverTimezone=Asia/Shanghai
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=100
store.db.globalTable = global_table
store.db.branchTable = branch_table
store.db.lockTable =lock_table
store.db.queryLimit = 100
store.db.maxWait = 5000



## transport
# tcp udt unix-domain-socket
transport.type=TCP
#NIO NATIVE
transport.server=NIO
#enable heartbeat
transport.heartbeat=true

transport.serialization=seata
transport.compressor=none

transport.threadFactory.bossThreadPrefix = NettyBoss
transport.threadFactory.workerThreadPrefix = NettyServerNIOWorker
transport.threadFactory.serverExecutorThread-prefix = 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



server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000

单体服务多库事务

SpringBoot项目引入依赖

<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>

项目配置文件

seata:
  application-id: test #这里填你应用的id
  service:
    grouplist:
      # seata-server地址
      default: 127.0.0.1:8091
    # 分组事务
    vgroup-mapping:
      global_tx_group: default
      enable-degrade: false
      disable-global-transaction: false
  # 是否开启spring-boot自动装配
  enabled: true
  # 是否启用数据源 bean 的自动代理
  enable-auto-data-source-proxy: true
  tx-service-group: global_tx_group
  client:
    tm:
      # 一阶段全局提交结果上报TC重试次数 默认1次,建议大于1
      commit-retry-count: 3
      # 一阶段全局回滚结果上报TC重试次数 默认1次,建议大于1
      rollback-retry-count: 3
    rm:
      # 是否上报一阶段成功 true、false,从1.1.0版本开始,默认false.true用于保持分支事务生命周期记录完整,false可提高不少性能
      report-success-enable: true
      # 自动刷新缓存中的表结构 默认false
      table-meta-check-enable: true
      # 一阶段结果上报TC重试次数
      report-retry-count: 5
      # 异步提交缓存队列长度 默认10000。 二阶段提交成功,RM异步清理undo队列
      async-commit-buffer-limit: 1000
      lock:
        # 校验或占用全局锁重试间隔 默认10,单位毫秒
        retry-interval: 10
        # 分支事务与其它全局回滚事务冲突时锁策略 默认true,优先释放本地锁让回滚成功
        retry-policy-branch-rollback-on-conflict: true
        # 校验或占用全局锁重试次数
        retry-times: 30
    undo:
      # 自定义undo表名 默认undo_log
      log-table: seata_undo_log
      # 二阶段回滚镜像校验
      data-validation: true
      # undo log序列化方式
      log-serialization: jackson

  transport:
    type: TCP
    server: NIO
    heartbeat: true
    # client和server通信编解码方式 seata(ByteBuf)、protobuf、kryo、hession、fst,默认seata
    serialization: seata
    # client和server通信数据压缩方式 none、gzip,默认none
    compressor: none
    thread-factory:
      boss-thread-prefix: NettyBoss
      client-worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      client-selector-thread-size: 1
      client-selector-thread-prefix: NettyClientWorkerThread

简单使用, 配合dynamic-datasource-spring-boot-starter使用

@Autowired
StaffMapper staffMapper;
    
@Override
@GlobalTransactional(rollbackFor = Exception.class)
public void globalTx() {
    userService.updateMaster();
    userService.updateIndependent();
	//模拟异常回滚
    int i = 1 / 0;
}

@DS("master")
@Transactional(rollbackFor = Exception.class)
public void updateMaster() {
    User user1 = baseDao.selectById(1);
    user1.setAge(999);
    baseDao.updateById(user1);

    User user2 = baseDao.selectById(2);
    user2.setAge(999);
    baseDao.updateById(user2);
}


@DS("independent")
@Transactional(rollbackFor = Exception.class)
public void updateIndependent() {
    Staff staff1 = staffMapper.selectById(1);
    staff1.setAge(999);
    staffMapper.updateById(staff1);


    Staff staff2 = staffMapper.selectById(2);
    staff2.setAge(999);
    staffMapper.updateById(staff2);
}

可以观察到seata_undo_log中的undo记录

SELECT CAST(rollback_info AS char) FROM seata_undo_log
{
    "@class": "io.seata.rm.datasource.undo.BranchUndoLog",
    "xid": "192.168.101.1:8091:6593516322371825665",
    "branchId": 6593516322371825668,
    "sqlUndoLogs": [
        "java.util.ArrayList",
        [
            {
                "@class": "io.seata.rm.datasource.undo.SQLUndoLog",
                "sqlType": "UPDATE",
                "tableName": "staff",
                "beforeImage": {
                    "@class": "io.seata.rm.datasource.sql.struct.TableRecords",
                    "tableName": "staff",
                    "rows": [
                        "java.util.ArrayList",
                        [
                            {
                                "@class": "io.seata.rm.datasource.sql.struct.Row",
                                "fields": [
                                    "java.util.ArrayList",
                                    [
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "id",
                                            "keyType": "PRIMARY_KEY",
                                            "type": 4,
                                            "value": [
                                                "java.lang.Long",
                                                1
                                            ]
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "name",
                                            "keyType": "NULL",
                                            "type": 12,
                                            "value": "1"
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "age",
                                            "keyType": "NULL",
                                            "type": 4,
                                            "value": 2
                                        }
                                    ]
                                ]
                            }
                        ]
                    ]
                },
                "afterImage": {
                    "@class": "io.seata.rm.datasource.sql.struct.TableRecords",
                    "tableName": "staff",
                    "rows": [
                        "java.util.ArrayList",
                        [
                            {
                                "@class": "io.seata.rm.datasource.sql.struct.Row",
                                "fields": [
                                    "java.util.ArrayList",
                                    [
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "id",
                                            "keyType": "PRIMARY_KEY",
                                            "type": 4,
                                            "value": [
                                                "java.lang.Long",
                                                1
                                            ]
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "name",
                                            "keyType": "NULL",
                                            "type": 12,
                                            "value": "1"
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "age",
                                            "keyType": "NULL",
                                            "type": 4,
                                            "value": 999
                                        }
                                    ]
                                ]
                            }
                        ]
                    ]
                }
            },
            {
                "@class": "io.seata.rm.datasource.undo.SQLUndoLog",
                "sqlType": "UPDATE",
                "tableName": "staff",
                "beforeImage": {
                    "@class": "io.seata.rm.datasource.sql.struct.TableRecords",
                    "tableName": "staff",
                    "rows": [
                        "java.util.ArrayList",
                        [
                            {
                                "@class": "io.seata.rm.datasource.sql.struct.Row",
                                "fields": [
                                    "java.util.ArrayList",
                                    [
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "id",
                                            "keyType": "PRIMARY_KEY",
                                            "type": 4,
                                            "value": [
                                                "java.lang.Long",
                                                2
                                            ]
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "name",
                                            "keyType": "NULL",
                                            "type": 12,
                                            "value": "2"
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "age",
                                            "keyType": "NULL",
                                            "type": 4,
                                            "value": 3
                                        }
                                    ]
                                ]
                            }
                        ]
                    ]
                },
                "afterImage": {
                    "@class": "io.seata.rm.datasource.sql.struct.TableRecords",
                    "tableName": "staff",
                    "rows": [
                        "java.util.ArrayList",
                        [
                            {
                                "@class": "io.seata.rm.datasource.sql.struct.Row",
                                "fields": [
                                    "java.util.ArrayList",
                                    [
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "id",
                                            "keyType": "PRIMARY_KEY",
                                            "type": 4,
                                            "value": [
                                                "java.lang.Long",
                                                2
                                            ]
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "name",
                                            "keyType": "NULL",
                                            "type": 12,
                                            "value": "2"
                                        },
                                        {
                                            "@class": "io.seata.rm.datasource.sql.struct.Field",
                                            "name": "age",
                                            "keyType": "NULL",
                                            "type": 4,
                                            "value": 999
                                        }
                                    ]
                                ]
                            }
                        ]
                    ]
                }
            }
        ]
    ]
}

可以看到该表内存储了数据操作前和操作后的记录

微服务项目分布式事务

如果是微服务项目, 需要分布式事务支持, 配置如下

引入依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>

配置和单体服务多库事务是一样的, seata.application-id可以不填, 默认取当前应用id

每个模块都需要配置, 因为seata需要代理数据源

但实际1.4.2版本使用jackson/fastjson序列化Date字段时会失败(https://github.com/seata/seata/issues/3883), 可以替换序列化方式为kryo

需要额外引入依赖

<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-serializer-kryo</artifactId>
    <version>1.4.2</version>
</dependency>

简易demo

@Autowired
RoleFeignClient roleFeignClient;

@Autowired
StaffFeignClient staffFeignClient;
    
@GlobalTransactional(rollbackFor = Exception.class)
public RestResult<Boolean> globalTxTest() {
    log.info("xid: {}", RootContext.getXID());

    roleFeignClient.updateRole();

    staffFeignClient.updateUser();

    int i = 1 / 0;
    return RestResult.ok();
}
@Override
@Transactional(rollbackFor = Exception.class)
public RestResult<Boolean> updateRole() {

    log.info("xid: {}", RootContext.getXID());

    RolePO role1 = roleDAO.selectById(1);
    role1.setName("1111111111");
    roleDAO.updateById(role1);

    RolePO role2 = roleDAO.selectById(2);
    role2.setName("2222222");
    roleDAO.updateById(role2);

    return RestResult.ok();
}
@Override
@Transactional(rollbackFor = Exception.class)
public RestResult<Boolean> updateUser() {

    log.info("xid: {}", RootContext.getXID());

    StaffPO staff1 = baseMapper.selectById(1);
    staff1.setAge(999);
    baseMapper.updateById(staff1);

    StaffPO staff2 = baseMapper.selectById(2);
    staff2.setAge(999);
    baseMapper.updateById(staff2);
    return RestResult.ok();
}


Seata分布式事务环境搭建

下载seata服务端https://github.com/seata/seata/releases修改registry.conf这里使用nacos做注册中心和配置中心, 也就不需要服务端的file.conf了但是使用nacos时, nacos的密码不能有特殊符号, 否则seata可能连接不上regi
2022-03-30

Seata全局事务隔离性

在数据库本地事务隔离级别 读已提交(Read Committed) 或以上的基础上,Seata(AT 模式)的默认全局隔离级别是 读未提交(Read Uncommitted) 。如果应用在特定场景下,必需要求全局的 读已提交 ,目前 Seata 的方式是通过 SELECT FOR UPDATE 语句
2021-01-26

freemarker 时间显示不正常 设置时区

项目在本地开发的时候显示正常,部署上服务器就一直差8个小时,最后发现freemarker官方文档有这样的说明time_zone:时区的名称来显示并格式化时间。 默认情况下,使用JVM的时区。 也可以是 Java 时区 API 接受的值,或者 "JVM default" (从 FreeMarker 2
2020-03-28
IDEA 2019.1 xml 不高亮

IDEA 2019.1 xml 不高亮

前几天更新了idea后,发现xml里的代码都没有了高亮,变得跟记事本一个德性了打开setting ,搜索 File Types,找到xml项, 查看下方的匹配格式,果然没有xml,(idea真是厉害)点击右方的+,输入*.xml,点击ok,解决问题
2020-03-28

npm install 淘宝镜像

npm install --registry=https://registry.npm.taobao.org
2020-03-28
Java中方法的参数传递机制

Java中方法的参数传递机制

来看一段代码 public class Man { private String name; private Integer age; public String getName() { return name; } publi
2020-03-28
基于自定义注解手写权限控制

基于自定义注解手写权限控制

方法一: AOP 方法二: 拦截器项目结构项目依赖<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-w
2020-03-28

Docker 部署 详细全过程 附代码

Docker 部署本站 全过程环境:CentOS7.61. 安装Docker其他版本CentOS可以参考这个https://help.aliyun.com/document_detail/187598.html查看本机内核版本,内核版本需高于 3.10uname -r 确保 yum 包最新yum u
2020-03-28

SpringBoot 启动普通java工程

引入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
2020-03-28

Vue.js DOM操作

<template> <input type="button" @click="reply($event)" value="回复"> </template> export default { methods: { replyFun(e) {
2020-03-29
CentOS7编译调试OpenJDK12

CentOS7编译调试OpenJDK12

1. 下载源码https://hg.openjdk.java.net/jdk/jdk12点击左侧的browse,再点击zip,就可以下载zip格式的源码压缩包。unzip xxx.zip 解压文件2. 安装jdkyum install java-11-openjdk-devel -y3. 运行con
2020-04-23
编写自己的Spring Boot Starter

编写自己的Spring Boot Starter

1.新建一个maven项目命名规则统一是xxx-spring-boot-starter完整pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
2020-06-29