springboot+openFeign+nacos+seata开发实战下篇

springboot+openFeign+nacos+seata开发实战下篇,第1张

由于springboot+openFeign+nacos+seata开发实战内容实在过多,故分成上下两篇来完成。

文章目录
  • 项目管理规范
  • seata程序实战
    • product服务详解
    • order服务详解
  • 本文小结


项目管理规范

本示例通过Seata中间件实现分布式事务,通过订单微服务执行下单 *** 作,然后由订单微服务调用商品微服务执行扣除库存的 *** 作,这也是微服务中一个典型的分布式事务场景。


seata程序实战

为了后续方便使用SpringCloud Alibaba进行开发, 首先创建一个pom类型的父项目, 主要用于项目技术栈版本管理, 创建一个maven项目,名称为spring-cloud-alibaba-example, 去除src文件, 修改pom文件


<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.0modelVersion>

    <groupId>ah.widethgroupId>
    <artifactId>spring-cloud-alibaba-exampleartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.12.RELEASEversion>
        <relativePath/> 
    parent>

    <packaging>pompackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR12spring-cloud.version>
        <com-alibaba-cloud.version>2.2.7.RELEASEcom-alibaba-cloud.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>

            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${com-alibaba-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

project>

后续创建的项目都放到此目录下, 只需要声明groupId和artifactId, 会自动引用父项目spring-cloud-alibaba-example的版本。与其说是父项目, 不如说是根项目: 因为下面每学习一个新的技术, 就会新建一个真正的父项目, 而在对应的父项目下面又会创建许多的子项目

seata-openFeign-nacos-example子项目详细结构


seata-openFeign-nacos-example父工程的pom.xml文件


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <artifactId>spring-cloud-alibaba-exampleartifactId>
        <groupId>ah.widethgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>


    <artifactId>seata-openFeign-nacos-exampleartifactId>
    <name>seata-openFeign-nacos-examplename>
    <description>seata分布式事务框架和openFeign整合的父工程description>
    <packaging>pompackaging>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>utf-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <dubbo.version>2.7.13dubbo.version>
        <nacos.version>1.4.1nacos.version>
    properties>

    <modules>
        <module>ordermodule>
        <module>productmodule>
    modules>

    <dependencies>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            
            <exclusions>
                <exclusion>
                    <artifactId>nacos-clientartifactId>
                    <groupId>com.alibaba.nacosgroupId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>com.alibaba.nacosgroupId>
            <artifactId>nacos-clientartifactId>
            <version>${nacos.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>


    dependencies>


project>


product服务详解

product项目作为服务提供者,maven文件


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <artifactId>seata-openFeign-nacos-exampleartifactId>
        <groupId>ah.widethgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>productartifactId>
    <name>productname>
    <description>商品服务模块description>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>utf-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.9version>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>com.github.xiaoymingroupId>
            <artifactId>swagger-bootstrap-uiartifactId>
            <version>1.8.1version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

    dependencies>

    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <encoding>${project.build.sourceEncoding}encoding>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    
    <repositories>
        <repository>
            <id>publicid>
            <name>aliyun nexusname>
            <url>http://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
    repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>publicid>
            <name>aliyun nexusname>
            <url>http://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>


project>

yaml配置文件

server:
  port: 8381

spring:
  application:
    name: product-service
  cloud:  #nacos注册中心地址的配置
    nacos:
      discovery:
        server-addr: localhost:8848  # nacos 注册中心的地址

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/product?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
  profiles:
    include: config

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

product数据库及其库表

/*
 Navicat Premium Data Transfer

 Source Server         : 127.0.0.1
 Source Server Type    : MySQL
 Source Server Version : 80013
 Source Host           : localhost:3306
 Source Schema         : product

 Target Server Type    : MySQL
 Target Server Version : 80013
 File Encoding         : 65001

 Date: 29/04/2022 14:38:29
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product`  (
  `pid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品表主键',
  `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
  `price` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
  `stock` int(16) NULL DEFAULT NULL COMMENT '商品库存',
  `del_flag` char(3) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)',
  `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime(0) NOT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, '苹果笔记本', 1000.00, 78, '0', 'admin', '2022-04-23 19:50:25', 'admin', '2022-04-23 22:53:44', NULL);
INSERT INTO `product` VALUES (2, '小米手机', 800.00, 200, '0', 'admin', '2022-04-23 19:51:17', 'admin', '2022-04-23 22:53:51', NULL);
INSERT INTO `product` VALUES (3, '蓝牙耳机', 200.00, 300, '0', 'admin', '2022-04-23 19:52:07', 'admin', '2022-04-23 22:53:56', NULL);

-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime(0) NOT NULL,
  `log_modified` datetime(0) NOT NULL,
  `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ProductApplication {

    public static void main(String[] args) {

        SpringApplication.run(ProductApplication.class, args);
        System.out.println("商品服务8381启动了!!");
    }

}

swagger配置

package ah.wideth.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 接口文档的
 * 描述信息
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_12)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("ah.wideth.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("seata分布式事务测试API接口文档")
                .termsOfServiceUrl("http://127.0.0.1:8381/swagger-ui.html")
                .version("1.0")
                .build();
    }

}

ProductServiceImpl文件

package ah.wideth.service.impl;

import ah.wideth.entity.Product;
import ah.wideth.mapper.ProductMapper;
import ah.wideth.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    /***
     * 通过商品ID
     * 找到商品
     * @param pid
     * @return
     */
    @Override
    public Product findProductById(Integer pid) {

        Product product = productMapper.findProductById(pid);
        return product;
    }

    /***
     * 减少库存
     * @param pid
     * @param num
     */
    @Override
    @Transactional
    public void reduceInventory(Integer pid, Integer num) {

        Product product = productMapper.findProductById(pid);
        product.setStock(product.getStock() - num);//减库存
        productMapper.saveProduct(product);

    }


}

ProductController文件

import ah.wideth.entity.Product;
import ah.wideth.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(tags = "商品")
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @GetMapping("/findProductById")
    @ApiOperation(value = "查询商品信息")
    public Product findProductById(Integer pid) {

        Product product = iProductService.findProductById(pid);
        return product;

    }

    @GetMapping("/reduceInventory")
    @ApiOperation(value = "减少库存")
    public String reduceInventory(Integer pid,Integer num) {

        iProductService.reduceInventory(pid,num);
        return "success";

    }
    
}

