基於seata1.0和spring cloud的Greenwich.SR2版本的分佈式事務demo例子的實現全過程

源碼下載

在這裏插入圖片描述
大家可以直接微信掃描上面的二維碼關注我的公衆號,然後回覆seata1.0裏面就會給到源代碼的下載地址同時會附上相應的視頻教程,並定期的與大家分享相關的技術文章。

前期注意事項

JDK版本必須是64位

首先要確保你的JDK的版本是64位的,若是32位的將會導致啓動seata的服務的時候內存溢出或者無法正常啓動,至於如何查看JDK是否爲64位大家自行百度。

mysql數據庫版本必須是5.x

接着我們需要保證我們的mysql數據庫版本是5.x的版本,若是高版本的則需要改動一些數據庫中的配置是無法正常運行官方給到的例子的,因此爲了確保我們可以正常的運行官方的例子,我們必須確保我們的數據庫版本是5.x,至於如何查看自己的mysql數據庫的版本不懂就百度吧。

maven倉庫配置

記得maven倉庫一定要設置成阿里的中央倉庫,如何設置自行百度。

demo實現

下載官方demo例子

首先我們直接到seata的官方的例子倉庫,地址是:https://github.com/seata/seata-samples,大家直接到這個官方的例子倉庫,將這些例子直接clone下來,然後我們使用我們的編譯器打開裏面的springcloud-eureka-feign-mybatis-seata這個demo例子,如下圖所示:
在這裏插入圖片描述

下載seata-server的release版本

接着下載seata-server 0.9的編譯好的版本,地址是:https://github.com/seata/seata/releases,大家按着下圖直接下載即可:
在這裏插入圖片描述
下載完成以後將我們的seata-server解壓到我們的springcloud-eureka-feign-mybatis-seata工程底下,解壓完成以後效果如下所示:
在這裏插入圖片描述

完善demo例子

配置數據庫

使用我們的navicate打開我們的mysql數據庫,依次創建seata、seat-order、seat-account、seat-storage這四個數據庫:
在這裏插入圖片描述
接着分寫執行以下的腳本:

seata數據庫腳本

-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `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`)
);

-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
  `branch_id` bigint not null,
  `xid` varchar(128) not null,
  `transaction_id` bigint ,
  `resource_group_id` varchar(32),
  `resource_id` varchar(256) ,
  `lock_key` varchar(128) ,
  `branch_type` varchar(8) ,
  `status` tinyint,
  `client_id` varchar(64),
  `application_data` varchar(2000),
  `gmt_create` datetime,
  `gmt_modified` datetime,
  primary key (`branch_id`),
  key `idx_xid` (`xid`)
);

-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
  `row_key` varchar(128) not null,
  `xid` varchar(96),
  `transaction_id` long ,
  `branch_id` long,
  `resource_id` varchar(256) ,
  `table_name` varchar(32) ,
  `pk` varchar(36) ,
  `gmt_create` datetime ,
  `gmt_modified` datetime,
  primary key(`row_key`)
);

seat-account數據庫腳本

CREATE TABLE `account` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `user_id` bigint(11) DEFAULT NULL COMMENT '用戶id',
  `total` decimal(10,0) DEFAULT NULL COMMENT '總額度',
  `used` decimal(10,0) DEFAULT NULL COMMENT '已用餘額',
  `residue` decimal(10,0) DEFAULT '0' COMMENT '剩餘可用額度',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `seat-account`.`account` (`id`, `user_id`, `total`, `used`, `residue`) VALUES ('1', '1', '1000', '0', '100');

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;

seat-order數據庫腳本

CREATE TABLE `order` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `user_id` bigint(11) DEFAULT NULL COMMENT '用戶id',
  `product_id` bigint(11) DEFAULT NULL COMMENT '產品id',
  `count` int(11) DEFAULT NULL COMMENT '數量',
  `money` decimal(11,0) DEFAULT NULL COMMENT '金額',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

ALTER TABLE `order` ADD COLUMN `status` int(1) DEFAULT NULL COMMENT '訂單狀態:0:創建中;1:已完結' AFTER `money` ;

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;

seat-storage數據庫腳本

