seata-server
Posted 丶落幕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了seata-server相关的知识,希望对你有一定的参考价值。
seata-server
文章使用seata版本1.3,nacos版本1.3
都是直接在Windows上运行的:
1.修改配置文件seata\\conf\\file.conf
这里就不改了,因为直接注册进nacos,从nacos中取配置(示例):
2.修改配置文件seata\\conf\\registry.conf
代码如下(示例):
#修改注册模式
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
#注册地址
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
cluster = "default"
username = "nacos"
password = "nacos"
}
3.将seata-server配置文件注册进nacos
修改registry.conf(示例):
#修改配置模式
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
#修改配置中心地址
nacos {
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
新建文件夹script,放入nacos-config.sh
config.txt放在seata根目录下
也可以直接双击
当在nacos中看到配置代表上传成功
启动nacos
此处nacos配置存储模式为mysql
启动seata
seata需要cmd加命令行参数
seata-server.bat -h 127.0.0.1 -p 8091
4.客户端
3个服务
一个订单
一个库存
一个consumer(通过feign调用订单和库存)
pom和yml配置基本大同小异(复制粘贴就好):
pom
<dependencies>
<dependency>
<groupId>com</groupId>
<artifactId>common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- alibaba-seata -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<!--移除掉该starter中自带的依赖,该依赖版本较低-->
<exclusion>
<artifactId>seata-all</artifactId>
<groupId>io.seata</groupId>
</exclusion>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--单独添加seata 1.3.0的依赖-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
yml
server:
port: 8002
spring:
application:
name: seata-storage
#注册nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
register-enabled: true
config:
server-addr: 127.0.0.1:8848
enabled: true
file-extension: yaml
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/seata_storage?serverTimezone=UTC
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
#seata配置
seata:
enabled: true
application-id: ${spring.application.name}
tx-service-group: ${spring.application.name}-tx-group
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
service:
vgroup-mapping:
seata-order-tx-group: default
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.ccss.entity
核心代码@GlobalTransactional开启全局事务
这里库存调用设置了异常,通过打断点会发现undo_log表记录的前置镜像和后置镜像
表结构
总结
篇幅过长,不上代码了,环境搭建难,用起来就一个注解,总结一些理论
- AT/TCC/Saga事务模式
- 读写隔离
- 2pc,3pc
- base理论
以上是关于seata-server的主要内容,如果未能解决你的问题,请参考以下文章
Seata-Server分布式事务原理加源码 - 微服务之分布式事务原理