接口调用测试


order服务详解

order服务作为消费者,去调用product生产者服务,pom.xml文件引入


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <artifactId>seata-openFeign-nacos-exampleartifactId>
        <groupId>ah.widethgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    <artifactId>orderartifactId>
    <name>ordername>
    <description>订单服务模块description>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>utf-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>io.seatagroupId>
            <artifactId>seata-spring-boot-starterartifactId>
            <version>1.4.0version>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.9version>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.7.0version>
        dependency>
        <dependency>
            <groupId>com.github.xiaoymingroupId>
            <artifactId>swagger-bootstrap-uiartifactId>
            <version>1.8.1version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.70version>
            <scope>compilescope>
        dependency>
        
    dependencies>
    
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <encoding>${project.build.sourceEncoding}encoding>
                configuration>
            plugin>

            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

    
    <repositories>
        <repository>
            <id>publicid>
            <name>aliyun nexusname>
            <url>http://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
    repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>publicid>
            <name>aliyun nexusname>
            <url>http://maven.aliyun.com/nexus/content/groups/public/url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
    pluginRepositories>


project>

yaml文件

server:
  port: 8380

spring:
  application:
    name: order-service
  cloud:  #nacos注册中心地址的配置
    nacos:
      discovery:
        server-addr: localhost:8848  # nacos 注册中心的地址

  #seata配置
#  alibaba:
#    seata:
#      #事务分组
#      tx-service-group: my_test_tx_group

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/order?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
  profiles:
    include: config

seata:
  tx-service-group: my_test_tx_group #事务分组

mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml

order数据库及其库表

/*
 Navicat Premium Data Transfer

 Source Server         : 127.0.0.1
 Source Server Type    : MySQL
 Source Server Version : 80013
 Source Host           : localhost:3306
 Source Schema         : order

 Target Server Type    : MySQL
 Target Server Version : 80013
 File Encoding         : 65001

 Date: 29/04/2022 15:01:17
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for order_info
-- ----------------------------
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单表主键',
  `real_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户姓名',
  `pid` bigint(20) NULL DEFAULT NULL COMMENT '商品ID',
  `pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
  `price` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
  `num` int(20) NULL DEFAULT NULL COMMENT '商品数量',
  `del_flag` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '0' COMMENT '删除标志(0代表存在 1代表删除)',
  `create_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of order_info
-- ----------------------------
INSERT INTO `order_info` VALUES (19, '测试人员', 1, '苹果笔记本', 1000.00, 3, '0', '', '2022-04-29 14:09:10', '', '2022-04-29 14:09:10', NULL);
INSERT INTO `order_info` VALUES (20, '测试人员', 1, '苹果笔记本', 1000.00, 3, '0', '', '2022-04-29 14:36:50', '', '2022-04-29 14:36:50', NULL);

-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime(0) NOT NULL,
  `log_modified` datetime(0) NOT NULL,
  `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

ProductClient文件

import ah.wideth.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "product-service")
public interface ProductClient {

    @GetMapping("/api/product/reduceInventory")
    String reduceInventory(@RequestParam("pid") Integer pid, @RequestParam("num") Integer num);

    @GetMapping("/api/product/findProductById")
    Product findProductById(@RequestParam("pid") Integer pid);

}

OrderServiceImpl文件

import ah.wideth.client.ProductClient;
import ah.wideth.entity.Order;
import ah.wideth.entity.Product;
import ah.wideth.mapper.OrderMapper;
import ah.wideth.service.IOrderService;
import com.alibaba.fastjson.JSON;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private ProductClient productClient;

    @Autowired
    private OrderMapper orderMapper;

    /***
     * 创建订单
     * @param pid
     * @return
     */
    @Override
    @GlobalTransactional//全局事务
    public Order createOrder(Integer pid) {

        // 调用商品微服务,查询商品信息
        // 分支事务
        Product product = productClient.findProductById(pid);
        log.info("查询到{}号商品的信息,内容是:{}",pid, JSON.toJSONString(product));

        // 创建订单
        Order order = new Order();

        order.setRealName("测试人员");
        order.setPid(product.getPid());
        order.setPName(product.getName());
        order.setPrice(product.getPrice());
        order.setNum(3);

        // 本地事务
        orderMapper.saveOrder(order);
        log.info("创建订单成功,订单信息为{}",JSON.toJSONString(order));

        // 扣库存
        // 分支事务
        productClient.reduceInventory(pid,order.getNum());

        return order;
    }
}

接口调用

接口调用成功


本文小结

本文介绍了seata如何使用,其实使用起来也不是很难,seata是代码无侵入式的解决方案,实际使用中只需要一个 @GlobalTransactional注解就可以解决问题。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/789432.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存