SpringCloud集成Seata并使用Nacos做注册中心与配置中心搭建初体验

 最近在为项目引入分布式事务做技术研究,目前来看,seata是个不错的选择,打算看看,本篇文章纯属个人学习的笔记,不负任何责任,本篇介绍以nacos-1.2.0,SpringBoot-2.3.0,seata-1.2.0,mysql-5.7作为seata-server高可用db模式的数据库,搭建分布式事务的demo。

第一步:nacos安装

到nacos官网下载nacos,地址:https://github.com/alibaba/nacos/releases,目前最新版本是1.2.0,下载直接以standalone模式启动,本篇文章重点不在nacos,所以单节点可以使用即可。

第二步:seata服务端安装

1、网上的教程基本是基于旧版来进行说明,seata-1.2.0版本的部署有点不太一样,少了一些配置文件,我们先下载可执行文件下来,地址:https://seata.io/zh-cn/blog/download.html,解压后修改conf/registry.conf文件:

registry {
  type = "nacos"
  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1"
    namespace = ""
    cluster = "default"
  }
}
config {
  type = "nacos"
  nacos {
    serverAddr = "127.0.0.1"
    namespace = ""
    group = "SEATA_GROUP"
  }
}

由于使用nacos作为注册中心,所以conf目录下的file.conf无需理会。然后就可以直接启动seata:sh bin/seata-server.sh,可以在nacos里看到一个名为seata-server的服务了。

2、由于seata使用mysql作为db高可用数据库,故需要在mysql创建一个seata库,并导入数据库脚本,脚本地址:https://github.com/seata/seata/tree/develop/script/server/db,在我提交的源码里也有该数据库脚本,在项目的doc/seata.sql里,还需要在业务库导入undo_log的表,脚本在https://github.com/seata/seata/tree/develop/script/client/at/db,在我的源码里也有。

3、导入配置到nacos的配置中心,到https://github.com/seata/seata/tree/develop/script/config-center这里下载跟目录的confix.txt配置文件以及到nacos目录下载nacos-config.sh脚本,修改config.txt文件内容:

service.vgroupMapping.my_test_tx_group=default
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=username
store.db.password=password
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

上面关于vgroupMapping.my_test_tx_group是和在项目里配置的一致,执行nacos-config.sh把config.txt文件导入到nacos配置中心,执行出现找不到config.txt的话修改一下nacos-config.sh里面关于config.txt的路径指向你本地的即可。

第三步:在程序加入相关依赖以及全局事务,

nacos注册中心与配置依赖:

<!--注册中心客户端-->
<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>

seata依赖:

<!--Seata 包-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-seata</artifactId>
    <version>2.2.0.RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.2.0</version>
</dependency>

application.yml文件加上:

seata:
  enabled: true
  application-id: ${spring.application.name}
  tx-service-group: my_test_tx_group
  config:
    type: nacos
    nacos:
      namespace:
      serverAddr: 127.0.0.1:8848
      group: SEATA_GROUP

  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      namespace:

当然,feign依赖也是不可少的:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

然后在需要分布式事务的地方加上注解:@GlobalTransactional(rollbackFor = Exception.class)

@GlobalTransactional(rollbackFor = Exception.class)
public String booking(String userId, String productId, int count) {
    BigDecimal money = new BigDecimal(1000);
    log.info("下单,用户:{},产品:{},数量:{}",userId,productId,count);
    /**下面就是调用订单服务、用户服务、产品服务*/
    orderClient.create(userId,productId,count,money);
    userClient.debit(userId,money);
    productClient.deduct(productId,count);
    return "success";
}

调用接口测试,即可看到分布式事务是否生效,也可以下载下面的源码进行测试,先把springcloud-seata-demo.sql脚本导入数据库,然后启动所有项目后,POST访问http://localhost:9999/business/booking,具体参数查看该接口。

 

源码地址:点击下载

 

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值