springcloud + nacos整合seata1.4.2

部署seata1.4.2

1、下载  Releases · seata/seata · GitHub

springcloud + nacos整合seata1.4.2_第1张图片

 

文件解压后

1) D:\Program Files\seata-1.4.2\script\config-center\config.txt

放入到

D:\Program Files\seata-server-1.4.2 目录下

2) D:\Program Files\seata-1.4.2\script\config-center\nacos目录下文件

放入到

D:\Program Files\seata-server-1.4.2\bin目录下

3)数据库中建库seata,执行语句

CREATE TABLE `global_table` (
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `status` tinyint(4) NOT NULL,
  `application_id` varchar(32) DEFAULT NULL,
  `transaction_service_group` varchar(32) DEFAULT NULL,
  `transaction_name` varchar(128) DEFAULT NULL,
  `timeout` int(11) DEFAULT NULL,
  `begin_time` bigint(20) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`xid`),
  KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
  KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `branch_table` (
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(128) NOT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `resource_group_id` varchar(32) DEFAULT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `branch_type` varchar(8) DEFAULT NULL,
  `status` tinyint(4) DEFAULT NULL,
  `client_id` varchar(64) DEFAULT NULL,
  `application_data` varchar(2000) DEFAULT NULL,
  `gmt_create` datetime(6) DEFAULT NULL,
  `gmt_modified` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `lock_table` (
  `row_key` varchar(128) NOT NULL,
  `xid` varchar(128) DEFAULT NULL,
  `transaction_id` bigint(20) DEFAULT NULL,
  `branch_id` bigint(20) NOT NULL,
  `resource_id` varchar(256) DEFAULT NULL,
  `table_name` varchar(32) DEFAULT NULL,
  `pk` varchar(36) DEFAULT NULL,
  `gmt_create` datetime DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`row_key`),
  KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

4) 修改D:\Program Files\seata-server-1.4.2\conf目录下文件file.conf,registry.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file、db、redis
  mode = "db"
  ## rsa decryption public key
  publicKey = ""
  ## 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.jdbc.Driver"
    ## if using mysql to store the data, recommend add rewriteBatchedStatements=true in jdbc connection param
    url = "jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true"
    user = "mysql"
    password = "mysql"
    minConn = 5
    maxConn = 100
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

改成自己的数据库信息

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 = "5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "file"

  file {
    name = "file.conf"
  }
}

修改成自己的nacos服务信息

5)打个包放入到Linux中,执行语句。默认端口8091

/opt/seata/seata-server-1.4.2/bin/seata-server.sh > /opt/seata/seata-server-1.4.2/logs/seata.log 2>&1 &

项目部署

1、父级pom.xml引入


        org.springframework.boot
        spring-boot-starter-parent
        2.3.5.RELEASE
         
    

                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR9
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.5.RELEASE
                pom
                import
            

2、子级pom.xml引入seata包


            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        

3、代码、配置

seata:
  application-id: seata-server
  tx-service-group: my_test_tx_group  # 事务组 config.txt配置
  service:
    vgroup-mapping:
      my_test_tx_group: default
@RequestMapping
    @GlobalTransactional
    public void test(Integer type) throws Exception{
        System.out.println("welcome to order");
//        try{
            System.out.println(RootContext.getXID());
            chargerService.test(type);
            userService.test(type);
        try{

            Thread.sleep(5000l);

        }catch (Exception e){
            log.error(e.getMessage());
            e.printStackTrace();
        }
        if (1==type){
            int i = 1/0;
        }
        System.out.println("--------------->结束");
    }

4、启动服务后,通过命令测试

curl http://localhost:9001/test?type=1

5、在睡眠期间可通过查询数据库seata知道数据变化

 global_table信息

springcloud + nacos整合seata1.4.2_第2张图片

 branch_table信息

springcloud + nacos整合seata1.4.2_第3张图片

 lock_table信息

springcloud + nacos整合seata1.4.2_第4张图片

 seata日志(一个回滚、一个提交)

springcloud + nacos整合seata1.4.2_第5张图片

 

 

你可能感兴趣的:(java,spring,spring,cloud,后端)