Simple Extensible Autonomous Transacation Architecture,seata是简单的、可扩展、自主性高的分布式架构

SEATA Server Configure

因我们使用正式的1.0.0-GA 版本,网上大多数找到的说明都是0.X版本,有不少变动,比如,在server中取消了db_store.sql的脚本,如找不到相关内容,可以通过源码来查找,比如db脚本源码:mysql db script

  1. 下载 seata-server

  2. 创建数据库(seata),可自定义,在file.conf中要用到。

    1. -- -------------------------------- The script used when storeMode is 'db' --------------------------------
    2. -- the table to store GlobalSession data
    3. CREATE TABLE IF NOT EXISTS `global_table`
    4. (
    5. `xid` VARCHAR(128) NOT NULL,
    6. `transaction_id` BIGINT,
    7. `status` TINYINT NOT NULL,
    8. `application_id` VARCHAR(32),
    9. `transaction_service_group` VARCHAR(32),
    10. `transaction_name` VARCHAR(128),
    11. `timeout` INT,
    12. `begin_time` BIGINT,
    13. `application_data` VARCHAR(2000),
    14. `gmt_create` DATETIME,
    15. `gmt_modified` DATETIME,
    16. PRIMARY KEY (`xid`),
    17. KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    18. KEY `idx_transaction_id` (`transaction_id`)
    19. ) ENGINE = InnoDB
    20. DEFAULT CHARSET = utf8;
    21. -- the table to store BranchSession data
    22. CREATE TABLE IF NOT EXISTS `branch_table`
    23. (
    24. `branch_id` BIGINT NOT NULL,
    25. `xid` VARCHAR(128) NOT NULL,
    26. `transaction_id` BIGINT,
    27. `resource_group_id` VARCHAR(32),
    28. `resource_id` VARCHAR(256),
    29. `branch_type` VARCHAR(8),
    30. `status` TINYINT,
    31. `client_id` VARCHAR(64),
    32. `application_data` VARCHAR(2000),
    33. `gmt_create` DATETIME,
    34. `gmt_modified` DATETIME,
    35. PRIMARY KEY (`branch_id`),
    36. KEY `idx_xid` (`xid`)
    37. ) ENGINE = InnoDB
    38. DEFAULT CHARSET = utf8;
    39. -- the table to store lock data
    40. CREATE TABLE IF NOT EXISTS `lock_table`
    41. (
    42. `row_key` VARCHAR(128) NOT NULL,
    43. `xid` VARCHAR(96),
    44. `transaction_id` BIGINT,
    45. `branch_id` BIGINT NOT NULL,
    46. `resource_id` VARCHAR(256),
    47. `table_name` VARCHAR(32),
    48. `pk` VARCHAR(36),
    49. `gmt_create` DATETIME,
    50. `gmt_modified` DATETIME,
    51. PRIMARY KEY (`row_key`),
    52. KEY `idx_branch_id` (`branch_id`)
    53. ) ENGINE = InnoDB
    54. DEFAULT CHARSET = utf8;
  3. 编辑file.conf配置文件

    1. service {
    2. #transaction service group mapping
    3. vgroup_mapping.sunrise_tx_group = "default"
    4. #only support when registry.type=file, please don't set multiple addresses
    5. default.grouplist = "127.0.0.1:8091"
    6. #disable seata
    7. disableGlobalTransaction = false
    8. #degrade current not support
    9. enableDegrade = false
    10. #disable
    11. disable = false
    12. #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
    13. max.commit.retry.timeout = "-1"
    14. max.rollback.retry.timeout = "-1"
    15. }
    16. ## transaction log store, only used in seata-server
    17. store {
    18. ## store mode: file、db
    19. mode = "db"
    20. ## file store property
    21. file {
    22. ## store location dir
    23. dir = "sessionStore"
    24. #degrade current not support
    25. enableDegrade = false
    26. #disable
    27. disable = false
    28. #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
    29. max.commit.retry.timeout = "-1"
    30. max.rollback.retry.timeout = "-1"
    31. }
    32. ## transaction log store, only used in seata-server
    33. store {
    34. ## store mode: file、db
    35. mode = "db"
    36. ## file store property
    37. file {
    38. ## store location dir
    39. dir = "sessionStore"
    40. }
    41. ## database store property
    42. db {
    43. ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    44. datasource = "dbcp"
    45. ## mysql/oracle/h2/oceanbase etc.
    46. db-type = "mysql"
    47. driver-class-name = "com.mysql.jdbc.Driver"
    48. url = "jdbc:mysql://localhost:3306/seata"
    49. user = "wr"
    50. password = "wr"
    51. min-conn = 1
    52. max-conn = 3
    53. global.table = "global_table"
    54. branch.table = "branch_table"
    55. lock-table = "lock_table"
    56. query-limit = 100
    57. }
    58. }
  4. 编辑conf/regiester.conf

    1. registry {
    2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
    3. # 更改1
    4. type = "eureka"
    5. nacos {
    6. serverAddr = "localhost"
    7. namespace = ""
    8. cluster = "default"
    9. }
    10. eureka {
    11. # 更改2
    12. serviceUrl = "http://localhost:21001/eureka"
    13. application = "default"
    14. weight = "1"
    15. }
    16. redis {
    17. serverAddr = "localhost:6379"
    18. db = "0"
    19. }
    20. zk {
    21. cluster = "default"
    22. serverAddr = "127.0.0.1:2181"
    23. session.timeout = 6000
    24. connect.timeout = 2000
    25. }
    26. consul {
    27. cluster = "default"
    28. serverAddr = "127.0.0.1:8500"
    29. }
    30. etcd3 {
    31. cluster = "default"
    32. serverAddr = "http://localhost:2379"
    33. }
    34. sofa {
    35. serverAddr = "127.0.0.1:9603"
    36. application = "default"
    37. region = "DEFAULT_ZONE"
    38. datacenter = "DefaultDataCenter"
    39. cluster = "default"
    40. group = "SEATA_GROUP"
    41. addressWaitTime = "3000"
    42. }
    43. file {
    44. name = "file.conf"
    45. }
    46. }
    47. config {
    48. # file、nacos 、apollo、zk、consul、etcd3
    49. type = "file"
    50. nacos {
    51. serverAddr = "localhost"
    52. namespace = ""
    53. }
    54. consul {
    55. serverAddr = "127.0.0.1:8500"
    56. }
    57. apollo {
    58. app.id = "seata-server"
    59. apollo.meta = "http://192.168.1.204:8801"
    60. }
    61. zk {
    62. serverAddr = "127.0.0.1:2181"
    63. session.timeout = 6000
    64. connect.timeout = 2000
    65. }
    66. etcd3 {
    67. serverAddr = "http://localhost:2379"
    68. }
    69. file {
    70. name = "file.conf"
    71. }
    72. }

    本次实现,我们还没有添加configration的时候,因此只需要更改register块中的相关属性即可,在后续我们实现了nacos config之后,再同步更新这里。

  5. 启动服务端

    1. [root@localhost.localdomain /usr/local/geekplus/server/seata]# bin/seata-server.sh



eureka中看到如下8091端口的服务之后,即启动seata-server正常!

Client Configure

AT Model

需要在每一个业务库中添加数据表undo_log.

  1. CREATE TABLE `undo_log` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `branch_id` bigint(20) NOT NULL,
  4. `xid` varchar(100) NOT NULL,
  5. `context` varchar(128) NOT NULL,
  6. `rollback_info` longblob NOT NULL,
  7. `log_status` int(11) NOT NULL,
  8. `log_created` datetime NOT NULL,
  9. `log_modified` datetime NOT NULL,
  10. `ext` varchar(100) DEFAULT NULL,
  11. PRIMARY KEY (`id`),
  12. UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
  13. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

Broadway-Demo

使用Springboot项目有一个小规律,我叫它为使用三部曲。

broadway-ws-tally-service项目为例:

  • 第一步:加依赖

    1. <!--seata-->
    2. <dependency>
    3. <groupId>com.alibaba.cloud</groupId>
    4. <artifactId>spring-cloud-alibaba-seata</artifactId>
    5. <version>2.1.0.RELEASE</version>
    6. <exclusions>
    7. <exclusion>
    8. <artifactId>seata-all</artifactId>
    9. <groupId>io.seata</groupId>
    10. </exclusion>
    11. </exclusions>
    12. </dependency>
    13. <dependency>
    14. <groupId>io.seata</groupId>
    15. <artifactId>seata-all</artifactId>
    16. <version>1.0.0</version>
    17. </dependency>
  • 第二步:开启注解

    1. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

    在这里需要说明一下,我们需要排除掉SpringBoot默认自动注入的DataSourceAutoConfigurationBean, 因为SEATA是基于数据源拦截来实现的分布式事务,因此,我们需要自定义数据源配置信息:

    1. package com.geekplus.broadway.ws.tally.application;
    2. import com.alibaba.druid.pool.DruidDataSource;
    3. import io.seata.rm.datasource.DataSourceProxy;
    4. import org.apache.ibatis.session.SqlSessionFactory;
    5. import org.mybatis.spring.SqlSessionFactoryBean;
    6. import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
    7. import org.springframework.boot.context.properties.ConfigurationProperties;
    8. import org.springframework.context.annotation.Bean;
    9. import org.springframework.context.annotation.Configuration;
    10. import org.springframework.context.annotation.Primary;
    11. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    12. import javax.sql.DataSource;
    13. @Configuration
    14. public class DataSourceConfiguration {
    15. @Bean
    16. @ConfigurationProperties(prefix = "spring.datasource")
    17. public DataSource druidDataSource(){
    18. DruidDataSource druidDataSource = new DruidDataSource();
    19. return druidDataSource;
    20. }
    21. @Primary
    22. @Bean("dataSource")
    23. public DataSourceProxy dataSource(DataSource druidDataSource){
    24. return new DataSourceProxy(druidDataSource);
    25. }
    26. @Bean
    27. public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{
    28. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    29. sqlSessionFactoryBean.setDataSource(dataSourceProxy);
    30. sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
    31. .getResources("classpath*:/com/geekplus/*.xml"));
    32. sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
    33. return sqlSessionFactoryBean.getObject();
    34. }
    35. }

    该配置类,一般放在与启动类相同的目录即可!

  • 第三部:改配置

    1. spring:
    2. cloud:
    3. alibaba:
    4. seata:
    5. tx-service-group: sunrise_tx_group

    TIPS:

    tx-service-group 一定要server,client都要保持一致,否则能坑死你!

    tx-service-group 一定要server,client都要保持一致,否则能坑死你!

    tx-service-group 一定要server,client都要保持一致,否则能坑死你!

