SpringCloud集成分布式事务Seata2.0.0

SpringCloud集成分布式事务Seata2.0.0

目录

一、部署Seata

1、使用windows部署

1.1、下载Seata启动包

1.2、创建数据库表

1.3、修改seata的application.yml

1.4、启动seata

二、业务系统集成

1、本项目基础依赖版本

2、添加依赖

3、添加配置

4、添加注解@GlobalTransactional

5、创建UNDO_LOG表


一、部署Seata

1、使用windows部署

1.1、下载Seata启动包

官方下载地址:https://github.***/apache/incubator-seata/releases/tag/v2.0.0

1.2、创建数据库表

首先创建名为seata的数据库,将seata启动包解压后,进入seata/script/server/db目录下,并执行对应数据库类型的sql文件,完成数据库表创建。

1.3、修改seata的application.yml

从application.example.yml将需要的配置复制进来,并进行修改。使用nacos、mysql。

最终版:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${log.home:${user.home}/logs/seata}
  #extend:
  #  logstash-appender:
  #    destination: 127.0.0.1:4560
  #  kafka-appender:
  #    bootstrap-servers: 127.0.0.1:9092
  #    topic: logback_to_logstash

console:
  user:
    username: seata
    password: seata
seata:
  config:
    # support: nacos, consul, apollo, zk, etcd3
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace:
      group: SEATA_GROUP
      username:
      password:
      context-path:
      data-id: seataServer.properties
  registry:
    # support: nacos, eureka, redis, zk, consul, etcd3, sofa
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
      cluster: default
      username:
      password:
      context-path:
  server:
    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
    max-***mit-retry-timeout: -1
    max-rollback-retry-timeout: -1
    rollback-retry-timeout-unlock-enable: false
    enable-check-auth: true
    enable-parallel-request-handle: true
    enable-parallel-handle-branch: false
    retry-dead-threshold: 130000
    xaer-nota-retry-timeout: 60000
    enableParallelRequestHandle: true
    recovery:
      ***mitting-retry-period: 1000
      async-***mitting-retry-period: 1000
      rollbacking-retry-period: 1000
      timeout-retry-period: 1000
    undo:
      log-save-days: 7
      log-delete-period: 86400000
    session:
      branch-async-queue-size: 5000 #branch async remove queue size
      enable-branch-async-remove: false #enable to asynchronous remove branchSession
  store:
    # support: file 、 db 、 redis 、 raft
    mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: ***.mysql.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/seata?rewriteBatchedStatements=true
      user: mysql
      password: mysql
      min-conn: 10
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 1000
      max-wait: 5000
  metrics:
    enabled: false
    registry-type: ***pact
    exporter-list: prometheus
    exporter-prometheus-port: 9898
  transport:
    rpc-tc-request-timeout: 15000
    enable-tc-server-batch-send-response: false
    shutdown:
      wait: 3
    thread-factory:
      boss-thread-prefix: ***tyBoss
      worker-thread-prefix: ***tyServerNIOWorker
      boss-thread-size: 1
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/metadata/v1/**

1.4、启动seata

进入seata/bin目录下,执行启动命令。

二、业务系统集成

1、本项目基础依赖版本

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.13.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Hoxton.SR3</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency>
    <groupId>***.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.1.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

注意:使用其他版本依赖,相应配置会有所不同。请按照官方发布的版本关系对照表进行添加依赖,否则会出现不兼容的bug:SpringCloud Alibaba版本依赖关系说明

2、添加依赖

<!--seata-->
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-all</artifactId>
    <version>2.0.0</version>
</dependency>

3、添加配置

seata:
  registry:
    type: nacos
    nacos:
      server-addr: localhost:8848
      namespace: ""
      group: SEATA_GROUP
      application: seata-server
  # 事务组名称
  tx-service-group: xxx(自定义事务组名称)
  service:
    # 事务组与tc集群的映射关系
    vgroup-mapping:
      # default - tc服务的集群名称
      xxx(自定义事务组名称,与上面一致): "default"

4、添加注解@GlobalTransactional

在需要开启全局事务的方法上添加注解@GlobalTransactional

5、创建UNDO_LOG表

在业务数据库中创建UNDO_LOG表

CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) 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=1 DEFAULT CHARSET=utf8mb4 COLLATE = utf8mb4_general_ci;

转载请说明出处内容投诉
CSS教程_站长资源网 » SpringCloud集成分布式事务Seata2.0.0

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买