seata实践

seata

下载

https://seata.io/zh-cn/blog/download.html

配置文件修改

conf/registry.conf

registry和config的type都改成nacos

然后指定一个namespace

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"

  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "SEATA_GROUP"
    namespace = "f19b3a88-2e8e-47e6-ac3a-97227baf413c"
    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 = "nacos"

  nacos {
    serverAddr = "127.0.0.1:8848"
    namespace = "f19b3a88-2e8e-47e6-ac3a-97227baf413c"
    group = "SEATA_GROUP"
    username = "nacos"
    password = "nacos"
  }
  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"
  }
}

conf/file.conf

service {
  #transaction service group mapping
  vgroupMapping.my_tx_group="default" #此处根据自己的情况修改
  default.grouplist="127.0.0.1:8091"
  disableGlobalTransaction=false
}
## 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 = "dbcp"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"
    user = "root"
    password = "root"
    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
  }

}

数据表创建语句

 
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `global_table`
(
    `xid`                       VARCHAR(128) NOT NULL,
    `transaction_id`            BIGINT,
    `status`                    TINYINT      NOT NULL,
    `application_id`            VARCHAR(32),
    `transaction_service_group` VARCHAR(32),
    `transaction_name`          VARCHAR(128),
    `timeout`                   INT,
    `begin_time`                BIGINT,
    `application_data`          VARCHAR(2000),
    `gmt_create`                DATETIME,
    `gmt_modified`              DATETIME,
    PRIMARY KEY (`xid`),
    KEY `idx_gmt_modified_status` (`gmt_modified`, `status`),
    KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store BranchSession data
CREATE TABLE IF NOT EXISTS `branch_table`
(
    `branch_id`         BIGINT       NOT NULL,
    `xid`               VARCHAR(128) NOT NULL,
    `transaction_id`    BIGINT,
    `resource_group_id` VARCHAR(32),
    `resource_id`       VARCHAR(256),
    `branch_type`       VARCHAR(8),
    `status`            TINYINT,
    `client_id`         VARCHAR(64),
    `application_data`  VARCHAR(2000),
    `gmt_create`        DATETIME,
    `gmt_modified`      DATETIME,
    PRIMARY KEY (`branch_id`),
    KEY `idx_xid` (`xid`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
    `row_key`        VARCHAR(128) NOT NULL,
    `xid`            VARCHAR(96),
    `transaction_id` BIGINT,
    `branch_id`      BIGINT       NOT NULL,
    `resource_id`    VARCHAR(256),
    `table_name`     VARCHAR(32),
    `pk`             VARCHAR(36),
    `gmt_create`     DATETIME,
    `gmt_modified`   DATETIME,
    PRIMARY KEY (`row_key`),
    KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8;

服务启动

在 *seata\bin* 使用 seata-server.sh或者seata-server.bat

如果是linux环境(要有JRE),执行seata-server.sh

如果是windows环境,执行seata-server.bat

在nacos的服务发现就会显示:

seata实践

nacos 需要的配置信息

nacos-config.sh

​ 在conf文件夹内,需要nacos-config.sh文件。下面有获取地址。

https://github.com/seata/seata/blob/develop/script/config-center/nacos/nacos-config.sh

在seata目录下新建 script 目录(不建也可以,我习惯分开),将 nacos-config.sh 放入script目录下

config.txt

config.txt就是seata各种详细的配置,执行 nacos-config.sh 即可将这些配置导入到nacos,这样就不需要将file.conf和registry.conf放到我们的项目中了,需要什么配置就直接从nacos中读取。

放在conf同级目录

seata实践

下载地址:https://github.com/seata/seata/blob/develop/script/config-center/config.txt

数据库连接

修改为自己的

seata实践

datasource

修改为= “dbcp”

service.vgroup_mapping

使用默认的值,或者修改为自己应用对应的名称;如果有多个服务,添加相应的配置(service.vgroup_mapping后面的值要与spring.cloud.alibaba.seata.tx-service-group对应的值匹配)如:

默认:
service.vgroup_mapping.my_test_tx_group=default

自定义:
service.vgroup_mapping.commission-service-fescar-service-group=default
service.vgroup_mapping.wallet-service-fescar-service-group=default
service.vgroup_mapping.maintainside-service-fescar-service-group=default

如果是用默认,则用下面的,否则根据自定义的值填上,如:commission-service-fescar-service-group

seata实践

seata配置导入nacos

在nacos-config.sh文件所处的地方,打开git bash输入命令

sh nacos-config.sh -h localhost  -g SEATA_GROUP -t f19b3a88-2e8e-47e6-ac3a-97227baf413c
最后面的是想要注册进的nacos命名空间id

seata实践

注意配置文件末尾有空行,需要删除,否则会提示失败,尽管实际上是成功的

init nacos config finished, please start seata-server

出现此提示为成功

yml配置

spring:
  application:
    name: order
    alibaba:
      seata:
        tx-service-group: my_test_tx_group 
        #此处与config.txt的对应:service.vgroupMapping.my_test_tx_group=default

到此就完成了一个低配的seata,可以使用@GlobalTransactional去测试了

参考

https://zhuanlan.zhihu.com/p/103249477

https://hollis.blog.csdn.net/article/details/112386366

https://www.cnblogs.com/zhangXingSheng/p/13473993.html

https://blog.csdn.net/qq853632587/article/details/111644295

上一篇:seata闪退问题(总结)!!!!


下一篇:分布式事务及seata基本使用