以上三步是大多数SpringBoot 项目的通用做法,当然我们的SEATA暂时还不是那么完善,配置信息目前还不支持在application.yml中配置完全,因此,我们需要2个核心的client 配置文件file.confregister.conf.

  • file.conf 该文件主要是用于连接RM的,我们只需要关注上面所说的tx-service-group就行,其他配置默认就可以。

    1. transport {
    2. # tcp udt unix-domain-socket
    3. type = "TCP"
    4. #NIO NATIVE
    5. server = "NIO"
    6. #enable heartbeat
    7. heartbeat = true
    8. # the client batch send request enable
    9. enable-client-batch-send-request = true
    10. #thread factory for netty
    11. thread-factory {
    12. boss-thread-prefix = "NettyBoss"
    13. worker-thread-prefix = "NettyServerNIOWorker"
    14. server-executor-thread-prefix = "NettyServerBizHandler"
    15. share-boss-worker = false
    16. client-selector-thread-prefix = "NettyClientSelector"
    17. client-selector-thread-size = 1
    18. client-worker-thread-prefix = "NettyClientWorkerThread"
    19. # netty boss thread size,will not be used for UDT
    20. boss-thread-size = 1
    21. #auto default pin or 8
    22. worker-thread-size = 8
    23. }
    24. shutdown {
    25. # when destroy server, wait seconds
    26. wait = 3
    27. }
    28. serialization = "seata"
    29. compressor = "none"
    30. }
    31. service {
    32. #transaction service group mapping
    33. vgroup_mapping.sunrise_tx_group = "default"
    34. #only support when registry.type=file, please don't set multiple addresses
    35. default.grouplist = "127.0.0.1:8091"
    36. #disable seata
    37. disableGlobalTransaction = false
    38. }
    39. client {
    40. rm {
    41. async.commit.buffer.limit = 10000
    42. lock {
    43. retry.internal = 10
    44. retry.times = 30
    45. retry.policy.branch-rollback-on-conflict = true
    46. }
    47. report.retry.count = 5
    48. table.meta.check.enable = false
    49. report.success.enable = true
    50. }
    51. tm {
    52. commit.retry.count = 5
    53. rollback.retry.count = 5
    54. }
    55. undo {
    56. data.validation = true
    57. log.serialization = "jackson"
    58. log.table = "undo_log"
    59. }
    60. log {
    61. exceptionRate = 100
    62. }
    63. support {
    64. # auto proxy the DataSource bean
    65. spring.datasource.autoproxy = false
    66. }
    67. }
  • register.conf该文件是用于指定注册中心相关配置的,它有2个核心block

    • registry块

      此模板表明我们的注册中心使用类型,需要根据type设置来同步更新使用的配置信息,比如我们使用的eureka,因此我们更新eureka.serviceUrl="http://172.16.1.187:21001/eureka".

    • config 块

      该模块用来设置我们的配置中心相关配置(此处后续更新,我们会选择apollo

    1. registry {
    2. # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
    3. type = "eureka"
    4. nacos {
    5. serverAddr = "localhost"
    6. namespace = ""
    7. cluster = "default"
    8. }
    9. eureka {
    10. serviceUrl = "http://172.16.1.187:21001/eureka"
    11. application = "default"
    12. weight = "1"
    13. }
    14. redis {
    15. serverAddr = "localhost:6379"
    16. db = "0"
    17. }
    18. zk {
    19. cluster = "default"
    20. serverAddr = "127.0.0.1:2181"
    21. session.timeout = 6000
    22. connect.timeout = 2000
    23. }
    24. consul {
    25. cluster = "default"
    26. serverAddr = "127.0.0.1:8500"
    27. }
    28. etcd3 {
    29. cluster = "default"
    30. serverAddr = "http://localhost:2379"
    31. }
    32. sofa {
    33. serverAddr = "127.0.0.1:9603"
    34. application = "default"
    35. region = "DEFAULT_ZONE"
    36. datacenter = "DefaultDataCenter"
    37. cluster = "default"
    38. group = "SEATA_GROUP"
    39. addressWaitTime = "3000"
    40. }
    41. file {
    42. name = "file.conf"
    43. }
    44. }
    45. config {
    46. # file、nacos 、apollo、zk、consul、etcd3
    47. type = "file"
    48. nacos {
    49. serverAddr = "localhost"
    50. namespace = ""
    51. }
    52. consul {
    53. serverAddr = "127.0.0.1:8500"
    54. }
    55. apollo {
    56. app.id = "seata-server"
    57. apollo.meta = "http://192.168.1.204:8801"
    58. }
    59. zk {
    60. serverAddr = "127.0.0.1:2181"
    61. session.timeout = 6000
    62. connect.timeout = 2000
    63. }
    64. etcd3 {
    65. serverAddr = "http://localhost:2379"
    66. }
    67. file {
    68. name = "file.conf"
    69. }
    70. }

SEATA 分布式事务入门DEMO的更多相关文章

  1. Dubbo学习系列之十五(Seata分布式事务方案TCC模式)

    上篇的续集. 工具: Idea201902/JDK11/Gradle5.6.2/Mysql8.0.11/Lombok0.27/Postman7.5.0/SpringBoot2.1.9/Nacos1.1 ...

  2. SpringCloud微服务实战——搭建企业级开发框架(二十七):集成多数据源+Seata分布式事务+读写分离+分库分表

    读写分离:为了确保数据库产品的稳定性,很多数据库拥有双机热备功能.也就是,第一台数据库服务器,是对外提供增删改业务的生产服务器:第二台数据库服务器,主要进行读的操作. 目前有多种方式实现读写分离,一种 ...

  3. Seata搭建与分布式事务入门

    在单体架构下,我们大多使用的是单体数据库,通过数据库的ACID特性支持,实现了本地事务.但是在微服务架构下复杂的业务关系中,分布式事务是不可避免的问题之一.Seata是Spring Cloud Ali ...

  4. spring boot:shardingsphere+druid多数据源整合seata分布式事务(spring boot 2.3.3)

    一,为什么要给shardingsphere配置多数据源? 1,shardingjdbc默认接管了所有的数据源, 如果我们有多个非分表的库时,则最多只能设置一个为默认数据库, 其他的非分表数据库不能访问 ...

  5. spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)

    一,shardingshpere为什么要整合seata? 分库分表是数据库扩展中最常用的处理方法, shardingshpere作为使用最广泛的分表中间件, 如果不支持分布式事务,则它的数据一致性就会 ...

  6. Dubbo学习系列之十四(Seata分布式事务方案AT模式)

    一直说写有关最新技术的文章,但前面似乎都有点偏了,只能说算主流技术,今天这个主题,我觉得应该名副其实.分布式微服务的深水区并不是单个微服务的设计,而是服务间的数据一致性问题!解决了这个问题,才算是把分 ...

  7. Seata–分布式事务

    10.1 分布式事务基础 10.1.1 事务 事务指的就是一个操作单元,在这个操作单元中的所有操作最终要保持一致的行为,要么所有操作都成功,要么所有的操作都被撤销.简单地说,事务提供一种"要 ...

  8. Seata分布式事务失败通知

    一.背景 在我们使用Seata作为分布式事务时,有些时候我们的分布式时候并不是每次都可以成功的,而对于这些失败的分布式事务就需要进行通知.这篇文章简单记录一下如何实现通知. 二.功能实现 此处模拟邮件 ...

  9. 5-4 Seata 分布式事务管理

    下载Seata https://github.com/seata/seata/releases https://github.com/seata/seata/releases/download/v1. ...

