基于seata1.0和spring cloud的Greenwich.SR2版本的分布式事务demo例子的实现全过程

本文主要是介绍基于seata1.0和spring cloud的Greenwich.SR2版本的分布式事务demo例子的实现全过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基于seata1.0和spring cloud的Greenwich.SR2版本的分布式事务demo例子的实现全过程

  • 源码下载
  • 前期注意事项
    • JDK版本必须是64位
    • mysql数据库版本必须是5.x
    • maven仓库配置
  • demo实现
    • 下载官方demo例子
    • 下载seata-server的release版本
    • 完善demo例子
      • 配置数据库
        • seata数据库脚本
        • seat-account数据库脚本
        • seat-order数据库脚本
        • seat-storage数据库脚本
      • 启动seata-server
        • 修改file.conf配置文件
        • 修改registry.conf配置文件
        • 启动seata-server
          • 问题一:无法启动我们的seata-server会报堆对象内存空间不足如下:
          • 问题二:启动好seata-server但是报以下的错误:
      • 启动order-server模块
        • 删除DataSourceConfiguration无用配置文件
        • 修改我们的工程的starter
        • 验证order-server的启动
      • 启动剩余模块
      • 验证分布式事务
        • 验证正常事务流程
        • 验证异常事务回滚流程

源码下载

在这里插入图片描述
大家可以直接微信扫描上面的二维码关注我的公众号,然后回复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->rgroupvgroup_mapping.my_test_tx_group = "default" ##修改这里将【vgroup_mapping.my_test_tx_group】修改为【vgroup_mapping.fsp_tx_group】#only support single nodedefault.grouplist = "127.0.0.1:8091"#degrade current not supportenableDegrade = false#disabledisable = false#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanentmax.commit.retry.timeout = "-1"max.rollback.retry.timeout = "-1"
}

接着修改以下部分

store {## store mode: file、dbmode = "db"   ##修改这里,表明事务信息用db存储## file store 当mode=db时,此部分配置就不生效了,这是mode=file的配置file {dir = "sessionStore"# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptionsmax-branch-session-size = 16384# globe session size , if exceeded throws exceptionsmax-global-session-size = 512# file buffer size , if exceeded allocate new bufferfile-write-buffer-cache-size = 16384# when recover batch read sizesession.reload.read_size = 100# async, syncflush-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 = 1max-conn = 3global.table = "global_table"branch.table = "branch_table"lock-table = "lock_table"query-limit = 100}
}
修改registry.conf配置文件
registry {# file 、nacos 、eureka、redis、zk、consul、etcd3、sofatype = "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: localhostprefer-ip-address: trueclient:serviceUrl:defaultZone: http://${eureka.instance.hostname}:8761/eureka/
feign:hystrix:enabled: false
logging:level:io:seata: info
mybatis:mapperLocations: classpath:mapper/*.xmltypeAliasesPackage: io.seata.sample.entity
server:port: 8180
spring:application:name: order-server
#    需要删除的配置
#    cloud:
#        alibaba:
#            seata:
#                tx-service-group: fsp_tx_groupdatasource:driver-class-name: com.mysql.jdbc.Driverpassword: 123456url: jdbc:mysql://127.0.0.1:3306/seat-orderusername: root# 需要新增的配置
seata:tx-service-group: fsp_tx_groupregistry:type: eurekaeureka:service-url: http://localhost:8761/eureka# 增加ribbon和hystrix的配置将他们的超时时间设置的长一点,可能是我电脑比较垃圾,因此执行一次完整的链路时间需要好几秒,因此在默认不配置以下参数的时候会导致我的链路请求超时导致失败而回滚,因此我增加了以下的配置。
ribbon:eureka:enabled: trueReadTimeout: 120000ConnectTimeout: 120000MaxAutoRetries: 0MaxAutoRetriesNextServer: 0OkToRetryOnAllOperations: falseNFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule# 设置断路器超时时间
hystrix:threadpool:default:coreSize: 1000maxQueueSize: 1000queueSizeRejectionThreshold: 500command:default:execution:isolation:thread:timeoutInMilliseconds: 490000strategy: 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 金额*/@Overridepublic 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("账户操作异常!");}

然后重启我们的工程,然后再次访问我们刚刚的地址,这时候大家就会看到控制台报错,且我们在数据库会看到我们的数据都被回滚了。

这篇关于基于seata1.0和spring cloud的Greenwich.SR2版本的分布式事务demo例子的实现全过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/374967

相关文章

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja