Article directory
Table of contents
1. The execution process of Seata is as follows
3. Configure the microservice client
foreword
Seata is an open source distributed transaction solution dedicated to providing high-performance and easy-to-use distributed transaction services. Seata will provide users with AT, TCC, SAGA and XA transaction modes to create a one-stop distributed solution for users.
1. The execution process of Seata is as follows
TM [transaction initiator] of service A [order micro-service] applies to TC [seata server] to open a global transaction, and TC will create a global transaction and return a unique XID
Service A starts to call service B [account microservice] remotely, at this time XID will be propagated on the call chain of microservice
The RM of service B registers the branch transaction with TC and brings it into the jurisdiction of the global transaction corresponding to XID
Service B executes branch transactions and performs operations on the database
After the global transaction call chain is processed, TM initiates a global transaction commit or rollback to TC according to whether there is an exception
TC coordinates all branch transactions under its jurisdiction and decides whether to roll back
TM: transaction initiator [on which method the global transaction annotation is added]
TC : transaction manager [seata's server]
RM: each microservice that operates the database
XID: global transaction id
2. Use steps
(1) Download seata1.3.0 --->
Support cluster mode and download the source code of the open source project
Modify conf/file.conf so that the seata cluster information can be shared, we should modify its storage location:
Specify the registration center address of seata and the content of the configuration center
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = "88aa134e-24e9-45ab-a336-6ba2ce63a913"
cluster = "default"
username = "nacos"
password = "nacos"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
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 = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = "88aa134e-24e9-45ab-a336-6ba2ce63a913"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
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"
}
}
Then
Then
3. Configure the microservice client
Add seata dependency in each microservice
<!--seata must match the version of the seata service-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</ dependency>
Modify the configuration file
Finally, add a global transaction as tm in the required business layer
@GlobalTransactional //全局事务 tm队长 全局事务
回滚其他微服务连接数据库。
public void saveOrder(Order order) {
log.info("-------->开始创建新订单");
orderDao.saveOrder(order);
log.info("-------订单微服务开始调用账户,做扣减");
accountFeign.increase(order.getUserId(),order.getMoney()); //事务提交
log.info("-------订单微服务开始调用账户,做扣减end");
int c=10/0;
log.info("--------订单微服务开始调用库存,做扣减");
storageFeign.increase(order.getProductId(),order.getCount());
log.info("-------订单微服务开始调用库存,做扣减end");
log.info("-------修改订单状态");
orderDao.updateStatus(order.getId());
log.info("-------修改订单状态结束");
log.info("--------下订单结束了,哈哈哈哈");
}
Summarize
none