成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringCloud整合Seata實現分布式事務通過nacos實現注冊和配置

開發 前端
什么是配置中心?配置中心可以說是一個"大衣柜",內部放置著各種配置文件,你可以通過自己所需進行獲取配置加載到對應的客戶端.比如Seata Client端(TM,RM),Seata Server(TC),會去讀取全局事務開關,事務會話存儲模式等信息.

環境:springboot2.3.11.RELEASE + spring cloud Hoxton.SR8 + spring cloud alibaba 2.2.5.RELEASE + seata1.3.0

前提:安裝并啟動了nacos服務

Seata注冊中心及配置中心說明

配置中心

什么是配置中心?配置中心可以說是一個"大衣柜",內部放置著各種配置文件,你可以通過自己所需進行獲取配置加載到對應的客戶端.比如Seata Client端(TM,RM),Seata Server(TC),會去讀取全局事務開關,事務會話存儲模式等信息.

Seata的配置中心與Spring cloud的配置中心區別是?在廣義上來說,并無區別,只不過Spring cloud的配置中心僅是作用于它們自身的組件,而Seata的配置中心也是一樣是作用于Seata自身.(注:Spring cloud的配置中心與Seata無關)

注冊中心

什么是注冊中心?注冊中心可以說是微服務架構中的”通訊錄“,它記錄了服務和服務地址的映射關系。在分布式架構中,服務會注冊到這里,當服務需要調用其它服務時,就到這里找到服務的地址,進行調用.比如Seata Client端(TM,RM),發現Seata Server(TC)集群的地址,彼此通信.

Seata的注冊中心與Dubbo,Spring cloud的注冊中心區別是?在廣義上來說,并無區別,只不過Dubbo與Spring cloud的注冊中心僅是作用于它們自身的組件,而Seata的注冊中心也是一樣是作用于Seata自身.(注:Dubbo與Spring cloud的注冊中心與Seata無關)

Seata服務配置

1、在%SEATA_HOME%目錄下新建config.txt文件

service.vgroupMapping.dt-group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&serverTimezone=GMT%2B8
store.db.user=root
store.db.password=xxxxxx
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

2、執行腳本

通過如下地址下載腳本執行

圖片

在windows系統下可以通過 git bash 來執行shell腳本

sh nacos-config.sh -h nacos的ip -p nacos端口 -g nacos配置文件的組 -t 你的namespace號(若是public可省略此選項) -u nacos用戶名 -w nacos密碼

3、數據庫初始化腳本

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=utf8;
-- 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;

完成以上步驟后查看nacos配置

接下來進行項目開發及配置接下來進行項目開發及配置

項目結構

圖片圖片

兩個子模塊:

dt-account-service 用戶模塊
dt-storage-service 庫存模塊

父工程依賴

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
      <exclusion>
        <groupId>io.seata</groupId>
        <artifactId>seata-all</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-all</artifactId>
    <version>${seata.version}</version>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring-cloud-alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

account帳號模塊

核心配置文件

spring:
  cloud:
    nacos:
      password: nacos
      username: nacos
      discovery:
        server-addr: 118.24.111.33:8848
        namespace: ""
        group: dt-group
---
seata:
  tx-service-group: dt-group
  registry:
    type: nacos
    nacos:
      application: seata-server #這里要與seata中的registry.conf配置的application一致
      group: dt-group
      namespace: ""
      server-addr: 118.24.111.33:8848
      username: nacos
      password: nacos
  config:
    type: nacos
    nacos:
      namespace: ""
      group: dt-group
      server-addr: 118.24.111.33:8848
      username: nacos
      password: nacos

AccountService服務類

@Service
public class AccountService {


  private static final String ERROR_USER_ID = "1002";
  @Resource
  private AccountMapper accountMapper ;
  @Resource
  private StorageFeignClient storageFeignClient;


