spring cloud 集成 seata 分布式事务

news2024/9/22 23:38:11

spring cloud 集成 seata 分布式事务

基于 seata-server 1.6.x

序言

  • 下载 seata-server
    • 准备一个数据库 seata 专门为 seata-server 做存储,如, 可以指定
      • branch_table
      • distributed_lock
      • global_table
      • lock_table
  • 准备一个业务库,比如存放定单,库存表
    • order_tbl
    • stock_tbl
  • 启动好 nacos

工程目录

两个微服务,order 通过 feign 调用 stock 达到 下单扣库存的操作,同时要保证事务

  • svc-order-service
  • svc-stock-service

seata-server 启动

配置 application.yaml

文件在 conf 目录下, 可通过 seata给的 example 文件作相对应的改动,这里采用 nacos 为注册和配置中心,使用 mysql作为存储:

server:
  port: 7091

spring:
  application:
    name: seata-server

logging:
  config: classpath:logback-spring.xml
  file:
    path: ${user.home}/logs/seata
console:
  user:
    username: seata
    password: seata

# 自己加的
vhost:
  ip: 192.xx.xx.129

seata:
  config:
    type: nacos
    nacos:
      server-addr: ${vhost.ip}:8848
      namespace: seata
      group: SEATA_GROUP
      username:
      password:
      context-path:
      data-id: seataServer.properties
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: ${vhost.ip}:8848
      group: SEATA_GROUP
      namespace: seata
      cluster: default
  server:
    service-port: 8091 #If not configured, the default is '${server.port} + 1000'
    max-commit-retry-timeout: -1
    max-rollback-retry-timeout: -1
    rollback-retry-timeout-unlock-enable: false
    enable-check-auth: true
    enable-parallel-request-handle: true
    retry-dead-threshold: 130000
    xaer-nota-retry-timeout: 60000
    enableParallelRequestHandle: true
    recovery:
      committing-retry-period: 1000
      async-committing-retry-period: 1000
      rollbacking-retry-period: 1000
      timeout-retry-period: 1000
    undo:
      log-save-days: 7
      log-delete-period: 86400000
    session:
      branch-async-queue-size: 5000 #branch async remove queue size
      enable-branch-async-remove: false #enable to asynchronous remove branchSession
  store:
    mode: db
    session:
      mode: db
    lock:
      mode: db
    db:
      datasource: druid
      db-type: mysql
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://${vhost.ip}:3306/seata?rewriteBatchedStatements=true
      user: root
      password: 111111
      min-conn: 10
      max-conn: 100
      global-table: global_table
      branch-table: branch_table
      lock-table: lock_table
      distributed-lock-table: distributed_lock
      query-limit: 1000
      max-wait: 5000
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

在 nacos 增加 dataId=seataServer.properties, 单独给 seata-server 开一个 namespace ,和 nacos中业务的namespace区别开,当然两者可以相同

# 数据存储方式,db代表数据库, 可以覆盖 seata中 application.yaml中的配置
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.xx.xx.129:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=GMT
 
store.db.user=root
store.db.password=111111
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
# 事务、日志等配置
server.recovery.committingRetryPeriod=3000
server.recovery.asynCommittingRetryPeriod=3000
server.recovery.rollbackingRetryPeriod=3000
server.recovery.timeoutRetryPeriod=3000
server.maxCommitRetryTimeout=-1
server.maxRollbackRetryTimeout=-1
server.rollbackRetryTimeoutUnlockEnable=false
server.undo.logSaveDays=7
server.undo.logDeletePeriod=86400000
 
# 客户端与服务端传输方式
transport.serialization=seata
transport.compressor=none
# 关闭metrics功能,提高性能
metrics.enabled=false
metrics.registryType=compact
metrics.exporterList=prometheus
metrics.exporterPrometheusPort=9898

然后启动 /bin/seata-server.bat ,看到 nacos注册中心有服务就可以了

微服务应用

这里取 svc-order-service 作为配置举例,svc-stock-service 是一样的

  1. 依赖
<dependencies>
    <!--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 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.32</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <!-- feign-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!--解决:Did you forget to include spring-cloud-starter-loadbalancer?-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-loadbalancer</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
    </dependency>
