Seata客服端集成(springCloud+nacos+seata)

一.简单介绍

本demo大部分采用官网的例子,涉及到一个业务入口服务(business),两个微服务(订单服务-order,仓库服务-stock),采用nacos配置,分布式事务用Seata。

相关版本:nacos 采用1.1.4 ,Seata采用seata-1.4.0

二.相关数据库准备

需要搭建两个数据库(采用mysql数据库),订单服务连接seata-order,仓库服务连接seata-stock,
undo_log建表语句如下(官网地址):

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT(20)   NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(100) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';

注意:undo_log表需要在两个数据库都执行。
业务表order_tbl建表sql如下:

DROP TABLE IF EXISTS `order_tbl`;
CREATE TABLE `order_tbl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` varchar(255) DEFAULT NULL,
  `commodity_code` varchar(255) DEFAULT NULL,
  `count` int(11) DEFAULT 0,
  `money` int(11) DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

业务表storage_tbl建表sql如下:

DROP TABLE IF EXISTS `storage_tbl`;
CREATE TABLE `storage_tbl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `commodity_code` varchar(255) DEFAULT NULL,
  `count` int(11) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`commodity_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 初始化库存模拟数据
INSERT INTO storage_tbl (id, commodity_code, count) VALUES (1, 'product-1', 1000);
INSERT INTO storage_tbl (id, commodity_code, count) VALUES (2, 'product-2', 0);

建表成功之后,如下图所示:


数据库表.png

三.客服端框架搭建

具体的seata客服端框架,可以参考官网给的例子,springCloud_nacos_seata
直接用idea搭建一个demo,组建common模块,business业务入口模块,order,stock服务模块

1.搭建基础模块(common)

直接看common模块pom.xml,主要是访问数据库的相关jar,和seata-all.jar

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

    <parent>
        <artifactId>seate-all-parent</artifactId>
        <groupId>com.seate.info</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.seata</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>common</name>
    <description>Demo project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <!--<dependency>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
                <version>1.4.0</version>
            </dependency>-->
            <dependency>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-all</artifactId>
                    <version>1.4.0</version>
                </dependency>
        </dependencies>
    </project>

2.搭建order服务模块(stock服务依赖的包一模一样)

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"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.seate</groupId>
    <artifactId>order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- nacos -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version> 0.9.0.RELEASE </version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.1.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>com.seata</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

3.搭建business模块

不需要连接数据库,所以不需要引用common,但是需要单独依赖seata-all.jar
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"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.seate</groupId>
    <artifactId>business</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>business</name>
    <description>Demo project for Spring Boot</description>


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

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>


    <!-- nacos -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version> 0.9.0.RELEASE </version>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>1.1.4</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        <version>2.1.0.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>io.seata</groupId>
                <artifactId>seata-all</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>

    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-all</artifactId>
        <version>1.4.0</version>
    </dependency>
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-openfeign-core</artifactId>
    </dependency>

    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.conf</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

</project>

上述服务搭建完成之后的目录如下:


image.png

四.客服端配置修改

1.registry.conf文件

这个文件需要存放到根目录,order,stock,business都需要copy一份到根目录,并且这个文件跟seata服务端的一样。服务端配置可以参考上一篇文章(Seata服务端配置(nacos))

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"
  loadBalance = "RandomLoadBalance"
  loadBalanceVirtualNodes = 10
  nacos {
    application = "seata-server"
    serverAddr = "nacos的ip地址:8848"
    namespace = "df2011b0-ed94-4fd2-9a33-baa6f97f5af5"
    group = "SEATA_GROUP"
    cluster = "default"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"
  nacos {
    serverAddr = "nacos的ip地址:8848"
    namespace = "df2011b0-ed94-4fd2-9a33-baa6f97f5af5"
    group = "SEATA_GROUP"
  }

}

注:registry中的group和cluster两个属性很重要,不然后面服务启动后会报如下错误:no available service found in cluster 'default', please make sure registry config correct and keep your seata server running

2.application.yml文件

order服务和stock服务配置如下:

server:
  port: 8090
  servlet:
      context-path: /order

spring:
    application:
        name: order-service
    cloud:
        nacos:
            discovery:
                server-addr: nacos地址:8848
                namespace: df2011b0-ed94-4fd2-9a33-baa6f97f5af5
        alibaba:
            seata:
                tx-service-group: order-tx-grp
    datasource:
        druid:
          url: jdbc:mysql://数据库地址:3306/seata-order?useUnicode=true
          driver-class-name: com.mysql.jdbc.Driver
          username: username
          password: password
