学习基于记录,而不止于记录。
希望自己能坚持下去~
0.写在前面
spring boot版本:2.2.7.RELEASE
Seata版本:1.2.0
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.18 |
+-----------+
其他依赖版本细节在pom.xml可以看到。
之前有了解过分布式事务的处理方法,比如:使用消息队列中间件实现事务的最终一致性
当然只是简单了解一下基本机制,并没有深入的学习和使用,但是看到这种方式实现分布式事务的局限性,那就是虽然在一些场景:比如注册用户和发送验证码,银行转账等,这种消息队列的方式可以很好的实现,但是在实际应用中发生分布式事务的场景,有时候需要信息的即时性有一定保证,比如:子系统需要即时的向平台系统获取登录信息,总不能像验证码和转账一样接受那么大的延时吧。还有一点就是消息中间件号称可以实现无侵入的实现分布式事务,但是事实上,就我们公司的一些项目来说,改造起来代价还说蛮大的。
综上所述,我考虑学习和使用另一种分布式事务解决方案:Eureka+Seata。
1.基本概念
建议直接看Seata官网,文档很全面也很友好,毕竟是国内阿里整出来的。
Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。在 Seata 开源之前,Seata 对应的内部版本在阿里经济体内部一直扮演着分布式一致性中间件的角色,帮助经济体平稳的度过历年的双11,对各BU业务进行了有力的支撑。2019.1 ,Seata 正式宣布对外开源。
开源万岁!!!
2.Eureka注册中心搭建
说白了就是一个spring boot项目,加入Eureka依赖,提供给注册中心给其他服务,进而方便实现负载均衡的一些集群环境下常备功能。这里不做重点,以后或会更详细研究。
1)创建项目,加上依赖
2)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>com.macro.cloud</groupId>
<artifactId>springcloud-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.macro.cloud</groupId>
<artifactId>seata-storage-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>seata-storage-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<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>
</project>
3)application.yml
server:
port: 5060
spring:
application:
name: eureka-server #注册中心服务名称
eureka:
instance:
hostname: localhost #Eureka实例的主机名
client:
#是否启用湖区注册服务信息,因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;
fetch-registry: false
#表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false;
register-with-eureka: true
service-url:
defaultZone: http://${eureka.instance.hostname}:5060/eureka
server:
#设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
#会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
#实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
enable-self-preservation: true
#扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
注意:
eureka.client.register-with-eureka通常设置为false
,但是为了演示方便,这里设置为true。这样就可以在注册中心看到Eureka注册服务自己被注册到注册中心。
4)启动类加注解
package com.grj.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
5)启动注册中心,在浏览器打开http://localhost:5060/(端口设置不同,地址也不同)
3.Seata服务
1)下载seata-server
下载中心
我使用的是1.2.0版本(可能下载速度会有些慢)
下载完成后,解压,修改seata\conf
目录下两个文件
2)registry.conf文件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
application = "seata-server"
serverAddr = "localhost"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://127.0.0.1:5060/eureka"
application = "seata-server"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
3)file.conf文件
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## file store property
file {
## store location dir
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
maxBranchSessionSize = 16384
# globe session size , if exceeded throws exceptions
maxGlobalSessionSize = 512
# file buffer size , if exceeded allocate new buffer
fileWriteBufferCacheSize = 16384
# when recover batch read size
sessionReloadReadSize = 100
# async, sync
flushDiskMode = async
}
## 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.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/seata?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true"
user = "root"
password = "123456"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
4)启动Seata
点击Seata/bin/seata-server.bat
启动Seata,你可以看到两个服务在注册中心,其中一个就是刚启动的Seata
4)测试Seata分布式事务
注意这一部分Seata就提供了官方实例,而且非常清楚,而且还提供了sql建库脚本。
简单来讲,就是订单、账户和库存三个服务,下订单时,订单系统创建订单,并且调用库存服务减少库存,然后调用账户系统,削减金额,前几部完成后再修改订单状态为已完成,整个事务完成。
在这里我也贴上我的代码,只是留给自己以后查看,有路过的还是建议直接看官方实例。
4.总结
Seata对于分布式事务的解决个人认为目前体验良好,而且真正意义上的上手简单,无侵入提供方案。目前公司正准备采用这种方案,部署到服务器采用k8s进行服务编排,以后或有详细文档继续补充。