alibaba seata服务端具体实现


Posted in Java/Android onFebruary 24, 2022

seata是什么?

seata是来处理分布式服务之间互相调用的事务问题。

Seata术语

TC (Transaction Coordinator) - 事务协调者

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

TM (Transaction Manager) - 事务管理器

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

RM (Resource Manager) - 资源管理器

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

seata中文文档:Seata 是什么

seata下载地址:https://github.com/seata/seata/releases

seata服务端具体实现

我下载的是1.3.0 

alibaba seata服务端具体实现

 解压后目录结构

alibaba seata服务端具体实现

 修改配置文件:registry.conf 注册信息。type默认是file,我要注册到nacos,修改nacos配置信息。

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
 
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:3333"
    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"
  }
}

file.conf配置数据库信息,这里我配置的是db。要新建数据库。添加四张表。

branch_table、global_table、lock_table、undo_log。1.3.0版本没有给我们sql。其它版本有sql写在下面

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
 
  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }
 
  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.cj.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
    user = "root"
    password = "123456"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
 
  ## redis store property
  redis {
    host = "127.0.0.1"
    port = "6379"
    password = ""
    database = "0"
    minConn = 1
    maxConn = 10
    queryLimit = 100
  }
 
}

sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for branch_table
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table`  (
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `status` tinyint(4) DEFAULT NULL,
  `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `gmt_create` datetime(6) DEFAULT NULL,
  `gmt_modified` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`branch_id`) USING BTREE,
  INDEX `idx_xid`(`xid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Table structure for global_table
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table`  (
  `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `status` tinyint(4) NOT NULL,
  `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `timeout` int(11) DEFAULT NULL,
  `begin_time` bigint(20) DEFAULT NULL,
  `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `gmt_create` datetime(0) DEFAULT NULL,
  `gmt_modified` datetime(0) DEFAULT NULL,
  PRIMARY KEY (`xid`) USING BTREE,
  INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,
  INDEX `idx_transaction_id`(`transaction_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Table structure for lock_table
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table`  (
  `row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `branch_id` bigint(20) NOT NULL,
  `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `gmt_create` datetime(0) DEFAULT NULL,
  `gmt_modified` datetime(0) DEFAULT NULL,
  PRIMARY KEY (`row_key`) USING BTREE,
  INDEX `idx_branch_id`(`branch_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime(0) NOT NULL,
  `log_modified` datetime(0) NOT NULL,
  `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
 
SET FOREIGN_KEY_CHECKS = 1;

启动你自己的nacos注册中心。

然后在bin目录下双击seata-server.bat文件启动。

查看nacos注册中心,发现有seata的服务,就表示成功了。

alibaba seata服务端具体实现

 seata客户端

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.xx.job</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>seata-order-service2001</artifactId>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.xx.job</groupId>
            <artifactId>cloud-common</artifactId>
            <version>${project.version}</version>
        </dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <artifactId>spring-boot-starter-web</artifactId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <groupId>io.seata</groupId>
            <artifactId>seata-all</artifactId>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
        </plugins>
    </build>
</project>

yml文件

server:
  port: 2001
 
spring:
  application:
    name: seata-order-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:3333
      config:
        file-extension: yml
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/order?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  type-aliases-package: com.xx.job.entity
  mapper-locations: classpath:mapper/*.xml
#====================================Seata Config===============================================
seata:
  enabled: true
  application-id: account-seata-example
  tx-service-group: account-service-seata-service-group # 事务群组(可以每个应用独立取名,也可以使用相同的名字)
  client:
    rm-report-success-enable: true
    rm-table-meta-check-enable: false # 自动刷新缓存中的表结构(默认false)
    rm-report-retry-count: 5 # 一阶段结果上报TC重试次数(默认5)
    rm-async-commit-buffer-limit: 10000 # 异步提交缓存队列长度(默认10000)
    rm:
      lock:
        lock-retry-internal: 10 # 校验或占用全局锁重试间隔(默认10ms)
        lock-retry-times: 30 # 校验或占用全局锁重试次数(默认30)
        lock-retry-policy-branch-rollback-on-conflict: true # 分支事务与其它全局回滚事务冲突时锁策略(优先释放本地锁让回滚成功)
    tm-commit-retry-count: 3 # 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
    tm-rollback-retry-count: 3 # 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
    undo:
      undo-data-validation: true # 二阶段回滚镜像校验(默认true开启)
      undo-log-serialization: jackson # undo序列化方式(默认jackson)
      undo-log-table: undo_log  # 自定义undo表名(默认undo_log)
    log:
      exceptionRate: 100 # 日志异常输出概率(默认100)
    support:
      spring:
        datasource-autoproxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default # TC 集群(必须与seata-server保持一致)
    enable-degrade: false # 降级开关
    disable-global-transaction: false # 禁用全局事务(默认false)
    grouplist:
      default: 127.0.0.1:8091
  transport:
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: NettyBoss
      worker-thread-prefix: NettyServerNIOWorker
      server-executor-thread-prefix: NettyServerBizHandler
      share-boss-worker: false
      client-selector-thread-prefix: NettyClientSelector
      client-selector-thread-size: 1
      client-worker-thread-prefix: NettyClientWorkerThread
    type: TCP
    server: NIO
    heartbeat: true
    serialization: seata
    compressor: none
    enable-client-batch-send-request: true # 客户端事务消息请求是否批量合并发送(默认true)
  registry:
    file:
      name: file.conf
    type: nacos
      server-addr: localhost:3333
      namespace:
      cluster: default
  config:

 业务代码需要加上两个注解,本地事物和分布式事物。

@Transactional@GlobalTransactional
package com.xx.job.seataorderservice2001.controller;
 
import com.xx.job.common.CommonResult;
import com.xx.job.entity.Account;
import com.xx.job.entity.Order;
import com.xx.job.entity.Storage;
import com.xx.job.seataorderservice2001.service.AccountService;
import com.xx.job.seataorderservice2001.service.OrderService;
import com.xx.job.seataorderservice2001.service.StorageSerivce;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    private AccountService accountService;
    private StorageSerivce storageSerivce;
    /**
     * 加订单 减余额 减库存
     * @param order
     * @return
     */
    @Transactional
    @GlobalTransactional
    @RequestMapping("/order/create")
    public CommonResult create(Order order){
        // 加订单
        orderService.create(order);
        // 减余额
        Account account = new Account();
        account.setUserId(order.getUserId());
        account.setMoney(80);
        accountService.update(account);
        // 减库存
        Storage storage = new Storage();
        storage.setCount(999);
        storage.setCommodityCode(order.getCommodityCode());
        storageSerivce.update(storage);
        return new CommonResult(200,"添加订单成功!");
    }
}

具体demo   gitee:spring-cloud-demo: spring-cloud-demo试例

到此这篇关于alibaba-seata实现的文章就介绍到这了,更多相关alibaba-seata实现内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
Java 将PPT幻灯片转为HTML文件的实现思路
Jun 11 Java/Android
新手入门Jvm-- JVM对象创建与内存分配机制
Jun 18 Java/Android
Java SSH 秘钥连接mysql数据库的方法
Jun 28 Java/Android
Sleuth+logback 设置traceid 及自定义信息方式
Jul 26 Java/Android
JVM钩子函数的使用场景详解
Aug 23 Java/Android
关于Mybatis中SQL节点的深入解析
Mar 19 Java/Android
Java 超详细讲解IO操作字节流与字符流
Mar 25 Java/Android
spring注解 @PropertySource配置数据源全流程
Mar 25 Java/Android
Spring依赖注入多种类型数据的示例代码
Mar 31 Java/Android
Android基于Fresco实现圆角和圆形图片
Apr 01 Java/Android
Java 深入探究讲解简单工厂模式
Apr 07 Java/Android
Java Spring读取和存储详细操作
Aug 05 Java/Android
java协程框架quasar和kotlin中的协程对比分析
Feb 24 #Java/Android
springmvc直接不经过controller访问WEB-INF中的页面问题
Feb 24 #Java/Android
正则表达式拆分url实例代码
Feb 24 #Java/Android
mybatis源码解读之executor包语句处理功能
Feb 15 #Java/Android
java executor包参数处理功能 
Feb 15 #Java/Android
Java如何实现通过键盘输入一个数组
Feb 15 #Java/Android
Java实现给Word文件添加文字水印
Feb 15 #Java/Android
You might like
用PHP实现多服务器共享SESSION数据的方法
2007/03/16 PHP
PHP中PDO基础教程 入门级
2011/09/04 PHP
php图片的裁剪与缩放生成符合需求的缩略图
2013/01/11 PHP
PHP小技巧之函数重载
2014/06/02 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
2015/12/31 PHP
PHP使用星号隐藏用户名,手机和邮箱的实现方法
2016/09/22 PHP
PHP常用日期加减计算方法实例小结
2018/07/31 PHP
thinkPHP事务操作简单案例分析
2019/10/17 PHP
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
JavaScript中的toLocaleDateString()方法使用简介
2015/06/12 Javascript
jQuery.each使用详解
2015/07/07 Javascript
jquery+json实现分页效果
2016/03/07 Javascript
BootStrap modal模态弹窗使用小结
2016/10/26 Javascript
详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式
2017/06/17 Javascript
vue2.0 keep-alive最佳实践
2017/07/06 Javascript
基于js Canvas实现二次贝塞尔曲线
2018/12/25 Javascript
新手快速上手webpack4打包工具的使用详解
2019/01/28 Javascript
微信小程序云开发之云函数详解
2019/05/16 Javascript
微信小程序如何实现全局重新加载
2019/06/05 Javascript
小程序实现搜索框功能
2020/03/26 Javascript
Node.js API详解之 dns模块用法实例分析
2020/05/15 Javascript
浅谈vue.watch的触发条件是什么
2020/11/07 Javascript
简单介绍Python中的decode()方法的使用
2015/05/18 Python
浅谈python中的面向对象和类的基本语法
2016/06/13 Python
Python队列、进程间通信、线程案例
2019/10/25 Python
区分python中的进程与线程
2020/08/13 Python
JBL澳大利亚官方商店:扬声器、耳机和音响系统
2018/05/24 全球购物
决心书范文
2014/03/11 职场文书
留守儿童工作方案
2014/06/02 职场文书
个人授权委托书模板
2014/09/14 职场文书
圆明园观后感
2015/06/03 职场文书
返乡农民工证明
2015/06/24 职场文书
Python基础之pandas数据合并
2021/04/27 Python
JavaScript控制台的更多功能
2021/04/28 Javascript
python中的class_static的@classmethod的巧妙用法
2021/06/22 Python
MySQL自定义函数及触发器
2022/08/05 MySQL