首页 > 其他分享 >SpringCloud集成Seata并使用Nacos做注册中心与配置中心

SpringCloud集成Seata并使用Nacos做注册中心与配置中心

时间:2023-02-21 11:35:08浏览次数:64  
标签:group seata SpringCloud 配置 nacos Nacos spring Seata

本文为博主原创,未经允许不得转载:

目录:

  1. 下载并启动Seata Server,并指定nacos作为配置中心和注册中心

  2. 同步seata server的配置到nacos

  3. 启动Seata Server

  4. Seata整合到Spring Cloud微服务

  5. 接入问题总结:

  6.demo项目gitee 链接: https://gitee.com/xiangbaxiang/seata-nacos-demo

1. 下载并启动Seata Server,并指定nacos作为配置中心和注册中心

  1.1 下载地址:http://seata.io/zh-cn/blog/download.html

     

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_Server

  1.2 通过 seata 的 conf 目录下的 file.conf 指定server 端存储模式:

    支持 file ,db , redis 三种方式:

      file:(默认)单机模式,全局事务会话信息内存中读写并持久化本地文件root.data,性能较高(默认)

      db:(5.7+)高可用模式,全局事务会话信息通过db共享,相应性能差些

      redis:Seata-Server 1.3及以上版本支持,性能较高,存在事务信息丢失风险,请提前配置适合当前场景的redis持久化配置

  1.3 在 register.conf 指定注册中心:

                             

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_Server_02

   1.4 在 register.conf 指定配置中心:

       

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_spring cloud_03

   注意:客户端配置registry.conf使用nacos时也要注意group要和seata server中的 group 一致,默认group是"DEFAULT_GROUP"

2. 同步seata server的配置到nacos

  先启动本地的nacos  

  获取/seata/script/config-center/config.txt,修改配置信息

    

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_微服务_04

   seata 从1.4 之后,seata 的安装包中去除了 同步的配置文件,可以从这个目录中获取: ​https://github.com/seata/seata/tree/1.3.0/script ​

   配置事务分组, 要与客户端配置的事务分组一致

    (客户端properties配置:spring.cloud.alibaba.seata.tx‐service‐group=my_test_tx_group)

                         

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_spring cloud_05

  通过脚本配置参数同步到Nacos

sh ${SEATAPATH}/script/config‐center/nacos/nacos‐config.sh ‐h localhost ‐p 8848 ‐g SEATA_GROUP ‐t 5a3c7d6c‐f497‐4d68‐a71a‐2e5e3340b3ca

    参数说明:

      -h: host,默认值 localhost

      -p: port,默认值 8848

      -g: 配置分组,默认值为 'SEATA_GROUP'

      -t: 租户信息,对应 Nacos 的命名空间ID字段, 默认值为空。

sh nacos‐config.sh ‐h localhost ‐p 8848 ‐g SEATA_GROUP

3. 启动Seata Server

    启动Seata Server命令

bin/seata‐server.sh

    启动成功,默认端口8091

4. Seata整合到Spring Cloud微服务

  业务场景:

    用户下单,整个业务逻辑由三个微服务构成:

    仓储服务:对给定的商品扣除库存数量。

    订单服务:根据采购需求创建订单。

    帐户服务:从用户帐户中扣除余额。

   4.1导入依赖:

<!‐‐ seata‐‐> 
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐starter‐alibaba‐seata</artifactId>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata‐all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata‐all</artifactId>
<version>1.4.0</version>
</dependency>
<!‐‐nacos 注册中心‐‐>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐starter‐alibaba‐nacos‐discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring‐cloud‐starter‐openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid‐spring‐boot‐starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql‐connector‐java</artifactId>
<scope>runtime</scope>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis‐spring‐boot‐starter</artifactId>
<version>2.1.1</version>
</dependency>

<dependency>
  <groupId>io.seata</groupId>
  <artifactId>seata‐spring‐boot‐starter</artifactId>
  <version>1.4.0</version>
</dependency>

  4.2 微服务对应数据库中添加undo_log

