springboot dubbo seata nacos集成 分布式事务seata实现

这篇具有很好参考价值的文章主要介绍了springboot dubbo seata nacos集成 分布式事务seata实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Seata介绍

官网:http://seata.io/zh-cn/docs/overview/what-is-seata.html
Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。

springboot dubbo seata nacos集成 分布式事务seata实现,分布式微服务,高性能,spring boot,dubbo,分布式

dubbo介绍

官网;https://cn.dubbo.apache.org/zh-cn/overview/what/
Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑。
在云原生时代,Dubbo 相继衍生出了 Dubbo3、Proxyless Mesh 等架构与解决方案,在易用性、超大规模微服务实践、云原生基础设施适配、安全性等几大方向上进行了全面升级。

目标

我们先说目标,为各位看官节省不匹配的时间
1、使用nacos做配置中心
2、使用nacos做注册中心
3、微服务模块化
4、使用dubbo作为服务管理
5、使用springboot做脚手架
6、使用seata做分布式事务

版本说明和代码地址

Dubbo :3.1.0
Springboot:2.3.1.RELEASE
Seata:1.6.1
Nacos-config:0.2.10

实现源代码地址
分支:microservice-boot-1.0.4-seata
代码演示和测试:
microservice-boot-common模块
microservice-boot-plat模块

pom.xml

