SpringCloudAlibaba整合分布式事务Seata

news/2024/11/24 18:27:06/

文章目录

  • 1 整合分布式事务Seata
    • 1.1 环境搭建
      • 1.1.1 Nacos搭建
      • 1.1.2 Seata搭建
    • 1.2 项目搭建
      • 1.2.1 项目示意
      • 1.2.2 pom.xml
        • 1.2.2.1 alibaba-demo模块
        • 1.2.2.2 call模块
        • 1.2.2.3 order模块
        • 1.2.2.4 common模块
      • 1.2.3 配置文件
        • 1.2.3.1 order模块
        • 1.2.3.2 call模块
      • 1.2.4 OpenFeign调用
      • 1.2.5 order模块

1 整合分布式事务Seata

点击了解分布式事务Seata相关信息

1.1 环境搭建

1.1.1 Nacos搭建

点击了解Nacos原理和使用

1.1.2 Seata搭建

点击了解Seata搭建

1.2 项目搭建

1.2.1 项目示意

如下图中,alibaba-demo为外层父目录模块,call为调用模块,common为存放共同依赖实体模块,order为支持crud模块
在这里插入图片描述

1.2.2 pom.xml

1.2.2.1 alibaba-demo模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>alibaba-demo</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>common</module><module>order</module><module>call</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><spring.boot.version>2.6.11</spring.boot.version><spring.cloud.version>2021.0.4</spring.cloud.version><spring.cloud.alibaba>2021.0.4.0</spring.cloud.alibaba></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.83</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring.boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring.cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring.cloud.alibaba}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

1.2.2.2 call模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>alibaba-demo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>call</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>${project.version}</version></dependency></dependencies></project>

1.2.2.3 order模块

该模块中引入 spring-cloud-starter-alibaba-seata 模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>alibaba-demo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>order</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>${project.version}</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId></dependency></dependencies></project>

1.2.2.4 common模块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>alibaba-demo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>common</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.0</version></dependency></dependencies></project>

1.2.3 配置文件

1.2.3.1 order模块

server:port: 8000spring:application:name: cloud-order-demodatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=trueusername: xxxxpassword: xxxxtype: com.alibaba.druid.pool.DruidDataSourcecloud:nacos:discovery:server-addr: 127.0.0.1:8848username: xxxxpassword: xxxxmybatis-plus:mapper-locations: 'classpath:mapper/*Mapper.xml'configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplseata:enabled: truetx-service-group: mygroupdata-source-proxy-mode: ATservice:vgroup-mapping:mygroup: defaultgrouplist:default: 127.0.0.1:8091registry:nacos:application: my-stata-servergroup: SEATA_GROUPnamespace: 5658186c-c382-453a-843e-6a490cc80480username: nacospassword: nacosserver-addr: 127.0.0.1:8848config:nacos:group: SEATA_GROUPnamespace: 5658186c-c382-453a-843e-6a490cc80480username: nacospassword: nacosserver-addr: 127.0.0.1:8848data-id: seata.properties

注意:

  • 上面的 seata.tx-service-group: 自定义值 要和它自己 seata.service.vgroup-mapping.自定义值:default自定义值相对应
    还要和 服务端的seata.properties中的service.vgroupMapping.自定义值=default自定义值 相对应
  • 上面的seata.service.vgroup-mapping.自定义值:default要和服务端的seata.propertiesservice.default.grouplist=127.0.0.1:8091 中的 default 相对应
  • 如果启动有error报错:不能找到service.vgroupMapping.自定义值 的服务,可以把service.vgroupMapping.单独配置指定

1.2.3.2 call模块

server:port: 7000spring:application:name: cloud-callcloud:nacos:discovery:server-addr: 127.0.0.1:8848username: nacospassword: nacoslogging:level:# feign日志以什么级别监控哪个接口cn.feign: debug

1.2.4 OpenFeign调用

在call模块中
调整feign日志级别

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FeignConfig {@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;}
}

feign调用接口

import cn.entity.User;
import cn.feign.fallback.UserFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;import java.util.List;@FeignClient(value = "cloud-order-demo",fallbackFactory = UserFeignFallbackFactory.class,path = "/user")
public interface UserFeignService {@PostMapping("/query")public List<User> queryAll();@PostMapping("/save")public boolean save(User user);
}