CREATE TABLE `undo_log` ( 
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
0 PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

  4.3 在yml中配置

seata:
enabled: true
application-id: ${spring.application.name}
# seata 服务分组,要与服务端nacos‐config.txt中service.vgroup_mapping的后缀对应
tx-service-group: my_test_tx_group
config:
# 指定nacos作为配置中心
type: nacos
nacos:
namespace:
serverAddr: 127.0.0.1:8848
group: SEATA_GROUP

registry:
# 指定nacos作为注册中心
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
namespace:

  在事务发起者中添加@GlobalTransactional注解

@GlobalTransactional

5. 接入问题总结:

SpringCloud集成Seata并使用Nacos做注册中心与配置中心_spring_06

  一般大多数情况下都是因为配置不匹配导致的:

  1.检查现在使用的seata服务和项目maven中seata的版本是否一致

  2.检查tx-service-group,nacos.cluster,nacos.group参数是否和Seata Server中的配置一致

  跟踪源码:seata/discover包下实现了RegistryService#lookup,用来获取服务列表

 6.demo项目gitee 链接: https://gitee.com/xiangbaxiang/seata-nacos-demo

  在查看并使用该项目时,请先查看 ReadMe.md中的相关介绍。



标签:group,seata,SpringCloud,配置,nacos,Nacos,spring,Seata
From: https://blog.51cto.com/u_15535797/6076386

相关文章

  • Nacos服务发现原理分析
    微服务将自己的实例注册到nacos注册中心,nacos服务端存储了注册列表,然后通过ribbon调用服务,具体是如何调用?如果nacos服务挂了,还能正常调用服务吗?调用的服务列表发生变化,调......
  • 高可用Nacos服务实战
     环境:centOS7nacos-server-2.2.0.zipkeepalived-2.2.7.tar.gznginx-1.22.1.tar.gz Nginx+keepalived搭建高可用集群Nacos   实现思路:1、安装nacos集群......
  • nacos配置中心
    1.介绍随着业务的发展、微服务架构的升级,服务的数量、程序的配置日益增多(各种微服务、各种服务器地址、各种参数),传统的配置文件方式和数据库的方式已无法满足开发人员对配......
  • 安装单机版nacos
    1.下载安装包wgethttps://github.com/alibaba/nacos/releases/download/2.2.0/nacos-server-2.2.0.tar.gztarxfnacos-server-2.2.0.tar.gz-C/usr/local/cd/usr/loc......
  • Seata分布式事务框架示例
    一、背景阿里给出了Seata的官方示例,地址:https://github.com/seata/seata-samples,提供了很多示例:springcloud-seata-sharding-jdbc-mybatis-plus-samplesEasytound......
  • Springcloud环境中bootstrap.yml加载原理
    如果是Springcloud项目,一般会将配置中心、注册中心等地址存入bootstrap.yml中,同时将其他的配置存入配置中心。也就是说bootstrap.yml的读取会比较靠前。下面研究其机......
  • CICD流水线 Jenkins + Docker compose 分环境 一键部署SpringCloud项目
    一、环境准备接上篇:上篇搭建好了Jenkins 环境 并把docker-compose.yml Dockerfile 相关jar包推送到了目标服务器。二、分环境部署1、SpringBoot配置pom.xml<pro......
  • nacos自动刷新配置
    在使用​​Nacos​​作为配置中心时,我们希望能够在更改配置文件之后,可以同步到各个服务中,下面我们介绍一下2种实现方式。配置文件:test:name:"test"方式一:如果使用@Config......
  • springcloud day01
    单体架构:业务所有功能都在一个项目中开发,打成一个包部署优点是架构简单部署成本低缺点是耦合度高分布式架构:根据业务功能对系统做拆分,每个业务功能模块作为一个独立的......
  • Spring boot Gateway 使用Nacos注册中心No servers available for service
    主要原因是Gateway没有发现合适的服务ReactiveCompositeDiscoveryClient.discoveryClients中如果只有一个或只有SimpleReactiveDiscoveryClient(没有合适服务就会使用这......