feign:
  hystrix:
    enabled: false

stock服务的配置如下:

server:
  port: 8092
  servlet:
    context-path: /stock

spring:
  application:
    name: stock-service
  cloud:
    alibaba:
      seata:
        tx-service-group: stock-tx-grp
    nacos:
      discovery:
        server-addr: nacos地址:8848
        namespace: df2011b0-ed94-4fd2-9a33-baa6f97f5af5
  datasource:
      druid:
          url: jdbc:mysql://数据库地址:3306/seata-stock?useUnicode=true
          driver-class-name: com.mysql.jdbc.Driver
          username: username
          password: password
feign:
  hystrix:
    enabled: false

注:配置文件中的tx-service-group配置的【stock-tx-grp】必须和seata中在nacos配置的service.vgroupMapping.{事务组名称},一致

business服务配置如下:

server:
  port: 8093
  servlet:
    context-path: /business

spring:
  application:
    name: business-service
  cloud:
    alibaba:
      seata:
        tx-service-group: order-tx-grp
    nacos:
      discovery:
        server-addr: nacos地址:8848
        namespace: df2011b0-ed94-4fd2-9a33-baa6f97f5af5

3.数据库代理配置文件(Java文件)

配置文件需要放到项目中,如下:

package com.seate.stock.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class MyBatisConfig {

    /**
     * @param sqlSessionFactory SqlSessionFactory
     * @return SqlSessionTemplate
     */
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * 从配置文件获取属性构造datasource,注意前缀,这里用的是druid,根据自己情况配置,
     * 原生datasource前缀取"spring.datasource"
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    /**
     * 构造datasource代理对象,替换原来的datasource
     * @param druidDataSource
     * @return
     */
    @Primary
    @Bean("dataSource")
    public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSourceProxy);
        SqlSessionFactory factory = null;
        try {
            factory = bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return factory;
    }

    /**
     * MP 自带分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor page = new PaginationInterceptor();
        page.setDialectType("mysql");
        return page;
    }

}

注:AT模式下需要配置
这个配置文件需要放到order,stock服务内,如下图:

MyBatisConfig.png

配置类配置好之后,在应用启动类中关闭SpringBoot的DataSource自动装载

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication(scanBasePackages = {"com.seate"}, exclude = DataSourceAutoConfiguration.class)
public class StockApplication {
    public static void main(String[] args) {
        SpringApplication.run(StockApplication.class, args);
    }
}

五.编写业务代码

order,stock中的业务代码可以参考官网demo。下面只粘贴对应service相关代码,controller代码参考官网。
business服务中代码如下:

package com.seate.business.service;

import com.seate.business.feign.OrderFeignClient;
import com.seate.business.feign.StorageFeignClient;
import com.seate.business.vo.CommintRequest;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 功能描述:
 *
 * @Author: 
 * @Date: 2020/12/7 17:58
 */
@Service
@Slf4j
public class BusinessService {
    @Resource
    private OrderFeignClient orderFeignClient;

    @Resource
    private StorageFeignClient storageFeignClient;
    /**
     * 下单:创建订单、减库存,涉及到两个服务
     *
     * @param userId
     * @param commodityCode
     * @param count
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public void placeOrder(String userId, String commodityCode, Integer count) {
        CommintRequest request=new CommintRequest();
        request.setUserId(userId);
        request.setCommodityCode(commodityCode);
        request.setCount(count);
        orderFeignClient.placeOrderCommit(request);
        storageFeignClient.deduct(commodityCode, count);
    }
}

注:
@GlobalTransactional 开启全局事务,放在business入口处,其中通过feign调用了order和stock服务

order服务的业务处理方法如下:

package com.seate.order.service;

import com.seate.order.dao.OrderInfoDao;
import com.seate.order.feign.StorageFeignClient;
import com.seate.order.model.OrderInfo;
import com.seate.order.vo.CommintRequest;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoProperties;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 功能描述:
 *
 * @Author: 
 * @Date: 2020/12/7 17:04
 */
@Slf4j
@Service
public class OrderInfoService {
    @Resource
    private OrderInfoDao orderInfoDao;