</dependencies>
  1. 配置文件

svc-order-service/application.properties:

spring.application.name=svc-order-service
server.port=9091
spring.cloud.nacos.discovery.server-addr=192.xx.xx.129:8848
spring.cloud.nacos.discovery.namespace=dev
spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group
spring.datasource.url=jdbc:mysql://192.xx.xx.129:3306/seata-biz?allowMultiQueries=true
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=111111

spring.datasource.druid.connect-properties.config.decrypt=false
spring.datasource.druid.connect-properties.druid.stat.logSlowSql=true
spring.datasource.druid.connect-properties.druid.stat.slowSqlMillis=200
spring.datasource.druid.filters=config,wall,stat
spring.datasource.druid.initial-size=5
spring.datasource.druid.max-active=100
spring.datasource.druid.max-wait=60000
spring.datasource.druid.min-evictable-idle-time-millis=30000
spring.datasource.druid.min-idle=5
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-password=admin
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.reset-enable=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.validation-query=select 1
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.exclusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
spring.datasource.druid.web-stat-filter.session-stat-enable=true
spring.datasource.druid.web-stat-filter.session-stat-max-count=10
spring.datasource.druid.web-stat-filter.url-pattern=/*

mybatis-plus.type-aliases-package=com.x.z.order.domain
mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
spring.main.allow-bean-definition-overriding=true


# seata 注册到nacos中的命名空间可以和应用不相同
seata.enabled=true
seata.application-id=${spring.application.name}
# 事务组
seata.tx-service-group=my_tx_group #(事务级名称可以随便取,但是在seata-server配置中心要对应)
seata.service.vgroupMapping.my_tx_group=default

# 下面和 seata-server 注册信息保持一致
seata.use-jdk-proxy=true
seata.enable-auto-data-source-proxy=true
seata.registry.type=nacos
# 和 seata-server应用名称一致
seata.registry.nacos.application=seata-server
seata.registry.nacos.server-addr=${spring.cloud.nacos.discovery.server-addr}
# 和 seata-server 配置保持一致
seata.registry.nacos.namespace=seata
seata.registry.nacos.group=SEATA_GROUP

# seata-server 所在的配置中心
seata.config.type=nacos
seata.config.nacos.server-addr=${spring.cloud.nacos.discovery.server-addr}
seata.config.nacos.namespace=seata
seata.config.nacos.group=SEATA_GROUP

  1. 向 seata-server 注册事务组

    看 配置文件中有这个

    # 事务组
    seata.tx-service-group=my_tx_group #(事务级名称可以随便取,但是在seata-server配置中心要对应)
    seata.service.vgroupMapping.my_tx_group=default
    

就需要向 seata-server 配置中心注册 dataId= service.vgroupMapping.my_tx_group, value=default(seata-server 配置的中cluster 那个属性的值)

在这里插入图片描述

在这里插入图片描述
也就是 seata-server 中 application.yml中 cluster 属性值 :
在这里插入图片描述

意思就是向 seata-server cluster注册进行事务组注册,如果不配置 就会出现 no available service ‘null‘ found!!问题

有了以下输出信息,就代表分布式事务注册成功:

register TM success. client version:1.3.0, server version:1.6.1,channel:[id: 0x0
register RM success. client version:1.3.0, server version:1.6.1,channel:[id: 0x8
register success, cost 53 ms, version:1.6.1,role:TMROLE,channel:[id: 0x056f8e04
register success, cost 53 ms, version:1.6.1,role:RMROLE,channel:[id: 0x82d3b8b3

问题

no available service ‘null‘ found

原因就是seata-server 注册的cluster 属性 在我们的应用程序中不匹配或者没有找到

请在 seata-server 中配置 dataId= `service.vgroupMapping.my_tx_group`, value=`default(seata-server 配置的中cluster 那个属性的值)`

good luck!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/357245.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【学习笔记2.19】动态规划、MySQL、Linux、Redis(框架)

动态规划 343整数拆分 class Solution {public int integerBreak(int n) {int dp [] new int [n 1];//dp[i]:正整数i拆分后的最大乘积dp[2] 1;for(int i 2;i < n ;i ){for(int j 1;j < i;j ){dp[i] Math.max(dp[i],Math.max(j * (i - j),j * dp[i - j]));} …

Ubuntu安装opencv库3.4.10,并在cmake工程中引入opencv库

Windows下安装不同&#xff0c;Ubuntu安装OpenCV库时&#xff0c;需要事先安装依赖&#xff0c;而且不同OpenCV库所需的依赖可能会有所不同&#xff0c;下面的依赖亲测 3.4.10 和 4.5.5版本的有效&#xff0c;但是4.6以上版本安装可能会报错。 参考链接&#xff1a;https://bl…

【Python】以邮件的方式定时发送一天的股票分析报告

【Python】以邮件的方式定时发送一天的股票分析报告 文章目录【Python】以邮件的方式定时发送一天的股票分析报告1、Python发送邮件1&#xff09;EmailSender封装2&#xff09;可能存在的问题2、jinja2动态渲染html页面3、阿里云OSS搭建图床1&#xff09;Python上传图片到OSS中…

在Linux和Windows上安装sentinel-1.8.5

记录&#xff1a;380场景&#xff1a;在CentOS 7.9操作系统上&#xff0c;安装sentinel-1.8.5。在Windows上操作系统上&#xff0c;安装sentinel-1.8.5。Sentinel是面向分布式、多语言异构化服务架构的流量治理组件。版本&#xff1a;JDK 1.8 sentinel-1.8.5 CentOS 7.9官网地址…

开发板上搭建vsftpd服务器

1、交叉编译vstftp 下载vsftpd源码&#xff1a;vsftpd-3.0.2.tar.gz # tar –xzvf vsftpd-3.0.2.tar.gz # cd vsftpd-3.0.2 修改makefile文件&#xff1a; CC arm-linux-gnueabihf-gcc # make 将vsftpd文件放到/usr/bin/&…

说说Hibernate

当你在实战项目中需要用到SSH时, 如果你之前只用过Mybatis那自然是不能解决问题的, 因为在很多银行类金融类项目中你可能会使用到Hibernate, 那么关于Hibernate你应该要了解什么呢, 本篇文章就以学习Hibernate框架为目的, 巩固在工作中可能需要用到的这种ORM技术, 同时也欢迎家…

PVE硬件直通之强制IOMMU分组

文章目录检查是否直接支持IOMMU分组配置IOMMU分组不直接支持的需要更新内核参考检查是否直接支持IOMMU分组 下面 以SATA控制器为例&#xff0c;看pci设备是否可以直接支持IOMMU分组 /* 打印pci设备详细信息*/ lspci -vv /* 找到SATA controller 段落*/ 16:00.1 SATA controll…