  @Transactional(rollbackFor = Exception.class)
  @GlobalTransactional
  public void debit(String userId, BigDecimal num, String commodityCode, int orderCount) {
    System.out.println(RootContext.getXID()) ;
    accountMapper.updateAccount(num, userId) ;
    storageFeignClient.deduct(commodityCode, orderCount);
    try {
      TimeUnit.MILLISECONDS.sleep(new Random().nextInt(100)) ;
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
        
    if (ERROR_USER_ID.equals(userId)) {
      throw new RuntimeException("account branch exception");
    }
  }
    
}

這里模擬了拋出異常當輸入的userId為1002時拋出異常,就為了測試所有服務的事務是否回滾了。注意這里需要添加@GlobalTransactional注解

Feign接口

@FeignClient(name = "storage-service", url = "127.0.0.1:8802")
public interface StorageFeignClient {


  @GetMapping("/storage/deduct")
  void deduct(@RequestParam("commodityCode") String commodityCode, @RequestParam("count") Integer count);


}

storage庫存子模塊

該子模塊的配置與account模塊的配置基本一致

StorageService

@Service
public class StorageService {
  @Resource
  private StorageMapper storageMapper ;


  @Transactional
  public void deduct(String commodityCode, int count) {
    System.out.println(RootContext.getXID()) ;
    storageMapper.updateStorage(count, commodityCode) ;
  }
}

測試

啟動seata,nacos,account,storage服務后查看nacos

圖片圖片


seata服務也已經注冊上來了。

數據庫初始化數據

圖片圖片


圖片圖片

正常請求

圖片圖片

數據變化

圖片圖片


當傳入userId=1002時

圖片圖片


account模塊控制臺

圖片圖片


storage模塊控制臺

圖片圖片


數據庫數據

圖片圖片


數據沒有任何變化說明回滾了

責任編輯:武曉燕 來源: 實戰案例錦集
相關推薦

2022-06-27 08:21:05

Seata分布式事務微服務

2020-12-09 09:14:57

SpringCloudSeata 分布式

2023-01-06 09:19:12

Seata分布式事務

2022-06-21 08:27:22

Seata分布式事務

2022-03-24 07:51:27

seata分布式事務Java

2020-04-28 12:18:08

Seata模式分布式

2021-08-06 08:33:27

Springboot分布式Seata

2022-07-10 20:24:48

Seata分布式事務

2022-01-12 10:02:02

TCC模式 Seata

2021-04-23 08:15:51

Seata XA AT

2025-04-30 10:44:02

2024-10-09 14:14:07

2023-11-06 13:15:32

分布式事務Seata

2024-01-26 08:18:03

2023-08-17 10:23:07

擴展方案

2020-03-31 08:05:23

分布式開發技術

2024-08-19 09:05:00

Seata分布式事務

2025-04-28 00:44:04

2022-07-03 14:03:57

分布式Seata

2022-10-26 17:28:41

分布式事務seata
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 无人区国产成人久久三区 | 天天干狠狠操 | 请别相信他免费喜剧电影在线观看 | 91精品国产一区二区三区动漫 | 91精品国产高清一区二区三区 | 国产免费视频 | 一区日韩| 亚洲免费在线 | 欧美片网站免费 | 中文字幕免费在线 | 精品久久久久国产 | 免费在线成人 | 激情在线视频 | 午夜av成人 | 国产欧美日韩一区二区三区在线观看 | 国产精品国产三级国产播12软件 | 激情av在线| 国产精品成人一区二区三区吃奶 | www.久久99| 亚洲一区二区免费 | 欧美精品综合在线 | 国产精品久久 | 国产成人综合网 | 日韩中文在线视频 | 日韩av免费在线观看 | 午夜精品久久久 | 国产日韩精品视频 | 精品伊人久久 | 日韩在线视频一区 | 亚洲精品观看 | 亚洲精品视频在线播放 | 亚洲精品久久久久久国产精华液 | 日韩av在线不卡 | www.黄色网| 高清国产午夜精品久久久久久 | 欧美激情精品久久久久 | 久久综合色综合 | 成人深夜福利 | 日韩欧美三级 | 亚洲国产精品第一区二区 | 久热久草 |