    public void placeOrder(CommintRequest request) {
        BigDecimal orderMoney = new BigDecimal(request.getCount()).multiply(new BigDecimal(5));
        OrderInfo order = new OrderInfo()
                .setUserId(request.getUserId())
                .setCommodityCode(request.getCommodityCode())
                .setCount(request.getCount())
                .setMoney(orderMoney);
        orderInfoDao.insert(order);
    }
}

order服务模拟订单创建操作

stock服务业务代码如下:

package com.seate.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seate.stock.dao.StorageInfoDao;
import com.seate.stock.model.StorageInfo;
import com.seate.stock.service.IStorageInfoService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 功能描述:
 *
 * @Author: 
 * @Date: 2020/12/7 17:44
 */
@Service
@Slf4j
public class StorageInfoService implements IStorageInfoService {

    @Resource
    private StorageInfoDao storageInfoDao;

    /**
     * 减库存
     *
     * @param commodityCode
     * @param count
     */
    @Override
    public void deduct(String commodityCode, int count) {
        log.info(">>>>>>StorageInfoService begin,XID为" + RootContext.getXID());
        QueryWrapper<StorageInfo> wrapper = new QueryWrapper<>();
        wrapper.setEntity(new StorageInfo().setCommodityCode(commodityCode));
        StorageInfo storage = storageInfoDao.selectOne(wrapper);
        storage.setCount(storage.getCount() - count);
        storageInfoDao.updateById(storage);
        if (commodityCode.equals("product-2")) {
            throw new RuntimeException("异常:模拟业务异常:Storage branch exception");
        }
    }
}

在stock服务中模拟抛出了异常,看order服务创建的订单是否会回滚

六.进行全局事务验证

启动nacos,seata服务,然后启动stock,order,business服务,
查看nacos服务列表如下:

1.jpg

上述表示各个服务都启动正常,并注册到nacos成功
查看Seata服务端日志如下:
image.png

查看order,stock,business服务,出现如下日志表示本地事务注册成功:


image.png

1.验证回滚功能

初始值:订单表order_tbl 没有数据,仓库表库存为10
访问接口:http://localhost:8093/business/placeOrder/rollback
返回:

{
timestamp: "2020-12-15T11:35:48.771+0000"
status: 500
error: "Internal Server Error"
message: "status 500 reading StorageFeignClient#deduct(String,Integer); content: {"timestamp":"2020-12-15T11:35:48.663+0000","status":500,"error":"Internal Server Error","message":"异常:模拟业务异常:Storage branch exception","path":"/stock/storage/deduct"}"
path: "[/business/placeOrder/rollback](chrome-extension://gpifhhbaillafhgecomgdilnmplnoelg/business/placeOrder/rollback "Click to insert into URL field")"
}

查看order数据库看订单是否创建成功:


image.png

查看stock数据库看库存是否扣减:


image.png

查看order服务日志如下:


image.png

查看stock服务日志如下:


image.png

查看business服务日志如下:
image.png

2.验证提交功能

初始值:订单表order_tbl 没有数据,仓库表库存为10
访问接口:http://localhost:8093/business/placeOrder/commit
返回值:true
查看order数据库订单是否创建成功:

image.png

查看stock数据库库存是否扣减:
image.png

查看order服务日志如下:
image.png

查看stock服务日志如下:
image.png

查看business服务日志如下:
image.png

七.搭建过程遇到的些异常情况

1.nacos配置读取不到

[imeoutChecker_1] i.s.c.r.netty.NettyClientChannelManager  : no available service 'null' found, please make sure registry config correct

出现上述异常,可能是nacos配置没有读取到,需要确认下nacos的版本,看下nacos客服端版本是否和服务端不一致,或者版本较低。本demo中的nacos版本1.1.4版本,更nacos服务端版本一致

2.seata客服端和服务端版本不一致

[imeoutChecker_1] i.s.c.r.netty.NettyClientChannelManager  : no available service 'default' found, please make sure registry config correct

nacos配置可以读取,但是seata客服端版本太低,本demo服务端版本是1.4.0,所以客服端版本需要升级。版本不一致也会造成上述异常

3.seata客服端版本太低

org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.seata.spring.annotation.GlobalTransactionScanner]: Factory method 'globalTransactionScanner' threw exception; nested exception is io.seata.common.exception.ShouldNeverHappenException: Can't find any object of class org.springframework.context.ApplicationContext

这个原因是引用的spring-cloud-starter-alibaba-seata包中的seata-all版本太低,和服务端版本对不上,需要剔除掉包中低版本的seata-all

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <version>2.1.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

本demo到这里就结束了,如果想看seata服务端搭建可以参考上一篇文章(Seata服务端搭建(nacos))

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容