设计模式:模板模式 CRTP设计习语

一、模板模式 1、模板模式 1&#xff09;定义 定义一个操作中的算法的骨架&#xff08;稳定&#xff09;&#xff0c;而将一些步骤延迟&#xff08;变化&#xff09;到子类中。Template Method使得子类可以不改变&#xff08;复用&#xff09;一个算法的结构即可重定义&…

tensorflow 学习笔记(二):神经网络的优化过程

前言&#xff1a; 学习跟随 如何原谅奋力过但无声的 tensorflow 笔记笔记。 本章主要讲解神经网络的优化过程&#xff1a;神经网络的优化方法&#xff0c;掌握学习率、激活函数、损失函数和正则化的使用&#xff0c;用 Python 语言写出 SGD、Momentum、Adagrad、RMSProp、Ada…

2023-02-18干活记录

MathBERT: 耗时&#xff1a;2-3hours(昨天和人聊天聊完了&#xff0c;今天九点才到实验室&#xff0c;呜呜呜一早上就看了个论文) 读论文&#xff1a;BERT-Based Embedding Model for Formula Retrieval Corpus Description&#xff1a; resource:from MSE;the formulas ex…

腾讯云——负载均衡CLB

负载均衡 CLB 提供四层&#xff08;TCP 协议/UDP 协议/TCP SSL 协议&#xff09;和七层&#xff08;HTTP 协议/HTTPS 协议&#xff09;负载均衡。您可以通过 CLB 将业务流量分发到多个后端服务器上&#xff0c;消除单点故障并保障业务可用性。CLB 自身采用集群部署&#xff0c;…