直接上pom文件吧

  
          <dubbo.version>3.1.0</dubbo.version>
          <spring-boot.version>2.3.1.RELEASE</spring-boot.version>
          <spring-context-support.version>1.0.11</spring-context-support.version>
  
          <!-- 微服务相关 -->
          <nacos-config.version>0.2.10</nacos-config.version>
          <io.seata.version>1.6.1</io.seata.version>
  
  
  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

 <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.spring</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>

        <!--nacos config -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${nacos-config.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </dependency>

验证模块

两个模块
microservice-boot-common
microservice-boot- plat
模拟:
plat中controller请求,本地服务(此服务使用分布式事务),本地服务调用dubbo服务(包括palt的保存数据和common的保存日志)

microservice-boot-common

1、yaml配置

  # seata 配置
seata:
  application-id: ${spring.application.name}
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: fb91b495-9490-436f-b9cd-023f2ca42b08
      group: SEATA_GROUP
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
      data-id: seataServer.properties
  # 默认就是这个额,可以不配置
  tx-service-group: default_tx_group
  registry:
    custom:
      name: ${spring.application.name}
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      application: seata-server
      group: SEATA_GROUP
      namespace: 920bb73f-17da-4128-9de0-41893097ce38
      username: nacos
      password: nacos

2、dubbo服务代码

package org.lwd.microservice.boot.common.service.dubbo;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.lwd.microservice.boot.common.api.dto.VisitDubboDTO;
import org.lwd.microservice.boot.common.api.dubbo.VisitDubboService;
import org.lwd.microservice.boot.common.entity.dto.VisitDTO;
import org.lwd.microservice.boot.common.service.VisitService;
import org.lwd.microservice.boot.core.constant.HttpStatusEnum;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @author weidong
 * @version V1.0.0
 * @since 2023/6/28
 */
@Slf4j
@DubboService
public class VisitDubboServiceImpl implements VisitDubboService {

    @Autowired
    VisitService visitService;

    /**
     * 保存
     *
     * @return
     */
    @Override
    public BaseResult<Integer> saveVisitDubboService(VisitDubboDTO visitDubboDTO) {
        log.info("----i am do saveVisitDubboService-----:{}", JSON.toJSONString(visitDubboDTO));
        BaseResult<Integer> baseResult = BaseResult.success();
        VisitDTO visitDTO = new VisitDTO();
        BeanUtils.copyProperties(visitDubboDTO, visitDTO);
        BaseResult<Integer> result = visitService.saveVisit(visitDTO);
        if (result.isSuccess()) {
            baseResult.setData(baseResult.getData());
        } else {
            baseResult.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
            baseResult.setMessage("保存日志失败");
        }

        return baseResult;
    }
}

microservice-boot- plat

1、yaml配置

# seata 配置
seata:
  application-id: ${spring.application.name}
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      namespace: fb91b495-9490-436f-b9cd-023f2ca42b08
      group: SEATA_GROUP
      username: nacos
      password: nacos
      context-path:
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key:
      #secret-key:
      data-id: seataServer.properties
  # 默认就是这个额,可以不配置
  tx-service-group: default_tx_group
  registry:
    custom:
      name: ${spring.application.name}
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      application: seata-server
      group: SEATA_GROUP
      namespace: 920bb73f-17da-4128-9de0-41893097ce38
      username: nacos
      password: nacos


2、dubbo服务实现

package org.lwd.microservice.boot.plat.service.dubbo;

import org.apache.dubbo.config.annotation.DubboService;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.lwd.microservice.boot.plat.api.dto.UserLoginDubboDTO;
import org.lwd.microservice.boot.plat.api.dubbo.UserLoginDubboService;
import org.lwd.microservice.boot.plat.entity.dto.UserLoginDTO;
import org.lwd.microservice.boot.plat.service.UserLoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 登录基本服务
 * @author weidong
 * @version V1.0.0
 * @since 2023/7/3
 */
@DubboService
public class UserLoginDubboServiceImpl implements UserLoginDubboService {

    @Autowired
    UserLoginService userLoginService;

    @Override
    public BaseResult<Integer> saveUserLoginDubbo(UserLoginDubboDTO userLoginDubboDTO) {
        BaseResult<Integer> result = BaseResult.success();
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        BeanUtils.copyProperties(userLoginDubboDTO,userLoginDTO);
        BaseResult<Integer> baseResult = userLoginService.saveUserLogin(userLoginDTO);
        if(baseResult.isSuccess()){
            result.setData(baseResult.getData());
        }
        return result;
    }
}

3、分布式事务服务实现

package org.lwd.microservice.boot.plat.service.impl;

import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.lwd.microservice.boot.common.api.dto.VisitDubboDTO;
import org.lwd.microservice.boot.common.api.dubbo.VisitDubboService;
import org.lwd.microservice.boot.core.constant.HttpStatusEnum;
import org.lwd.microservice.boot.core.entity.BaseResult;
import org.lwd.microservice.boot.plat.api.dto.UserLoginDubboDTO;
import org.lwd.microservice.boot.plat.api.dubbo.UserLoginDubboService;
import org.lwd.microservice.boot.plat.entity.dto.UserLoginDTO;
import org.lwd.microservice.boot.plat.service.UserLoginSeataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author weidong
 * @version V1.0.0
 * @since 2023/7/3
 */
@Slf4j
@Service
public class UserLoginSeataServiceImpl implements UserLoginSeataService {

    @DubboReference
    UserLoginDubboService userLoginDubboService;
    @DubboReference
    VisitDubboService visitDubboService;

    @Override
    @GlobalTransactional(timeoutMills = 30000, name = "default_tx_group")
    public BaseResult<Integer> saveUserLoginSeata(UserLoginDTO dto) throws Exception {
        BaseResult<Integer> result = BaseResult.success();
        log.info("开始全局事务:xid=" + RootContext.getXID());
        log.info("begin userLogin: " + dto);

        //保存登录信息
        UserLoginDubboDTO userLoginDubboDTO = new UserLoginDubboDTO();
        BeanUtils.copyProperties(dto, userLoginDubboDTO);
        BaseResult<Integer> userResult = userLoginDubboService.saveUserLoginDubbo(userLoginDubboDTO);
        if (userResult.isSuccess()) {
            result.setData(userResult.getData());
        } else {
            result.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
            throw new Exception("登录信息保存系统异常");
        }
        if (!result.isSuccess()) {
            return result;
        }

        //保存日志
        VisitDubboDTO visitDubboDTO = new VisitDubboDTO();
        visitDubboDTO.setServerIpAddress("3.3.3.3");
        if (dto.getEnabled().equals(1)) {
            visitDubboDTO.setCreateTime(dto.getCreateTime());
        }
        BaseResult<Integer> visitResult = visitDubboService.saveVisitDubboService(visitDubboDTO);
        if (visitResult.isSuccess()) {
            log.info("visit info pk:{}", visitResult.getData());
            result.setData(visitResult.getData());
        } else {
            result.setCode(HttpStatusEnum.REQUEST_FAIL.getCode());
            throw new Exception("日志保存系统异常");
        }

        return result;
    }
}

验证结果

springboot dubbo seata nacos集成 分布式事务seata实现,分布式微服务,高性能,spring boot,dubbo,分布式

注意事项

1、seata在注册到nacos时,订阅端的应用名称为unknown
临时解决方案:

package org.lwd.microservice.boot.common;

import org.lwd.microservice.boot.middle.runtime.util.YmlUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *
 * @author weidong
 * @version V1.0.0
 * @since 2023/4/7
 */
@SpringBootApplication
public class CommonApplication {
    public static void main(String[] args) {
        //TODO 这块有一个问题,seata在注册到nacos时,订阅端的应用名称为unknown,经验证是获取不到ProjectNameConfig中设置的${spring.application.name}
        //估计是加载顺序获取其他问题,现在这获取数据,优先处理
        System.setProperty("project.name", YmlUtils.getApplicationName());
        SpringApplication.run(CommonApplication.class, args);
    }
}


2、分布式事务默认AT模式文章来源地址https://www.toymoban.com/news/detail-536076.html

到了这里,关于springboot dubbo seata nacos集成 分布式事务seata实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 若依微服务 + seata1.5.2版本分布式事务(安装配置nacos+部署)

    若依官方使用的1.4.0版本seata,版本较低配置相对更麻烦一些 一、seata服务端下载,下载方式介绍两种入口,如下: 1、找到对应版本,下载 binary 即可。 下载包名为:seata-server-1.5.2.zip 2. github上下载   Releases · seata/seata · GitHub  找到对应的1.5.2版本,每个版本下都有一个缩放

    2024年02月09日
    浏览(30)
  • SpringBoot~ dubbo + zookeeper实现分布式开发的应用

    配置服务名字, 注册中心地址, 扫描被注册的包 server.port=8081 #当前应用名字 dubbo.application.name=provider-server #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 #扫描指定包下服务 dubbo.scan.base-packages=com.demo.service 实现一个接口,在接口中完成需求 public interface Translate { String tran

    2024年04月10日
    浏览(33)
  • SpringCloud(17~21章):Alibaba入门简介、Nacos服务注册和配置中心、Sentinel实现熔断与限流、Seata处理分布式事务

    Spring Cloud Netflix项目进入维护模式 https://spring.io/blog/2018/12/12/spring-cloud-greenwich-rc1-available-now 说明 Spring Cloud Netflix Projects Entering Maintenance Mode 什么是维护模式 将模块置于维护模式,意味着 Spring Cloud 团队将不会再向模块添加新功能。我们将修复 block 级别的 bug 以及安全问题,我

    2024年01月19日
    浏览(38)
  • SpringBoot整合Dubbo和Zookeeper分布式服务框架使用的入门项目实例

    Dubbo是一个 分布式服务框架 ,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求。其本质上是个远程服务调用

    2024年01月21日
    浏览(25)
  • 最新版 !快速掌握 JDK17 + springboot3 + springcloud Alibaba : 10、Seata 整合实现分布式事务

    上一节成功启动了seata,传送门: https://blog.csdn.net/qq_16089135/article/details/133989446 1.1 官方文档 中文文档 Seata 是什么 1.2 模式分类 AT :基于支持本地 ACID 事务的关系型数据库。 Java 应用,通过 JDBC 访问数据库。 整体机制:二阶段提交。 一阶段:业务数据和回滚日志记录在同一

    2024年02月06日
    浏览(29)
  • SpringBoot集成Skywalking实现分布式链路追踪

    官方网址:  Apache SkyWalking 官方文档:  SkyWalking 极简入门 | Apache SkyWalking 下载地址 :Downloads | Apache SkyWalking  Agent :以探针的方式进行请求链路的数据采集,并向管理服务上报; OAP-Service :接收数据,完成数据的存储和展示; Storage :数据的存储层,支持ElasticSearch、Mysql、

    2024年02月03日
    浏览(24)
  • 分布式id生成方案及springboot进行集成

    UUID(Universally Unique Identifier) 即通用唯一识别码,是一种由网络软件使用的标识符,它是由IP地址、当前时间戳、随机数、节点等多个部分组成,具有唯一性。但是,UUID方案的缺点是,生成的id较长,不便于存储和使用。 Snowflake算法 它是Twitter公司开源的一个分布式唯一ID生成器

    2023年04月08日
    浏览(17)
  • 分布式:一文吃透分布式事务和seata事务

    什么是事务 事务是并发控制的单位,是用户定义的一个操作序列。 事务特性 原子性(Atomicity): 事务是数据库的逻辑工作单位,事务中包括的诸操作要么全做,要么全不做。 一致性(Consistency): 事务执行的结果必须是使数据库从一个一致性状态变到另一个一致性状态。一致性

    2024年02月07日
    浏览(40)
  • Seata分布式事务

    本地事务,也就是传统的单机事务。在传统数据库事务中,必须要满足四个原则: 分布式事务,就是指不是在单个服务或单个数据库架构下,产生的事务,例如: 跨数据源的分布式事务 跨服务的分布式事务 综合情况 完成上面的操作需要访问三个不同的微服务和三个不同的

    2024年02月09日
    浏览(24)
  • 分布式事务 Seata

    事务(Transaction)是计算机科学中的一个重要概念,主要是指一个 完整的、不可分割的操作序列 。在关系型数据库中,事务通常用于描述对数据库进行的一系列操作的执行单元。 事务的ACID特性 : 原子性(Atomicity):事务是一个原子操作,要么全部执行,要么全部回滚。如

    2024年02月17日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包