![79b7ea2ac0f6cee1d8a49168405a94ec.png](https://img-blog.csdnimg.cn/img_convert/79b7ea2ac0f6cee1d8a49168405a94ec.png)
1.什么是微服务分布式事务?
对于传统的单体应用,我们一般通过3个Module
,同一个数据源更新数据,很自然整个过程数据的一致性是由本地事务来保证的。
![cd787bce333f4bac328d7ec09a989080.png](https://img-blog.csdnimg.cn/img_convert/cd787bce333f4bac328d7ec09a989080.png)
随着业务需求和架构的变化,单体应用被拆分为微服务:原来的 3 个 Module 被拆分为 3 个独立的服务,分别使用独立的数据源。业务过程将由 3 个服务的调用来完成。
![6c4b613ce9f10aca706a75cd31d22097.png](https://img-blog.csdnimg.cn/img_convert/6c4b613ce9f10aca706a75cd31d22097.png)
此时,每一个服务内部的数据一致性仍由本地事务来保证。而整个业务层面的全局数据一致性要如何保障呢?这就是微服务架构下面临的,典型的分布式事务需求:我们需要一个分布式事务的解决方案保障业务全局的数据一致性。
![2191ff76f9f1c41fc4624156ad348d6e.png](https://img-blog.csdnimg.cn/img_convert/2191ff76f9f1c41fc4624156ad348d6e.png)
以上就是Seata要解决的问题了~更多前往:Seata官网
Spring Cloud使用Seata实现分布式事务,并且使用Nacos作为配置中心。
Nacos
是阿里巴巴中间件团队开源的的一个用于发现、配置和管理微服务的一个项目,具体请移步:
Nacos
官网。
Step 1、安装Nacos
从Github
上同步代码
git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos clean install -U
ls -al distribution/target/
// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
也可以通过下载编译后压缩包的方式
unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
cd nacos/bin
Step 2.启动Nacos
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
Windows
启动命令:
cmd startup.cmd
或者双击startup.cmd
运行文件。
访问:http://localhost:8848/nacos/index.html#/configurationManagement?dataId=&group=&appName=&namespace= 可以查看服务列表了~
更多Nacos
信息请前往:Nacos官网
nacos主页:nacos,账号:nacos,密码:nacos
Step 3 向Nacos注册Seata服务
1.在Seata Releases 中下载0.6.1版本 2.修改 conf/registry.conf'将
registry下的
type
修改为nacos
,以及把config
下的type
也改为nacos
即可。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
...
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
...
}
3.把Seata
配置加到Nacos
中去
cd conf
sh nacos-config.sh localhost
脚本执行最后输出 "init nacos config finished, please start Seata-server." 说明推送配置成功。若想进一步确认可登陆Nacos 控制台 配置列表 筛选 Group=SEATA_GROUP 的配置项。
然后进入Nacos主页就会发现多了47条配置
![310a315914bcbaf0f2f091b994efdfdf.png](https://img-blog.csdnimg.cn/img_convert/310a315914bcbaf0f2f091b994efdfdf.png)
- 运行 Seata-server
Linux/Unix/Mac
sh seata-server.sh $LISTEN_PORT $STORE_MODE $IP(此参数可选)
Windows
cmd seata-server.bat $LISTEN_PORT $PATH_FOR_PERSISTENT_DATA $IP(此参数可选)
运行成功后可在 Nacos 控制台看到 服务名 =serverAddr 服务注册列表:
![1612cc2d3616544941a8ce6743d8f53b.png](https://img-blog.csdnimg.cn/img_convert/1612cc2d3616544941a8ce6743d8f53b.png)
这就成功注册到nacos
上啦~
欢迎关注我的个人公众号~
![e9ecb1a83e298b89e438b52554525ecb.png](https://img-blog.csdnimg.cn/img_convert/e9ecb1a83e298b89e438b52554525ecb.png)