电子技术——共栅和共源共栅放大器的高频响应

电子技术——共栅和共源共栅放大器的高频响应 我们在之前学过无论是是CS放大器还是CE放大器&#xff0c;都可以看做是一个带通&#xff08;IC低通&#xff09;滤波器。在高频处的响应收到输入电容 CinC_{in}Cin​ 的限制&#xff08;主要是米勒效应&#xff09;。因此&#xff…

中南民族大学数字电路实验一

数字电路实验一基本逻辑门实验1.与非门实现与门2.与非门实现或门3.与非门实现或非门4.与非门实现异或门5.与非门实现与或门6.与非门实现与或非门实验报告结果分析基本逻辑门实验 一、实验目的 1&#xff0e;掌握 logisim 软件的使用方法&#xff1b; 2&#xff0e;学习基于该软…

基于蜣螂算法优化Kmeans图像分割-附代码

基于蜣螂优化Kmeans图像分割算法 - 附代码 文章目录基于蜣螂优化Kmeans图像分割算法 - 附代码1.Kmeans原理2.基于蜣螂算法的Kmeans聚类3.算法实验结果4.Matlab代码摘要&#xff1a;基于蜣螂优化Kmeans图像分割算法。1.Kmeans原理 K-Means算法是一种无监督分类算法&#xff0c;…

蚂蚁感冒---第五届蓝桥杯真题

目录 题目链接 题目描述 分析&#xff1a; 代码&#xff1a; y总综合​ 666 题目链接 1211. 蚂蚁感冒 - AcWing题库 题目描述 分析&#xff1a; y总真牛逼&#xff0c;掉头等价于穿过&#xff0c;以第一个点为分界点&#xff0c;分别判断 代码&#xff1a; &#xff08;自…

Java线程池的创建以及原理

一、为什么要使用线程池 在外面的日常开发中&#xff0c;也使用了不少池化技术&#xff0c;比如线程池、数据库连接池、HTTP连接池等等都是对这个思想的应用。 池化技术的思想主要是为了减少每次获取资源的消耗&#xff0c;提高对资源的利用率。 线程池提供了一种限制和管理资…

centos7系统-kubeadm安装k8s集群(v1.26版本)亲测有效,解决各种坑可供参考

文章目录硬件要求可省略的步骤配置虚拟机ip设置阿里镜像源各服务器初始化配置配置主节点的主机名称配置从节点的主机名称配置各节点的Host文件关闭各节点的防火墙关闭selinux永久禁用各节点的交换分区同步各节点的时间将桥接的IPv4流量传递到iptables的链&#xff08;三台都执行…

PHP面向对象01:面向对象基础

PHP面向对象01&#xff1a;面向对象基础一、关键字说明二、技术实现1. 定义类2. 类成员三、 访问修饰限定符1. public2. protected3. private4. 空修饰限定符四、类内部对象五、构造和析构1. 构造方法2. 析构方法六、范围解析操作符1. 访问类常量2. 静态成员3. self关键字七、类…

自动驾驶:时钟同步

文章目录 一、自动驾驶时间同步简介二、时间同步需要的服务1、PTP1.1 ptp4l三、UTC转换UNIX时间戳(timestamp)一、自动驾驶时间同步简介 二、时间同步需要的服务 1、PTP ptp4l -i mgbe3_0 -f /etc/automotive-slave.cfg & phc2sys -s mgbe3_0 -O 0

IDEA插件 RestfulTool插件——Restful服务开发辅助工具集

IDEA插件 RestfulTool插件——Restful服务开发辅助工具集 目录IDEA插件 RestfulTool插件——Restful服务开发辅助工具集1.插件介绍2.安装方式3.使用方法1.插件介绍 RestfulTool插件。一套 Restful 服务开发辅助工具集&#xff1a; 提供了一个 Services tree 的显示窗口 双击 …