美文网首页
seata1.2.0搭建

seata1.2.0搭建

作者: 浅乔未果 | 来源:发表于2020-07-28 13:17 被阅读0次

    选择版本说明

    Snipaste_2020-07-27_19-22-11.png

    在LINUX下配置

    nacos自己参考搭建

    1. 下载服务

    首先访问:https://seata.io/zh-cn/blog/download.html

    下载我们需要使用的seata1.2服务

    2. 创建表和数据库

    1. 在你的参与全局事务的数据库中加入undo_log这张表

    表的查找:

    在官网下查找资源,下载官方配置资源

    Snipaste_2020-07-28_10-22-00.png

    根据下载到的资源下查找相对应的表sql导入

    Snipaste_2020-07-28_10-22-57.png

    附上:

    -- for AT mode you must to init this sql for you business database. the seata server not need it.
    CREATE TABLE IF NOT EXISTS `undo_log`
    (
        `id`            BIGINT(20)   NOT NULL AUTO_INCREMENT COMMENT 'increment id',
        `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
        `xid`           VARCHAR(100) NOT NULL COMMENT 'global transaction id',
        `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
        `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
        `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
        `log_created`   DATETIME     NOT NULL COMMENT 'create datetime',
        `log_modified`  DATETIME     NOT NULL COMMENT 'modify datetime',
        PRIMARY KEY (`id`),
        UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
    ) ENGINE = InnoDB
      AUTO_INCREMENT = 1
      DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
    
    1. 在你的mysql数据库中创建名为seata的库,并使用以下下sql

    根据下载到的资源下查找相对应的表sql导入

    Snipaste_2020-07-28_10-31-24.png

    附上:

    -- -------------------------------- The script used when storeMode is 'db' --------------------------------
    -- the table to store GlobalSession data
    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(6),
        `gmt_modified`      DATETIME(6),
        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;
    

    3. 修改config.txt文件

    根据下载到的资源下查找相对应的文件,在以往版本中为nacos-config.txt

    Snipaste_2020-07-28_10-35-43.png

    修改后:

    service.vgroupMapping.nacos_seata_tx_group=default
    service.default.grouplist=127.0.0.1:8091
    service.enableDegrade=false
    service.disableGlobalTransaction=false
    store.mode=db
    store.db.datasource=druid
    store.db.dbType=mysql
    store.db.driverClassName=com.mysql.jdbc.Driver
    store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
    store.db.user=root
    store.db.password=root
    store.db.minConn=5
    store.db.maxConn=30
    store.db.globalTable=global_table
    store.db.branchTable=branch_table
    store.db.queryLimit=100
    store.db.lockTable=lock_table
    store.db.maxWait=5000
    
    • 注意vgroupMapping在旧版中使用的是vgroup_mapping,nacos_seata_tx_group自行取名,在下文中需要配置

    • 数据库地址,用户名密码,如果是mysql8的需要修改驱动名称并加入相对应的驱动包

    运行并将资源导入nacos

    [root@localhost conf]# ./nacos-config.sh
    -bash: ./nacos-config.sh: Permission denied
    [root@localhost conf]# chmod 777 nacos-config.sh
    [root@localhost conf]# ./nacos-config.sh
    set nacosAddr=localhost:8848
    set group=SEATA_GROUP
    Set service.vgroupMapping.nacos_seata_tx_group=default successfully 
    Set service.default.grouplist=127.0.0.1:8091 successfully 
    Set service.enableDegrade=false successfully 
    Set service.disableGlobalTransaction=false successfully 
    Set store.mode=db successfully 
    Set store.db.datasource=druid successfully 
    Set store.db.dbType=mysql successfully 
    Set store.db.driverClassName=com.mysql.jdbc.Driver successfully 
    Set store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true successfully 
    Set store.db.user=root successfully 
    Set store.db.password=57328242 successfully 
    Set store.db.minConn=5 successfully 
    Set store.db.maxConn=30 successfully 
    Set store.db.globalTable=global_table successfully 
    Set store.db.branchTable=branch_table successfully 
    Set store.db.queryLimit=100 successfully 
    Set store.db.lockTable=lock_table successfully 
    Set store.db.maxWait=5000 successfully 
    =========================================================================
     Complete initialization parameters,  total-count:18 ,  failure-count:0 
    =========================================================================
     Init nacos config finished, please start seata-server.
    

    导入成功后:查看nacos出现相关的配置

    Snipaste_2020-07-28_10-58-22.png

    创建dev命名空间

    Snipaste_2020-07-28_12-13-11.png

    将public中的配置信息导入到dev中

    Snipaste_2020-07-28_12-14-32.png

    4. 在idea引入相对应的依赖

    springcloud:

     <!--seata-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-seata</artifactId>
                <version>2.2.0.RELEASE</version>
                <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.2.0</version>
            </dependency>
    

    5. 更改server中的registry.conf

    registry {
      # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
      type = "nacos"
    
      nacos {
        application = "seata-server"
        serverAddr = "localhost"
        namespace = "1ef39358-af4a-438b-8cf0-1ea1fb31de5f"  #导入dev命名空间的id
        cluster = "default"
        username = "nacos"
        password = "nacos"
    }   
    }
    
    config {
      # file、nacos 、apollo、zk、consul、etcd3
      type = "nacos"
    
      nacos {
        serverAddr = "localhost"
        namespace = "1ef39358-af4a-438b-8cf0-1ea1fb31de5f" #导入dev命名空间的id
        group = "SEATA_GROUP"
        username = "nacos"
        password = "nacos"
      }
    }
    

    6. 进入bin目录下启动seata

    sh seata-server.sh -p 8091 -h 192.168.235.130
    

    官网提供相关命令

    • 命令启动: seata-server.sh -h 127.0.0.1 -p 8091 -m db -n 1 -e test
    • 开启多个:seata-server.sh -h 127.0.0.1 -p 8092 -m db -n 1 -e test(直接在另外的窗口修改端口号注册)


      Snipaste_2020-07-28_13-04-40.png
    • 官网提供相关命令
        -h: 注册到注册中心的ip
        -p: Server rpc 监听端口
        -m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数 (Seata-Server 1.3及以上版本支持redis)
        -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
        -e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
    

    7. 创建application.yml

    根据下载到的资源下查找相对应的文件,并修改(除必要配置,默认即可)

    Snipaste_2020-07-28_11-10-05.png
    seata:
      enabled: true
      application-id: applicationName
      tx-service-group: nacos_seata_tx_group    #与上文配置相对应
      config:
        type: nacos
        nacos:
          namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f #与上文对应
          serverAddr: 192.168.235.130:8848 #nacos的地址和端口
          group: SEATA_GROUP
          userName: "nacos"
          password: "nacos"
      registry:
        type: nacos
        nacos:
          application: seata-server
          server-addr: 192.168.235.130:8848
          namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f #与上文对应
          userName: "nacos"
          password: "nacos"
    

    8. 启动微服务

    druid详细配置信息缺失

    Snipaste_2020-07-28_12-17-19.png

    加入druid配置信息

    yaml完整配置

    spring:
      application:
        name: seata-order-service
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.235.130:8848
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://192.168.235.130:3306/seata_order?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: root
        password: 57328242
        driver-class-name: org.gjt.mm.mysql.Driver
        druid: #druid配置
          filters: stat
          initialSize: 2
          maxActive: 300
          maxWait: 60000
          timeBetweenEvictionRunsMillis: 60000
          minEvictableIdleTimeMillis: 300000
          validationQuery: SELECT 1
          testWhileIdle: true
          testOnBorrow: false
          testOnReturn: false
          poolPreparedStatements: false
          maxPoolPreparedStatementPerConnectionSize: 200
    
    seata:
      enabled: true
      application-id: applicationName
      tx-service-group: nacos_seata_tx_group
      config:
        type: nacos
        nacos:
          namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f
          serverAddr: 192.168.235.130:8848
          group: SEATA_GROUP
          userName: "nacos"
          password: "nacos"
      registry:
        type: nacos
        nacos:
          application: seata-server
          server-addr: 192.168.235.130:8848
          namespace: 1ef39358-af4a-438b-8cf0-1ea1fb31de5f
          userName: "nacos"
          password: "nacos"
    

    9. 其他

    业务系统集成Client

    步骤一:添加seata依赖(建议单选)

    • 依赖seata-all

    • 依赖seata-spring-boot-starter,支持yml、properties配置(.conf可删除),内部已依赖seata-all

    • 依赖spring-cloud-alibaba-seata,内部集成了seata,并实现了xid传递

    步骤二:undo_log建表、配置参数

    步骤三:数据源代理(不支持自动和手动配置并存,不支持XA数据源自动代理)

    • 0.9.0版本开始seata支持自动代理数据源
        1.1.0: seata-all取消属性配置,改由注解@EnableAutoDataSourceProxy开启,并可选择jdk proxy或者cglib proxy
        1.0.0: client.support.spring.datasource.autoproxy=true
        0.9.0: support.spring.datasource.autoproxy=true
    
    • 手动配置可参考下方的例子
     @Primary
        @Bean("dataSource")
        public DataSourceProxy dataSource(DataSource druidDataSource) {
            //AT 代理 二选一
            return new DataSourceProxy(druidDataSource);
            //XA 代理
            return new DataSourceProxyXA(druidDataSource)
        }
    

    步骤四:初始化GlobalTransactionScanner

    • 手动
       @Bean
           public GlobalTransactionScanner globalTransactionScanner() {
               String applicationName = this.applicationContext.getEnvironment().getProperty("spring.application.name");
               String txServiceGroup = this.seataProperties.getTxServiceGroup();
               if (StringUtils.isEmpty(txServiceGroup)) {
                   txServiceGroup = applicationName + "-fescar-service-group";
                   this.seataProperties.setTxServiceGroup(txServiceGroup);
               }
       
               return new GlobalTransactionScanner(applicationName, txServiceGroup);
           }
    
    • 自动,引入seata-spring-boot-starter、spring-cloud-starter-alibaba-seata等jar

    步骤五:实现xid跨服务传递

    • 手动 参考源码integration文件夹下的各种rpc实现 module

    • 自动 springCloud用户可以引入spring-cloud-starter-alibaba-seata,内部已经实现xid传递

    相关文章

      网友评论

          本文标题:seata1.2.0搭建

          本文链接:https://www.haomeiwen.com/subject/plqrtktx.html