1. 技术栈

SpringCloud Alibaba + Nacos + Gateway + MySQL + Mybatis-plus

待接入:Redis + MongoDB + zookeeper + guava + Sentinel + RocketMQ + Seata + Alibaba Cloud OSS + Alibaba Cloud SchedulerX + Alibaba Cloud SMS

版本号

技术栈版本号
SpringCloud Alibaba2022.0.0.0
SpringBoot3.0.2
Nacos服务端:2.4.3 客户端:2022.0.0.0
Gateway4.0.0
Redis6.2.16
MySQL8.0.39
Mybatis-plus3.5.7
未完待续

2. 配置

父工程

<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>com.xiaoqiu</groupId>
  <artifactId>worker-dev</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>worker-dev</name>
  <url>http://maven.apache.org</url>
  <modules>
    <!--
      worker-common: 通用工程,管理工具类、枚举类、异常类、常用属性类、资源类
      worker-pojo: 所有实体类, pojo、bean、entity、bo、vo、dto
      worker-api: web接口(应用)的公用依赖
      service-user-7001: 用户服务,提供用户相关的接口
      service-company-6001: 公司服务,提供公司相关的接口
    -->
    <module>worker-common</module>
    <module>worker-pojo</module>
    <module>worker-api</module>
    <module>service-user-7001</module>
    <module>service-company-6001</module>
      <module>gateway</module>
  </modules>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>17</java.version>
    <spring-cloud.version>2022.0.0</spring-cloud.version>
    <spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
    <jackson.version>2.15.2</jackson.version>
    <mybatis-plus.version>3.5.7</mybatis-plus.version>
    <mybatis-spring.version>3.0.2</mybatis-spring.version>
    <mysql.version>8.0.26</mysql.version>
  </properties>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.2</version>
    <relativePath/>
  </parent>


  <dependencyManagement>
    <dependencies>
      <!--spring cloud 官方依赖-->
      <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}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--  从2020.0开始,bootstrap.yml默认不开启,需要单独配置开启或引入这个依赖    -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
      </dependency>
      <!--      MySQL驱动-->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
      </dependency>
      
			<!--Mybatis-plus-->
      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis-plus.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
      </dependency>
      <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>${mybatis-plus.version}</version>
      </dependency>

      <!-- jackson -->
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
      </dependency>

      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <!--        Java编译包-->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

common包配置

<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>
    <parent>
        <groupId>com.xiaoqiu</groupId>
        <artifactId>worker-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>worker-common</artifactId>
    <packaging>jar</packaging>

    <name>worker-common</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

pojo层

<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>
    <parent>
        <groupId>com.xiaoqiu</groupId>
        <artifactId>worker-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>worker-pojo</artifactId>
    <packaging>jar</packaging>

    <name>worker-pojo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xiaoqiu</groupId>
            <artifactId>worker-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!--        mybatis-plus逆向工具-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--逆向生成模板-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

代码生成配置:

package com.xiaoqiu.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class MPDBConfig {

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
//      /Users/xiaoqiu/Documents/idea_workspaces/worker-dev/worker-pojo/src/main/java/com/xiaoqiu/config
        gc.setOutputDir(projectPath + "/worker-pojo/src/main/java");
        gc.setAuthor("小秋");      // 设置作者
        gc.setFileOverride(true);      // 多次生成是否覆盖之前的
//        gc.setActiveRecord(true);      // 设置是否开启AR模式
//        gc.setIdType(IdType.ASSIGN_ID);      // 设置主键策略
        gc.setServiceName("%sService");      // 设置service名称,默认为IxxxService,去掉I
        gc.setBaseResultMap(true);          // 生成的mapper.xml中包含基本的ResultMap结果集
        gc.setBaseColumnList(true);         // 生成基本的sql查询片段
        gc.setOpen(false);
        // gc.setSwagger2(true); 实体属性 Swagger2 注解
        autoGenerator.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setUrl("jdbc:mysql://192.168.0.250:3306/worker-dev?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("xxx");
        dsc.setPassword("xxx");
        autoGenerator.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(true);      // 开启全局大小写(true: 大写)
        strategy.setNaming(NamingStrategy.underline_to_camel);          // 下划线转驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);    // 下划线转驼峰命名
//        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setInclude("stu");
        strategy.setInclude("users");
        strategy.setInclude("admin");
        autoGenerator.setStrategy(strategy);

        // 包名策略配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.xiaoqiu");
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        pc.setXml("mapper.xml");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
//        pc.setController(null);
        autoGenerator.setPackageInfo(pc);

        // 执行逆向生成
        autoGenerator.execute();
    }
}

API层

<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>
    <parent>
        <groupId>com.xiaoqiu</groupId>
        <artifactId>worker-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>worker-api</artifactId>
    <packaging>jar</packaging>

    <name>worker-api</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xiaoqiu</groupId>
            <artifactId>worker-pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

网关层

<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>
    <parent>
        <groupId>com.xiaoqiu</groupId>
        <artifactId>worker-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>gateway</artifactId>
    <packaging>jar</packaging>

    <name>gateway</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xiaoqiu</groupId>
            <artifactId>worker-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <exclusions>
                <!--                需要排除web,不然会和网关冲突-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

在启动类中,要排除数据源自动自动装配类,因为不使用数据库,所以不配置数据源

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main( String[] args ) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

Bootstrap.yml文件

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      config:
        server-addr: 192.168.0.250:8848
        username: nacos
        password: nacos
        name: gateway-service.yml
        namespace: worker-dev_dev
        file-extension: yaml
        group: dev

因为配置了nacos,application.yml失效,所以下面的动态配置的yml内容在nacos中

server:
  port: 8000
  tomcat:
    uri-encoding: UTF-8
    max-swallow-size: 1024

spring:
  application:
    name: gateway-service
#  config:
#    import: nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.250:8848
        username: nacos
        password: nacos
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: userRoute
          uri: lb://user-service
          predicates:
            - Path=/user/**
        - id: companyRoute
          uri: lb://company-service
          predicates:
            - Path=/company/**

用户服务

<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>
    <parent>
        <groupId>com.xiaoqiu</groupId>
        <artifactId>worker-dev</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>service-user-7001</artifactId>
    <packaging>jar</packaging>

    <name>service-user-7001</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.xiaoqiu</groupId>
            <artifactId>worker-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

用户的bootstrap.yml配置

spring:
  application:
    name: user-service
  cloud:
    nacos:
      config:
        server-addr: 192.168.0.250:8848
        username: nacos
        password: nacos
        name: user-service.yml
        namespace: worker-dev_dev
        file-extension: yaml
        group: dev

nacos中的配置

server:
  port: 7001
  tomcat:
    uri-encoding: UTF-8
    max-swallow-size: 1024

spring:
  application:
    name: user-service
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.0.250:3306/worker-dev?useUnicode=true&useSSL=false&characterEncoding=utf8
    username: xxx
    password: xxx
    hikari:
      connection-timeout: 30000
      idle-timeout: 600000
      max-lifetime: 1800000
      minimum-idle: 5
      maximum-pool-size: 20
      pool-name: DataSourceHikariCP
      auto-commit: true
      connection-test-query: SELECT 1
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.250:8848
        username: nacos
        password: nacos