spring cloud整合seata

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: spring cloud整合seata

1. 下载seata-server

1.从 https://github.com/seata/seata/releases,下载服务器软件包,将其解压缩。

2. 建表

全局事务过程中,会涉及3块内容:

  • 全局事务 global_table
  • 分支事务 branch_table
  • 全局锁 lock_table
-- -------------------------------- 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. 修改seata-server配置
#### 3.1 配置registry.conf

//注册中心配置
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
eureka {
  serviceUrl = "http://192.xx.xx.xx:8761/eureka"
  application = "fsp_tx" //tc注册时服务名
  weight = "1"
}

}

//配置中心配置
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
  name = "file.conf"
}
}

3.2 配置file.conf

## transaction log store, only used in seata-server
store {
  ## store mode: file、db
  mode = "db"

  ## database store property
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "druid"
    ## mysql/oracle/postgresql/h2/oceanbase etc.
    dbType = "mysql"
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://192.168.173.95:3306/seata-server" //tc的数据库,可自定义命名,对应就好
    user = "mysql"
    password = "runlion@123"
    minConn = 5
    maxConn = 30
    globalTable = "global_table"
    branchTable = "branch_table"
    lockTable = "lock_table"
    queryLimit = 100
    maxWait = 5000
  }
}

4. 启动seata-server 注册到eureka

  • linux
nohup sh seata-server.sh -h xx.xx.xx.xx -p 8091 -m db -n 1 &
  • windows
seata-server.bat
  • 这里是以nohup的方式后台启动,参数可选:
-h: 注册到注册中心的ip
-p: Server rpc 监听端口
-m: 全局事务会话信息存储模式,file、db,优先读取启动参数
-n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突
-e: 多环境配置参考 http://seata.io/en-us/docs/ops/multi-configuration-isolation.html
高可用部署可参考:https://seata.io/zh-cn/docs/ops/deploy-ha.html

5. 服务引入seata依赖

<!--seata-->
<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  <version>2.2.0.RELEASE</version>
//如果内嵌最新版本starter,我们不用排除再引入  
  <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>

6. 服务配置文件


# -----------seata--------------
seata:
    enabled: true
    application-id: order-server #服务名
    tx-service-group: default # default是自定义的事务分组名称
    enable-auto-data-source-proxy: true # 启用自动数据源代理
    use-jdk-proxy: false
    #    excludes-for-auto-proxying:
    #    client:
    #        rm:
    #            async-commit-buffer-limit: 1000
    #            report-retry-count: 5
    #            table-meta-check-enable: false
    #            report-success-enable: false
    #            saga-branch-register-enable: false
    #            lock:
    #                retry-interval: 10
    #                retry-times: 30
    #                retry-policy-branch-rollback-on-conflict: true
    #        tm:
    #            commit-retry-count: 5
    #            rollback-retry-count: 5
    #        undo:
    #            data-validation: true
    #            log-serialization: jackson
    #            log-table: undo_log
    #        log:
    #            exceptionRate: 100
    service:
        vgroup-mapping:
            default: seata-server # default是自定义的事务分组名称,fsp_tx是tc注册到注册中心的服务名称
        #        grouplist:
        #            default: 127.0.0.1:8091 #     仅注册中心为file时使用
        enable-degrade: false # 是否启用降级
        disable-global-transaction: false # 是否禁用全局事务
    #    transport:
    #        shutdown:
    #            wait: 3
    #        thread-factory:
    #            boss-thread-prefix: NettyBoss
    #            worker-thread-prefix: NettyServerNIOWorker
    #            server-executor-thread-prefix: NettyServerBizHandler
    #            share-boss-worker: false
    #            client-selector-thread-prefix: NettyClientSelector
    #            client-selector-thread-size: 1
    #            client-worker-thread-prefix: NettyClientWorkerThread
    #            worker-thread-size: default
    #            boss-thread-size: 1
    #        type: TCP
    #        server: NIO
    #        heartbeat: true
    #        serialization: seata
    #        compressor: none
    #        enable-client-batch-send-request: true
    config:
        type: file # 配置中心为file模式
    #        consul:
    #            server-addr: 127.0.0.1:8500
    #        apollo:
    #            apollo-meta: http://192.168.1.204:8801
    #            app-id: seata-server
    #            namespace: application
    #        etcd3:
    #            server-addr: http://localhost:2379
    #        nacos:
    #            namespace:
    #            serverAddr: localhost
    #            group: SEATA_GROUP
    #            userName: ""
    #            password: ""
    #        zk:
    #            server-addr: 127.0.0.1:2181
    #            session-timeout: 6000
    #            connect-timeout: 2000
    #            username: ""
    #            password: ""
    registry:
        type: eureka # 注册中心为eureka
        eureka:
            weight: 1
            service-url: http://127.0.0.1:50024/eureka # 注册中心地址
#        consul:
#            server-addr: 127.0.0.1:8500
#        etcd3:
#            serverAddr: http://localhost:2379
#        nacos:
#            application: seata-server
#            server-addr: localhost
#            namespace:
#            userName: ""
#            password: ""
#        redis:
#            server-addr: localhost:6379
#            db: 0
#            password:
#            timeout: 0
#        sofa:
#            server-addr: 127.0.0.1:9603
#            region: DEFAULT_ZONE
#            datacenter: DefaultDataCenter
#            group: SEATA_GROUP
#            addressWaitTime: 3000
#            application: default
#        zk:
#            server-addr: 127.0.0.1:2181
#            session-timeout: 6000
#            connect-timeout: 2000
#            username: ""
#            password: ""

# -----------seata--------------

7. 使用

@GlobalTransaction 全局事务注解
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1天前
|
监控 安全 Java
Spring cloud原理详解
Spring cloud原理详解
13 0
|
6天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
14 1
|
7天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
32 6
|
12天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
12天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
14天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
28 1
|
14天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
15 2
|
14天前
|
监控 Java Sentinel
Spring Cloud Sentinel:概念与实战应用
【4月更文挑战第28天】在分布式微服务架构中,确保系统的稳定性和可靠性至关重要。Spring Cloud Sentinel 为微服务提供流量控制、熔断降级和系统负载保护,有效预防服务雪崩。本篇博客深入探讨 Spring Cloud Sentinel 的核心概念,并通过实际案例展示其在项目中的应用。
24 0
|
14天前
|
Cloud Native Java Nacos
Spring Cloud Nacos:概念与实战应用
【4月更文挑战第28天】Spring Cloud Nacos 是一个基于 Spring Cloud 构建的服务发现和配置管理工具,适用于微服务架构。Nacos 提供了动态服务发现、服务配置、服务元数据及流量管理等功能,帮助开发者构建云原生应用。
21 0