CREATE TABLE `storage` (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `product_id` bigint(11) DEFAULT NULL COMMENT '產品id',
  `total` int(11) DEFAULT NULL COMMENT '總庫存',
  `used` int(11) DEFAULT NULL COMMENT '已用庫存',
  `residue` int(11) DEFAULT NULL COMMENT '剩餘庫存',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `seat-storage`.`storage` (`id`, `product_id`, `total`, `used`, `residue`) VALUES ('1', '1', '100', '0', '100');

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;

啓動seata-server

在啓動我們的seata-server之前我們需要先啓動我們的註冊中心,註冊中心直接啓動即可,無需修改任何的配置,註冊中心啓動完成以後我們需要修改seata目錄底下的file.conf和registry.conf配置文件:
在這裏插入圖片描述

修改file.conf配置文件

首先修改以下部分:

service {
  #vgroup->rgroup
  vgroup_mapping.my_test_tx_group = "default" ##修改這裏將【vgroup_mapping.my_test_tx_group】修改爲【vgroup_mapping.fsp_tx_group】
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

接着修改以下部分

store {
  ## store mode: file、db
  mode = "db"   ##修改這裏,表明事務信息用db存儲

  ## file store 當mode=db時,此部分配置就不生效了,這是mode=file的配置
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store  mode=db時,事務日誌存儲會存儲在這個配置的數據庫裏
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://你的數據庫的地址/seat"  ##修改這裏
    user = "root"  ##修改這裏
    password = "root"  ##修改這裏
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}

修改registry.conf配置文件

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "eureka" ## 修改這裏將註冊中心設置爲eureka
  
  ## 若註冊中心有修改則直接修改此處的配置
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  
}

啓動seata-server

直接進入以下的路徑啓動我們的seata-server:
在這裏插入圖片描述
啓動完成以後我們可以在我們直接打開我們的註冊中心http://127.0.0.1:8761/,看到我們的seata-server已經註冊到我們的註冊中心了,如果這裏大家在啓動的時候沒有事先安裝好64位的JDK以及5.x版本的mysql那麼這邊會出現以下的兩個問題:

問題一:無法啓動我們的seata-server會報堆對象內存空間不足如下:
Could not reserve enough space for 2097152KB object heap

解決此問題的方法就是安裝64位的JDK。

問題二:啓動好seata-server但是報以下的錯誤:
Could not retrieve transation read-only status server

解決此問題的方法就是安裝的數據庫版本需要是5.x的版本。

啓動order-server模塊

首先修改我們的springcloud-eureka-feign-mybatis-seata底下的pom.xml,修改以後如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath/>
	</parent>
	<groupId>io.seata.sample</groupId>
	<artifactId>springcloud-eureka-feign-mybatis-seata</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<name>springcloud-eureka-feign-mybatis-seata</name>
	<description>Demo project for Spring Boot</description>

	<modules>
		<module>order-server</module>
		<module>account-server</module>
		<module>storage-server</module>
	</modules>

	<properties>
		<java.version>1.8</java.version>
		<mysql-connector-java.version>5.1.37</mysql-connector-java.version>
		<mybatis-spring-boot-starter.version>2.0.0</mybatis-spring-boot-starter.version>
		<druid-spring-boot-starter.version>1.1.10</druid-spring-boot-starter.version>
		<lombok.version>1.18.8</lombok.version>
		<seata.version>1.0.0</seata.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--mybatis-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot-starter.version}</version>
		</dependency>
		<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-java.version}</version>
		</dependency>
		<!--druid-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${druid-spring-boot-starter.version}</version>
		</dependency>
		<!--lombok-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>${lombok.version}</version>
		</dependency>
		<!--seata 設置spring-cloud-alibaba-seata版本爲2.1.1-->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-seata</artifactId>
			<version>2.1.1.RELEASE</version>
			<exclusions>
				<exclusion>
					<artifactId>seata-all</artifactId>
					<groupId>io.seata</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 將原先的seata-all替換成seata-spring-boot-starter -->
		<dependency>
			<groupId>io.seata</groupId>
			<artifactId>seata-spring-boot-starter</artifactId>
			<version>1.0.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