随机推荐

  1. 2019-8-31-jekyll-在博客添加流程图

    title author date CreateTime categories jekyll 在博客添加流程图 lindexi 2019-08-31 16:55:59 +0800 2018-2-13 ...

  2. css写一个计算器叭

    显示效果如图,emoji可替换为数字.

  3. pycharm解析器找不到

    pycharm每个项目都需要设定解释器位置吧,把自己解释器位置添加到项目设置就好了 先装python,再装pycharm,不然找不到解析器的,解析器其实就是python.exe.

  4. Python--day34--前面网络编程的复习

  5. ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(14)之会员中心管理

    源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 会员中心,主要包含了会员 ...

  6. es6笔记 day2---解构赋值

    解构赋值 这个知识点非常有用,特别是在做数据交互的时候(Ajax).那么它是怎么使用的呢? 它就是这么使用的↓ let [a,b,c] = [12,5,6];  这就是解构赋值 注意:左右两边,结构格 ...

  7. 0012 sublime快捷操作emmet语法

    Emmet的前身是Zen coding,它使用缩写,来提高html/css的编写速度. 生成标签 直接输入标签名 按tab键即可 比如 div 然后tab 键, 就可以生成 如果想要生成多个相同标签 ...

  8. Java方法的参数传递是值传递还是引用传递?

    当基本数据类型(Boolean,byte,char,String,int,Long,float,double)作为参数传递时,传递的是实参值的副本,即传的是值,无论在函数中怎么操作这个副本,实参的值是 ...

  9. nginx实现缓存功能

    Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...

  10. 如何利用腾讯云COS为静态博客添加动态相册

    前言 本文首发于个人网站Jianger's Blog,欢迎访问订阅.个人博客小站刚建站不久,想着除了主题里的功能外再添加上相册模块,于是半搜索半摸索把相册模块搞出来了,最后采用了利用腾讯云对象存储作图 ...