大家好,至于为什么做分布事务,我想大家在看到这篇文章的时候,已经知道了,
我就不再这里老生常谈。
今天主要是解决一个问题,使用seata 1.0.0+ nacos +spring cloud alibaa2.2.0.RELEASE 来做分布事务
场景:银行转账的场景,A 用户 转给 B 用户,在分布式场景下实现;
1. 准备环境 seata1.0.0 下载地址: https://seata.io/zh-cn/blog/download.html
2. 下载 nacos 下载地址 https://github.com/alibaba/nacos/releases
启动 nacos 在解压下面的nacos
cd d:\tool\nacos\bin
执行 > startup.cmd
或者双击 startup.cmd 启动
注意: 我修改了端口,原端口好为:8848
启动seata ,seata 默认启动是file 文件的格式;如果修改修改成nacos 启动,需要修改两个配置文件 file.conf registry.conf
vim file.conf
service {
#transaction service group mapping
## 注意点 ,该xa-seata-1-seata-service-group 名字事务分组会在项目中使用
vgroup_mapping.xa-seata-1-seata-service-group = "default"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support
enableDegrade = false
#disable seata
disableGlobalTransaction = false
}
设置seata 的连接模式为db
在数据库中导入表
启动 seata 服务 双击
在正式开始前,在每个库中,需要导入一张 undo_log 给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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hongtu</groupId>
<artifactId>xa-seata-org-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xa-seata-org-1</name>
<description>测试seata xa的模式使用并总结</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR7</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
application.yml 注意: alibaba:seata tx-servie-group 的值,和file.conf 的group 是一致的
server:
port: 9090
spring:
application:
name: xa-seata-1
datasource:
url: jdbc:mysql://localhost:3306/order_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
# 使用nacos 做注册服务
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8847
# yml 是应用程序,所以指定 tx-service-gruop 和我们seata 服务的 service-group 配置一致即可
alibaba:
seata:
tx-service-group: xa-seata-1-seata-service-group
seata:
application-id: ${spring.application.name}
service:
# 设置seata 服务器端在什么位置
grouplist: 127.0.0.1:8091
vgroup-mapping: default
在 service 事务方法上加入一个注解@GlobalTransactional 开启全局事务
@GlobalTransactional
@Transactional
@Override
public void updateAccountBalance(String accountNo, Double amount) {
accountInfoDao.updateAccountBalance(accountNo, amount * -1);
String remoteRst = bank2Client.transfer(amount);
//制造异常
if(amount==2){
throw new RuntimeException("转账测试");
}
//远程调用失败
if(remoteRst.equals("error")){
throw new RuntimeException("降级处理");
}
}