1,创建maven项目工程如下
- equipment-admin 后台
- equipment-applet 前台或小程序端或app、h5
- equipment-common 公共模块
- equipment-gateway 网关
- equipment-mapper mapper层操作数据库
- equipment-model 实体类对应数据库表
2,在父pom文件引入依赖
<properties>
<project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.cloud.alibaba.version>2.2.6.RELEASE</spring.cloud.alibaba.version>
<spring.boot.version>2.3.2.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR9</spring.cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--Spring Cloud 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>
<!--Spring Boot 版本管理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--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>
</dependencies>
</dependencyManagement>
3,在admin和applet中引入依赖
<dependencies>
<dependency>
<groupId>awz.equipment</groupId>
<artifactId>equipment-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<configuration>
<mainClass>awz.equipment.getway.EquipmentGatewayMain</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!-- 将依赖到的包都放进去 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
4,在gateway网关引入依赖
<dependencies>
<!-- 引入gateway网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
<configuration>
<mainClass>awz.equipment.getway.EquipmentGatewayMain</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<!-- 将依赖到的包都放进去 -->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
5,编写启动类
@SpringBootApplication
public class EquipmentGatewayMain
{
public static void main( String[] args )
{
SpringApplication.run(EquipmentGatewayMain.class,args);
}
}
admin和applet依次编写
6,分别在admin和applet中编写测试接口
@GetMapping(value = "/test")
public R test() {
return new R(ResultEnum.SUCCESS);
}
7,网关编写断言
server:
port: 9090
spring:
application:
name: equipment-gateway
cloud:
gateway:
routes:
- id: equipment-admin
# uri: lb://equipment-admin
uri: http://localhost:9091
predicates:
- Path=/admin/**
# filters:
# - AddResponseHeader=X-Response-Foo, Bar
# # AuthorizeGatewayFilterFactory自定义过滤器配置,值为true需要验证授权,false不需要
# - Authorize=true
- id: equipment-applet
# uri: lb://equipment-applet
uri: http://localhost:9092
predicates:
- Path=/applet/**
启动测试,访问网关接口即可完成转发