seata学习笔记(一)——springcloud集成seata

简介: 便于简单展示,只配置了两个服务之间,用于验证seata分布式事务

便于简单展示,只配置了两个服务之间,用于验证seata分布式事务


seata server启动


从seata官网下载seata server包,包中自带了bat和sh启动脚本,注册方式修改为eureka(registry.conf)

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "eureka"
  nacos {
    application = "seata-server"
    serverAddr = "localhost"
    namespace = ""
    cluster = "default"
    username = ""
    password = ""
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
...


完成注册


image.png


整体项目结构



image.png


order服务引入seata及seata配置


pom.xml

<!-- 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配置
seata:
  enabled: true
  application-id: order-seata
  tx-service-group: cloud-web_tx_group
  registry:
    type: eureka
    eureka:
      service-url: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
  service:
    vgroup-mapping:
      cloud-web_tx_group: default


GlobalTransactional配置全局事务


/**
     * 模拟下单操作,验证分布式事务
     * @param id
     * @return
     */
    @GlobalTransactional
    @GetMapping("/{id}/add")
    public void add(@PathVariable Integer id) {
        orderService.add(id);
    }


add接口中模拟分布式事务,

@Override
    public void add(Integer userId) {
        //新增一条订单记录
        Order order = new Order();
        order.setUserId(1);
        order.setProductId(1);
        order.setPayAmount(1D);
        order.setStatus("1");
        order.setAddTime(new Date());
        order.setLastUpdateTime(new Date());
        this.save(order);
        //更新账户信息,模拟分布式事务
        accountFeignService.updateAccount(userId);
    }


account服务中配置类似


源码地址https://github.com/cfpl1201/pinpoint_mybatis.git


相关文章
|
17天前
|
存储 数据可视化 Java
基于MicrometerTracing门面和Zipkin实现集成springcloud2023的服务追踪
Sleuth将会停止维护,Sleuth最新版本也只支持springboot2。作为替代可以使用MicrometerTracing在微服务中作为服务追踪的工具。
59 1
|
2月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
|
3月前
|
NoSQL Java Nacos
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
97 3
|
4天前
|
消息中间件 Java 数据库
新版 Seata 集成 RocketMQ事务消息,越来越 牛X 了!阿里的 Seata , yyds !
这里 借助 Seata 集成 RocketMQ 事务消息的 新功能,介绍一下一个新遇到的面试题:如果如何实现 **强弱一致性 结合**的分布式事务?
新版 Seata 集成 RocketMQ事务消息,越来越 牛X 了!阿里的 Seata , yyds !
|
1月前
|
SQL NoSQL 数据库
SpringCloud基础6——分布式事务,Seata
分布式事务、ACID原则、CAP定理、Seata、Seata的四种分布式方案:XA、AT、TCC、SAGA模式
SpringCloud基础6——分布式事务,Seata
|
2月前
|
Java jenkins Shell
jenkins学习笔记之五:Maven、Ant、Gradl、Node构建工具集成
jenkins学习笔记之五:Maven、Ant、Gradl、Node构建工具集成
|
2月前
|
jenkins 持续交付
jenkins学习笔记之六:共享库方式集成构建工具
jenkins学习笔记之六:共享库方式集成构建工具
|
2月前
|
关系型数据库 MySQL 数据库
SpringCloud2023中使用Seata解决分布式事务
对于分布式系统而言,需要保证分布式系统中的数据一致性,保证数据在子系统中始终保持一致,避免业务出现问题。分布式系统中对数据的操作要么一起成功,要么一起失败,必须是一个整体性的事务。Seata简化了这个使用过程。
78 2
|
2月前
|
jenkins 持续交付
jenkins学习笔记之九:jenkins认证集成github
jenkins学习笔记之九:jenkins认证集成github
|
2月前
|
安全 jenkins 持续交付
jenkins学习笔记之八:jenkins认证集成gitlab
jenkins学习笔记之八:jenkins认证集成gitlab