signed

QiShunwang

“诚信为本、客户至上”

springCloud Hoxton.SR9 整合nacos 1.4.1+seata 1.3.0 + sentinel

2021/3/21 0:56:54   来源:

系统基础构建

  1. 新建父工程: cloud_basics_ali
    pom.xml
<description>阿里微服务系统基础构建</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
        <spring-boot-admin.version>2.3.1</spring-boot-admin.version>
        <spring-boot.mybatis>2.1.3</spring-boot.mybatis>
        <tobato.version>1.26.5</tobato.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <pagehelper.boot.version>1.3.0</pagehelper.boot.version>
        <druid.version>1.2.4</druid.version>
        <dynamic-ds.version>3.2.1</dynamic-ds.version>
        <commons.io.version>2.5</commons.io.version>
        <commons.fileupload.version>1.3.3</commons.fileupload.version>
        <velocity.version>1.7</velocity.version>
        <fastjson.version>1.2.75</fastjson.version>
        <minio.version>8.0.3</minio.version>
        <poi.version>4.1.2</poi.version>
        <common-pool.version>2.6.2</common-pool.version>
    </properties>
    <!-- 声明依赖 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud 微服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringCloud alibaba 微服务 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- SpringBoot 依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--  SpringBoot 监控客户端 -->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>${spring-boot-admin.version}</version>
            </dependency>
            <!-- Mybatis 依赖配置 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${spring-boot.mybatis}</version>
            </dependency>
            <!-- pagehelper 分页插件 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>${pagehelper.boot.version}</version>
            </dependency>
            <!-- io常用工具类 -->
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons.io.version}</version>
            </dependency>
            <!-- JSON 解析器和生成器 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
            <!-- 公共资源池 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
                <version>${common-pool.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
  1. 建立系统子工程 basics_modules
<?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>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统业务模块</description>
    <artifactId>basics_modules</artifactId>
    <packaging>pom</packaging>
</project>
  1. 建立系统网关basics_gateway
<?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>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统网关统一访问系统</description>
    <artifactId>basics_gateway</artifactId>

    <dependencies>
        <!-- SpringCloud Gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Sentinel Gateway -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>
        <!-- Sentinel Datasource Nacos -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!-- SpringBoot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 建立底层封装api basics_common_server
<?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>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>底层api封装层</description>
    <artifactId>basics_common_server</artifactId>
    <packaging>pom</packaging>

</project>
  1. 建立授权服务 basics_auth2
<?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>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统对外统一授权应用</description>
    <artifactId>basics_auth2</artifactId>

    <dependencies>
        <!-- SpringCloud Ailibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- SpringBoot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Mysql Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 建立系统内部相互调用basics_api
<?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>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统内部交互调用层</description>
    <artifactId>basics_api</artifactId>
    <packaging>pom</packaging>

</project>
  • model截图
    在这里插入图片描述

工具服务构建

  • 构建basics_common_server子服务
    • 搭建数据源服务common_datasource
      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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>数据源配置中心</description>
    <artifactId>common_datasource</artifactId>

    <dependencies>
        <!-- Druid  数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <!-- 使用多数据源或主从分离模式读取数据-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>${dynamic-ds.version}</version>
        </dependency>
        <!-- 分布式事务管理 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        </dependency>
    </dependencies>
</project>
  • 搭建核心库服务common_core
    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <parent>
       <artifactId>basics_common_server</artifactId>
       <groupId>com.ma</groupId>
       <version>1.0-SNAPSHOT</version>
   </parent>
   <modelVersion>4.0.0</modelVersion>
   <description>核心组件</description>
   <artifactId>common_core</artifactId>

   <dependencies>
       <!-- SpringCloud Openfeign -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
       </dependency>
       <!-- Spring Context Support -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context-support</artifactId>
       </dependency>
       <!-- Spring Web -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
       </dependency>
       <!-- Apache Commons Pool2 -->
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-pool2</artifactId>
       </dependency>
       <!-- Pagehelper -->
       <dependency>
           <groupId>com.github.pagehelper</groupId>
           <artifactId>pagehelper-spring-boot-starter</artifactId>
       </dependency>
       <!-- Hibernate Validator -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-validation</artifactId>
       </dependency>
       <!-- Jackson -->
       <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
       </dependency>
       <!-- Alibaba Fastjson -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
       </dependency>
       <!-- Apache Lang3 -->
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-lang3</artifactId>
       </dependency>
       <!-- Commons Io -->
       <dependency>
           <groupId>commons-io</groupId>
           <artifactId>commons-io</artifactId>
       </dependency>
       <!-- Java Servlet -->
       <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>javax.servlet-api</artifactId>
       </dependency>
   </dependencies>
</project>
  • 搭建日志服务common_core 此步骤可省略

  • 搭建redis服务common_core 此步骤可省略

  • basics_common_server目录结构
    在这里插入图片描述


接口服务构建

  • 构建basics_modules子服务
    • 搭建系统服务一basics_system 这里引用了刚刚搭建的common_corecommon_datasource 数据库使用的是 test_system 操作了 test_demo 这张表
      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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>basics_modules</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统模块</description>
    <artifactId>basics_system</artifactId>

    <dependencies>
        <!-- SpringBoot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- SpringCloud Ailibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!-- SpringBoot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Mysql Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--        引入核心库-->
        <dependency>
            <groupId>com.ma</groupId>
            <artifactId>common_core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--        引入数据源支持-->
        <dependency>
            <groupId>com.ma</groupId>
            <artifactId>common_datasource</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--        引入其他服务调用-->
        <dependency>
            <groupId>com.ma</groupId>
            <artifactId>system_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 配置文件属性
    bootstrap.yml
# Tomcat
server:
  port: 9201

# Spring
spring:
  application:
    # 应用名称  不能使用下划线
    name: basics-system
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: localhost:8848
      config:
        # 配置中心地址
        server-addr: localhost:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

application.yml

# spring配置
spring:
  main:
    allow-bean-definition-overriding: true
  autoconfigure:
#    排除默认数据源
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    dynamic:
      druid:
        initial-size: 5
        min-idle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        filters: stat,wall,slf4j
        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      datasource:
        # 主库数据源
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://ip:port/test_system?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: root
      seata: false    # 开启seata代理,开启后默认每个数据源都代理,如果某个不需要代理可单独关闭
      seata-mode: AT #支持XA及AT模式,默认AT
seata:
  # 默认关闭,如需启用spring.datasource.dynami.seata需要同时开启
  enabled: false
  tx-service-group: my_test_tx_group
  enable-auto-data-source-proxy: false
  service:
    vgroupMapping:
      my_test_tx_group: default
  config:
    type: nacos
    nacos:
      serverAddr: localhost:8848
      username: nacos
      password: nacos
      group: SEATA_GROUP
  registry:
    type: nacos
    nacos:
      server-addr: localhost:8848
      username: nacos
      password: nacos
      group: SEATA_GROUP

# mybatis配置
mybatis:
  # 搜索指定包别名
  typeAliasesPackage: com.basics.system.*
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath:mapper/*.xml


#请求处理的超时时间
ribbon:
  ReadTimeout: 10000
  ConnectTimeout: 10000

# feign 配置
feign:
  sentinel:
    enabled: true
  okhttp:
    enabled: true
  httpclient:
    enabled: false
  client:
    config:
      default:
        connectTimeout: 10000
        readTimeout: 10000
  compression:
    request:
      enabled: true
    response:
      enabled: true
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
logging:
  level:
    com.basics.system: DEBUG

这里我们配置了端口,连接数据库信息,数据库可配置主从或多数据源配置。配置了nacos信息,目前配置文件关闭了 seata事务,稍后测试服务跑通后,可进行开启事务

  • service 普通service 方法
  • controller 普通controller 接口
  • mapper 基础增删改查
  • 启动类
@SpringCloudApplication
@EnableFeignClients
public class SystemApplication {
   public static void main(String[] args) {
       SpringApplication.run(SystemApplication.class, args);
   }
}
  • 截图如下:
    在这里插入图片描述
  • 配置nacos
  1. 下载nacos 1.4.1 https://github.com/alibaba/nacos/tags
    在这里插入图片描述

  2. 设置nacos持久化
    解压打开conf下的application.properties 设置数据库连接信息
    在这里插入图片描述

  3. 数据库执行文件 nacos-mysql.sql展示如下:
    在这里插入图片描述

  • 本地启动nacos bin目录下startup.cmd启动 默认访问地址http://localhost:8848/nacos 默认账号密码 nacos/nacos 备注; 目前nacos启动为单机版 也就是standalone
    在这里插入图片描述

  • 启动basics_system项目 运行状态及nacos服务状态如下:
    在这里插入图片描述
    在这里插入图片描述

  • postman 测试接口状态:
    在这里插入图片描述

  • 以上就是basics_modules 的子服务一的完整搭建步骤
    接下来按照该步骤创建新的子服务 basics_system_02 进行构建系统服务二 端口为9202 访问数据库名称为 test_system_02
    application.name 为 basics-system-02 操作了 test_name 这张表,默认接口也是增删改查。 构建成功后请进行接口测试跑通
    正常启动后nacos展示服务如下:
    在这里插入图片描述

  • 目前项目结构如下:
    在这里插入图片描述


服务相互调用层构建

接下来构建 ruoyi-api 系统内部openfeign接口调用层

  1. ruoyi-api 下新建model ruoyi-api-system02 用来操作basics_system_02
  • 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud_basics_ali</artifactId>
        <groupId>com.ma</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <description>系统模块内部交互openfeign接口</description>
    <artifactId>system_api</artifactId>

    <dependencies>
        <!--        引入核心库-->
        <dependency>
            <groupId>com.ma</groupId>
            <artifactId>common_core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • 新建接口TestService 调用ruoyi-api-system02服务接口 /demo02进行插入一条数据接口

    代码如下:
/**
 * @author mgy
 * @version 1.0
 * @date 2021/3/20
 */
@FeignClient(contextId = "basics-system-test", value = "basics-system-02", fallbackFactory = TestServiceFallbackFactory.class)
public interface TestService {
    /**
     * 增加system服务02 test_name一条数据
     * @param testName {@link TestName}
     * @return int
     */
    @PostMapping("/demo02")
    int insertTestDemo(TestName testName);
}

降级方法TestServiceFallbackFactory

/**
 * @author mgy
 * 服务降级处理
 * @version 1.0
 * @date 2021/3/20
 */
@Component
public class TestServiceFallbackFactory implements FallbackFactory<TestService> {
    private static final Logger log = LoggerFactory.getLogger(TestServiceFallbackFactory.class);
    @Override
    public TestService create(Throwable throwable) {
        log.error("basics_system02服务调用失败:{}", throwable.getMessage());
        return new TestService() {
            @Override
            public int insertTestDemo(TestName testName) {
                return 0;
            }
        };
    }
}

  • resources目录下进行文件夹META-INF 新建文件spring.factories 内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.basics.system.api.service.impl.TestServiceFallbackFactory

这里的路径为TestServiceFallbackFactory的路径
以上为调用层所有内容,
备注:请注意! 这里我将TestName 这个类放置到了basics_common_server 子服务 common_core 核心库下,这也是核心库的意义,将统一的基础的内容放置到该项目下:
在这里插入图片描述


正式调用系统

  1. 实现系统之间进行相互调用
    basics_modules 下的basics_system pom引入刚刚构建的system_api
    pom.xml 输入:
        <!--        引入其他服务调用-->
        <dependency>
            <groupId>com.ma</groupId>
            <artifactId>system_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  1. 引入service ,调用方法:
    在这里插入图片描述
  2. 详细代码:
@RestController
@RequestMapping("/demo")
public class TestDemoController {
    @Resource
    private ITestDemoService testDemoService;

    @Resource
    private TestService testService;

    /**
     * 新增test_demo
     */
    @PostMapping
    public int add(@RequestBody TestDemo testDemo) {
//      调用system02服务 录入 test_name表数据
        testService.insertTestDemo(new TestName("测试111", "1231231"));
        return testDemoService.insertTestDemo(testDemo);
    }
}

重新调用 "http://localhost:9201/demo" 接口查看结果:
在这里插入图片描述
在这里插入图片描述
sql执行控制台我们可以查看系统相互调用接口执行成功! 以上就是我们调用内部接口的详细步骤。 接下来将进行接入seata事务管理。

Seata 事务开启

  1. seata 下载
  2. seata-server 配置
  3. seata-client端配置
  4. 启动测试