feign回调工厂接口,可以打印详细堆栈信息

import cn.entity.User;
import cn.feign.UserFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;import java.util.List;@Slf4j
@Component
public class UserFeignFallbackFactory implements FallbackFactory<UserFeignService> {@Overridepublic UserFeignService create(Throwable cause) {log.error("异常原因:{}", cause.getMessage(), cause);return new UserFeignService(){@Overridepublic List<User> queryAll() {return null;}@Overridepublic boolean save(User user) {return false;}};}
}

1.2.5 order模块

controller

@RequestMapping("/user")
@RestController
public class UserController {@Autowiredprivate IUserService userService;@PostMapping("/query")public List<User> query (){List<User> list = userService.list();return list;}@PostMapping("/save")@GlobalTransactional@GlobalLockpublic boolean save (@RequestBody User user){boolean save = userService.save(user);if(save){long count = userService.count();long i = count / 0;}return save;}@PostMapping("/add")public int add (@RequestBody User user){int i = userService.insertUser(user);return i;}}

后续的server以及mapper模块都是集成mybatisplus,没有任何代码,因此不在赘述


http://www.ppmy.cn/news/99487.html

相关文章

shell编程lesson06

流控制 while 和 until 循环 #!/bin/bash# while-count:display a series of numbersi1 while [ $i -le 5 ];doecho $ii$((i1)) done echo "Finished!"while命令的语法结构如下&#xff1a; while commands;do commands;done 我们可以使用while循环改进上一节中的…

MKS SERVO4257D 闭环步进电机_系列4 MODBUS指令说明

第1部分 产品介绍 MKS SERVO 28D/35D/42D/57D 系列闭环步进电机是创客基地为满足市场需求而自主研发的一款产品。具备脉冲接口和RS485/CAN串行接口&#xff0c;支持MODBUS-RTU通讯协议&#xff0c;内置高效FOC矢量算法&#xff0c;采用高精度编码器&#xff0c;通过位置反馈&am…

剑指 Offer 14- I 剪绳子

题目&#xff1a; 给你一根长度为 n 的绳子&#xff0c;请把绳子剪成整数长度的 m 段&#xff08;m、n都是整数&#xff0c;n>1并且m>1&#xff09;&#xff0c;每段绳子的长度记为 k[0],k[1]…k[m-1] 。请问 k[0]k[1]…*k[m-1] 可能的最大乘积是多少&#xff1f;例如&am…

数据结构与算法04:队列

目录 什么是队列&#xff1f; 循环队列 双端队列 阻塞队列 队列的应用场景 每日一练 什么是队列&#xff1f; 在 上一篇文章 中讲述了栈&#xff1a;先进后出就是栈&#xff0c;队列刚好相反&#xff0c;先进先出的数据结构就是队列&#xff0c;还是拿纸箱子来举例&…

SpringBoot整合Swagger3.0

SpringBoot整合Swagger3.0 SpringBoot整合Swagger3.0 引入pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xs…

【chatGPT】使用ChatGPT进行工作提效

参加新星计划2023【使用ChatGPT进行工作提效】记录的写笔记&#xff01; 借助ChatGPT自动制作PPT&#xff1a;https://blog.csdn.net/Catherinemin/article/details/130846117 ChatgGPT生成Excel统计公式&#xff1a;https://blog.csdn.net/Catherinemin/article/details/13085…

DJ6-6/7 文件共享和访问控制、文件保护

目录 6.6 文件共享和访问控制 1、同时存取 2、存取权限 3、文件共享的实现 6.6.1 基于索引结点的共享方式 1、基本思想 2、具体操作 6.6.2 利用符号链接实现文件共享 6.6.3 利用 URL 实现文件共享 6.7 文件保护 6.6 文件共享和访问控制 文件共享的有效控制涉及…

@Autowired 和 @Resource的区别只知道注入方式不同?那可不行,其性能上也有差距!

目录 Autowire vs Resource 性能比较 先上结论&#xff1a; Resource查找Bean的时间复杂度为O(1)&#xff1a; Autowired查找Bean的时间复杂度为O(n)&#xff1a; 不能将所有的Resource无脑替换成Autowired 结合源码分析Autowire vs Resource 性能比较 Autowire注解的处…