到了1.0版本以後我們就不需要file.conf和registry.conf配置文件了,我們可以直接刪除我們order-server工程底下的file.conf和registry.conf配置文件,直接將配置寫到我們的application.yml配置中即可,因此改造以後的application.yml如下:

eureka:
    instance:
        hostname: localhost
        prefer-ip-address: true
    client:
        serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:8761/eureka/
feign:
    hystrix:
        enabled: false
logging:
    level:
        io:
            seata: info
mybatis:
    mapperLocations: classpath:mapper/*.xml
    typeAliasesPackage: io.seata.sample.entity
server:
    port: 8180
spring:
    application:
        name: order-server
#    需要刪除的配置
#    cloud:
#        alibaba:
#            seata:
#                tx-service-group: fsp_tx_group
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        password: 123456
        url: jdbc:mysql://127.0.0.1:3306/seat-order
        username: root

# 需要新增的配置
seata:
    tx-service-group: fsp_tx_group
    registry:
        type: eureka
        eureka:
            service-url: http://localhost:8761/eureka



# 增加ribbon和hystrix的配置將他們的超時時間設置的長一點,可能是我電腦比較垃圾,因此執行一次完整的鏈路時間需要好幾秒,因此在默認不配置以下參數的時候會導致我的鏈路請求超時導致失敗而回滾,因此我增加了以下的配置。
ribbon:
    eureka:
        enabled: true
    ReadTimeout: 120000
    ConnectTimeout: 120000
    MaxAutoRetries: 0
    MaxAutoRetriesNextServer: 0
    OkToRetryOnAllOperations: false
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

# 設置斷路器超時時間
hystrix:
    threadpool:
        default:
            coreSize: 1000
            maxQueueSize: 1000
            queueSizeRejectionThreshold: 500
    command:
        default:
            execution:
                isolation:
                    thread:
                        timeoutInMilliseconds: 490000
                    strategy: SEMAPHORE

刪除DataSourceConfiguration無用配置文件

接着刪除我們的DataSourceConfiguration配置文件,因爲到了1.0版本已經不需要我們再去手動配置了

修改我們的工程的starter

修改完成以後代碼如下:

/**
 * 訂單服務
 * @author wangzhongxiang
 */
@SpringBootApplication(exclude = GlobalTransactionAutoConfiguration.class)
@MapperScan("io.seata.sample.dao")
@EnableDiscoveryClient
@EnableFeignClients
public class OrderServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(OrderServerApplication.class, args);
	}

}

驗證order-server的啓動

啓動完成以後我們可以在seata-server的控制檯看到我們的order-server已經註冊進去了,同時在註冊中心也會看到我們的order-server已經註冊完成了,結果如下所示:
在這裏插入圖片描述

啓動剩餘模塊

同理按着上面的order-server的修改剩下的account-server和storage-server,然後啓動剩下的兩個工程,如果在seata-server你們看到以下的頁面就說明我們的剩餘的模塊已經正常啓動了。
在這裏插入圖片描述

驗證分佈式事務

驗證正常事務流程

直接打開我們的瀏覽器然後輸入以下的地址:http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100,這時候我們可以看我們的啓動的服務的控制檯,大家會發現我們已經正常完成了整個事務的閉環。
在這裏插入圖片描述

驗證異常事務回滾流程

直接修改我們的order-account的service中的代碼如下所示:

/**
     * 扣減賬戶餘額
     * @param userId 用戶id
     * @param money 金額
     */
    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("------->扣減賬戶開始account中");
        accountDao.decrease(userId,money);
        LOGGER.info("------->扣減賬戶結束account中");
        //修改訂單狀態,此調用會導致調用成環
        LOGGER.info("修改訂單狀態開始");
        String mes = orderApi.update(userId, money.multiply(new BigDecimal("0.09")),0);
        LOGGER.info("修改訂單狀態結束:{}",mes);
        // 模擬異常錯誤
        throw new RuntimeException("賬戶操作異常!");
    }

然後重啓我們的工程,然後再次訪問我們剛剛的地址,這時候大家就會看到控制檯報錯,且我們在數據庫會看到我們的數據都被回滾了。

發佈了123 篇原創文章 · 獲贊 70 · 訪問量 112萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章