spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。

版本信息

spring-cloud-alibaba版本 2.2.0.RELEASE 
nacos 1.2版本  
seata 1.2版本
spring-cloud版本 Hoxton.SR3
spring-boot版本 2.2.6.RELEASE

目录

spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第1张图片

 

pom.xml


    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-config
    
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        com.alibaba
        druid
        1.1.22
    
    
        tk.mybatis
        mapper-spring-boot-starter
        2.0.3-beta1
    
    
        tk.mybatis
        mapper
        4.0.0
    
    
        mysql
        mysql-connector-java
        8.0.15
    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-seata
        
            
                io.seata
                seata-spring-boot-starter
            
        
    
    
        io.seata
        seata-spring-boot-starter
        1.2.0
        
            
                com.alibaba
                druid
            
            
                io.seata
                seata-all
            
        
    
    
        io.seata
        seata-all
        1.2.0
    

 

nacos搭建 

   nacos下载地址: https://github.com/alibaba/nacos

   nacos 使用mysql存储    初始化nacos想要的表 

      修改conf/application.properties连接信息

                    spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第2张图片

 

使用mysql8会有问题

      只需在nacos目录下新建plugins/mysql目录导入mysql8连接jar包即可

 

seata搭建:

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

    修改registry.conf

          spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第3张图片

  修改file.conf 中store为db 修改db连接信息

   导入以下表

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,
  `lock_key` varchar(128) 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 DEFAULT NULL,
  `gmt_modified` datetime DEFAULT NULL,
  PRIMARY KEY (`branch_id`),
  KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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 `lock_table` (
  `row_key` varchar(128) NOT NULL,
  `xid` varchar(96) DEFAULT NULL,
  `transaction_id` mediumtext,
  `branch_id` mediumtext,
  `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`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 


 

启动类

spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第4张图片

bootstrap.yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        prefix: account-server
        file-extension: yml

 

nacos上面的配置文件

server:
  port: 9001
spring:
  application:
    name: account-server # 应用名称 自定义
  # druid 数据源配置
  datasource:
    url: jdbc:mysql://localhost:3333/seata_account?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useUnicode=true&serverTimezone=UTC&nullCatalogMeansCurrent=true
    username: root
    password: rootroot
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    filters: stat,wall,slf4j
    maxActive: 20
    initialSize: 5
    maxWait: 60000
    minIdle: 5
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: my_test_tx_group
  service:
    vgroupMapping:
      my_test_tx_group: default
    grouplist:
      default: 127.0.0.1:8091
  config:
    type: nacos
    nacos:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP
      userName: "nacos"
      password: "nacos"
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      userName: "nacos"
      password: "nacos"

 

 

效果

spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第5张图片

spring cloud alibaba 2.2.0 + seata 1.2 + nacos 1.2.1搭建分布式事物_第6张图片

 

 

你可能感兴趣的:(seata)