# microservice-alibaba **Repository Path**: zhanglei-code/microservice-alibaba ## Basic Information - **Project Name**: microservice-alibaba - **Description**: SpringCloudAlibaba、Nacos、Sentinel、Seata整合demo。 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2021-08-10 - **Last Updated**: 2024-09-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # microservice-alibaba #### 介绍 SpringCloudAlibaba、Nacos、Sentinel、Seata整合demo。 SpringCloud文档:https://spring.io/projects/spring-cloud SpringCloudAlibaba文档:https://github.com/alibaba/spring-cloud-alibaba/wiki Nacos文档:https://nacos.io/zh-cn/docs/what-is-nacos.html Seata文档:https://seata.io/zh-cn/docs/overview/what-is-seata.html Sentinel文档:https://sentinelguard.io/zh-cn/docs/introduction.html #### 软件架构 JDK 1.8 Spring Boot 2.1.10.RELEASE Spring Cloud Greenwich.SR6 Spring Cloud Alibaba 2.1.2.RELEASE Nacos 1.2.1 Sentinel 1.7.1 Seata 1.2.0 #### 安装教程 1. Nacos下载安装 1)下载nacos-server-1.2.1.zip https://github.com/alibaba/nacos/releases/tag/1.2.1 2)启动运行 解压后进入到nacos的bin目录,cmd运行: ~~~shell # standalone代表着单机模式运行,非集群模式 startup.cmd -m standalone ~~~ 3)访问测试 浏览器访问:http://192.168.1.118:8848/nacos。 用户名密码都是nacos。 ![image-20210811103556707](images/image-20210811103556707.png) 2. Sentinel仪表盘下载安装 1)下载sentinel-dashboard-1.7.1.jar https://github.com/alibaba/Sentinel/releases/tag/1.7.1 2)启动运行 ~~~shell java -Dserver.port=9000 -Xms256m -Xmx256m -jar sentinel-dashboard-1.7.1.jar ~~~ 3)访问测试 浏览器访问:http://192.168.1.118:9000。 用户名密码都是sentinel。 ![image-20210811222854895](images/image-20210811222854895.png) 3. 分布式事务seata下载安装 > Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。 > 官网地址:https://seata.io/zh-cn/index.html。 **资源下载** 下载地址:https://github.com/seata/seata/releases/tag/v1.2.0 下载文件:`seata-server-1.2.0.zip`、`Source code(zip)` `Source code(zip)`中script文件夹资源目录介绍(下面简称资源文件): >**client** >存放client端sql脚本 (包含 undo_log表) ,参数配置。 >**config-center** >各个配置中心参数导入脚本,config.txt(包含server和client,原名nacos-config.txt)为通用参数文件。 >**server** >server端数据库脚本(包含lock_table、branch_table、global_table)及各个容器配置。 ![image-20210811104849331](images/image-20210811104849331.png) **快速开始** Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。 我们需要搭建Server端(下载的seata-server-1.2.0.zip软件包)与Client端(我们的微服务),另外还有一个配置端(Nacos配置中心),需要导入相关配置供Server端和Client端使用。 **配置端** **1)启动Nacos** **2)修改配置文件,推送配置** 这里Seata的Server端存储模式(store.mode)选择db高可用模式,后面会介绍几种模式的区别。 修改资源文件script/config-center/config.txt,附上修改部分。 ~~~ini …… service.vgroupMapping.my_test_tx_group=default service.default.grouplist=127.0.0.1:8091 …… store.mode=db …… store.db.datasource=druid store.db.dbType=mysql store.db.driverClassName=com.mysql.jdbc.Driver store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true store.db.user=root store.db.password=root store.db.minConn=5 store.db.maxConn=30 store.db.globalTable=global_table store.db.branchTable=branch_table store.db.queryLimit=100 store.db.lockTable=lock_table store.db.maxWait=5000 …… ~~~ 进入到资源文件script/config-center/nacos,使用终端执行`python nacos-config.py 192.168.1.118:8848`,将配置推送到nacos配置中心(Windows下需要安装Python环境)。 ![image-20210811111854088](images/image-20210811111854088.png) **Server端** **1)创建seata-server高可用的db** > Server端存储模式(store.mode)现有file、db、redis三种(后续将引入raft,mongodb)。 > file模式为单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高; > db模式为高可用模式,全局事务会话信息通过db共享,相应性能差些; > redis模式Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置合适当前场景的redis持久化配置。 我们选择db模式,创建名为seata的数据库,脚本见资源文件script/server/db/mysql.sql。 **2)指定注册中心与配置中心** 修改seata-server-1.2.0/conf/registry.conf。 ~~~ini registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa # 指定注册中心 type = "nacos" nacos { application = "seata-server" serverAddr = "127.0.0.1:8848" namespace = "" cluster = "default" username = "nacos" password = "nacos" } } config { # file、nacos 、apollo、zk、consul、etcd3 # 指定配置中心 type = "nacos" nacos { serverAddr = "127.0.0.1:8848" namespace = "" group = "SEATA_GROUP" username = "nacos" password = "nacos" } } ~~~ **3)启动seata-server** `seata-server.bat -h 192.168.1.118 -p 8091 -m db` > 参数说明: > -h: 注册到注册中心的ip > -p: Server rpc 监听端口,默认8091 > -m: 全局事务会话信息存储模式,file、db、redis,优先读取启动参数 (Seata-Server 1.3及以上版本支持redis) > -n: Server node,多个Server时,需区分各自节点,用于生成不同区间的transactionId,以免冲突 > -e: 多环境配置 ![image-20210811113218011](images/image-20210811113218011.png) **Client端** **1)创建AT模式所需的undo_log表** 参与全局事务的数据库中都需要创建undo_log表,脚本见资源文件script/client/at/db/mysql.sql。 **2)添加seata依赖** ~~~xml com.alibaba.cloud spring-cloud-starter-alibaba-seata io.seata seata-spring-boot-starter io.seata seata-spring-boot-starter 1.2.0 ~~~ **3)添加配置文件** tx-service-group与资源文件script/config-center/config.txt中service.vgroupMapping保持一致。 ~~~yaml seata: enabled: true application-id: applicationName tx-service-group: my_test_tx_group enable-auto-data-source-proxy: true config: type: nacos nacos: namespace: serverAddr: 192.168.1.118:8848 group: SEATA_GROUP userName: "nacos" password: "nacos" registry: type: nacos nacos: application: seata-server server-addr: 192.168.1.118:8848 namespace: userName: "nacos" password: "nacos" ~~~ **4)加入全局事务注解** 在全局事务发起的入口添加@GlobalTransactional,其它服务不需要添加。 ~~~java /** * 模拟用户下单 */ @GlobalTransactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class) @Override public void buy() { // 1.订单服务保存数据(模拟保存订单) // 2.库存服务修改数据(模拟扣减库存) int i = 1 / 0; } ~~~ #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)