欧阳劲驰 il y a 1 mois
commit
ec4fddbbba

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+target/
+trilog/
+.idea/
+.idea/inspectionProfiles/
+.idea/libraries/
+*.bak
+### !.mvn/wrapper/maven-wrapper.jar
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.gitattributes
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+.mvn
+.mvn/
+
+### NetBeans ###
+nbproject/private/
+build/
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/

+ 96 - 0
pom.xml

@@ -0,0 +1,96 @@
+<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.shkpr.service</groupId>
+    <!--工件名-->
+    <artifactId>KprCustomGateway</artifactId>
+    <!--版本号-->
+    <version>1.0.0-dev</version>
+    <!--项目名-->
+    <name>KprCustomGateway</name>
+
+    <!--项目描述-->
+    <description>
+        项目定制网关服务,作用于项目定制用
+        访问路由前缀为:网关路径/项目名称/项目子路径
+        如:/custom-gw/zy-dma/xxx
+    </description>
+
+
+    <!--属性配置+统一版本管理-->
+    <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.boot.version>2.1.3.RELEASE</spring.boot.version>
+    </properties>
+
+    <!--依赖项-->
+    <dependencies>
+        <!--spring-web-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <!--spring-test-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+        </dependency>
+        <!--lombok-->
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+    </dependencies>
+
+    <!--构建脚本-->
+    <build>
+        <!-- 打成jar包的名称 -->
+        <finalName>v1-alam-biz-plugin-${version}</finalName>
+        <!--插件项-->
+        <plugins>
+            <!--maven-compiler-->
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+            </plugin>
+            <!--spring-boot-->
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+    <!--仓库-->
+    <repositories>
+        <!--阿里中央-->
+        <repository>
+            <id>central</id>
+            <url>https://maven.aliyun.com/repository/central</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+        </repository>
+    </repositories>
+
+    <!--依赖控制-->
+    <dependencyManagement>
+        <dependencies>
+            <!--spring依赖控制-->
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring.boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+</project>

+ 17 - 0
src/main/java/com/shkpr/service/customgateway/CustomGateWayApplication.java

@@ -0,0 +1,17 @@
+package com.shkpr.service.customgateway;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * di框架启动类
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@SpringBootApplication
+public class CustomGateWayApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(CustomGateWayApplication.class, args);
+    }
+}

+ 24 - 0
src/main/java/com/shkpr/service/customgateway/commtools/NameUtils.java

@@ -0,0 +1,24 @@
+package com.shkpr.service.customgateway.commtools;
+
+/**
+ * 名称工具
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+public class NameUtils {
+    /**
+     * 前缀
+     */
+    public static final String GENERATED_NAME_PREFIX = "_gen_key_";
+
+    /**
+     * 生成名称
+     *
+     * @param i 序号
+     * @return 名称
+     */
+    public static String generateName(int i) {
+        return GENERATED_NAME_PREFIX + i;
+    }
+}

+ 17 - 0
src/main/java/com/shkpr/service/customgateway/config/GatewayConfig.java

@@ -0,0 +1,17 @@
+package com.shkpr.service.customgateway.config;
+
+import com.shkpr.service.customgateway.properties.GatewayProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 网关配置
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Configuration
+@EnableConfigurationProperties(GatewayProperties.class)
+public class GatewayConfig {
+
+}

+ 65 - 0
src/main/java/com/shkpr/service/customgateway/domain/FilterDefinition.java

@@ -0,0 +1,65 @@
+package com.shkpr.service.customgateway.domain;
+
+
+import com.shkpr.service.customgateway.commtools.NameUtils;
+import lombok.*;
+import org.springframework.util.StringUtils;
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.constraints.NotNull;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * 过滤器定义
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Setter
+@Getter
+@NoArgsConstructor
+@EqualsAndHashCode
+@ToString
+@Validated
+public class FilterDefinition {
+    /**
+     * 名称
+     */
+    @NotNull
+    private String name;
+    /**
+     * 参数
+     */
+    private Map<String, String> args = new LinkedHashMap<>();
+
+    /**
+     * 文本构造
+     *
+     * @param text 文本
+     */
+    public FilterDefinition(String text) {
+        //等号分割
+        int eqIdx = text.indexOf('=');
+        if (eqIdx <= 0) {
+            setName(text);
+            return;
+        }
+        //设置名称
+        setName(text.substring(0, eqIdx));
+        //设置参数
+        String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ",");
+        for (int i = 0; i < args.length; i++) this.args.put(NameUtils.generateName(i), args[i]);
+    }
+
+    /**
+     * 添加参数
+     *
+     * @param key   k
+     * @param value v
+     */
+    public void addArg(String key, String value) {
+        this.args.put(key, value);
+    }
+}
+

+ 63 - 0
src/main/java/com/shkpr/service/customgateway/domain/PredicateDefinition.java

@@ -0,0 +1,63 @@
+package com.shkpr.service.customgateway.domain;
+
+
+import com.shkpr.service.customgateway.commtools.NameUtils;
+import lombok.*;
+import org.springframework.util.StringUtils;
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.ValidationException;
+import javax.validation.constraints.NotNull;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * 谓词定义
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Setter
+@Getter
+@NoArgsConstructor
+@EqualsAndHashCode
+@ToString
+@Validated
+public class PredicateDefinition {
+    /**
+     * 名称
+     */
+    @NotNull
+    private String name;
+    /**
+     * 参数
+     */
+    private Map<String, String> args = new LinkedHashMap<>();
+
+    /**
+     * 文本构造
+     *
+     * @param text 文本
+     */
+    public PredicateDefinition(String text) {
+        //等号分割
+        int eqIdx = text.indexOf('=');
+        if (eqIdx <= 0)
+            throw new ValidationException("无法解析Predicate定义文本'" + text + "'" + ",必须符合name=value的格式");
+        //设置名称
+        setName(text.substring(0, eqIdx));
+        //设置参数
+        String[] args = StringUtils.tokenizeToStringArray(text.substring(eqIdx + 1), ",");
+        for (int i = 0; i < args.length; i++) this.args.put(NameUtils.generateName(i), args[i]);
+    }
+
+    /**
+     * 添加参数
+     *
+     * @param key   k
+     * @param value v
+     */
+    public void addArg(String key, String value) {
+        this.args.put(key, value);
+    }
+}

+ 50 - 0
src/main/java/com/shkpr/service/customgateway/domain/RouteDefinition.java

@@ -0,0 +1,50 @@
+package com.shkpr.service.customgateway.domain;
+
+import lombok.*;
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.constraints.NotNull;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 路由定义
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Setter
+@Getter
+@NoArgsConstructor
+@EqualsAndHashCode
+@ToString
+@Validated
+public class RouteDefinition {
+    /**
+     * ID
+     */
+    private String id;
+    /**
+     * 谓词集合
+     */
+    private List<PredicateDefinition> predicates = new ArrayList<>();
+    /**
+     * 过滤器集合
+     */
+    private List<FilterDefinition> filters = new ArrayList<>();
+    /**
+     * 目标URI
+     */
+    private @NotNull URI uri;
+    /**
+     * 元数据
+     */
+    private Map<String, Object> metadata = new HashMap<>();
+    /**
+     * 权重
+     */
+    private int order = 0;
+}

+ 24 - 0
src/main/java/com/shkpr/service/customgateway/properties/GatewayProperties.java

@@ -0,0 +1,24 @@
+package com.shkpr.service.customgateway.properties;
+
+import com.shkpr.service.customgateway.domain.RouteDefinition;
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.util.List;
+
+/**
+ * 网关属性
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Getter
+@Setter
+@ConfigurationProperties(prefix = "spring.gateway")
+public class GatewayProperties {
+    /**
+     * 路由集合
+     */
+    List<RouteDefinition> routes;
+}

+ 11 - 0
src/main/resources/application.yml

@@ -0,0 +1,11 @@
+#spring
+spring:
+  application:
+    name: KprCustomGateway
+  gateway:
+    routes:
+      - id: zy-dma
+        uri: http://locaohost:8000/
+#web
+server:
+  port: 9011

+ 29 - 0
src/test/java/com/shkpr/service/customgateway/CustomGateWayApplicationTests.java

@@ -0,0 +1,29 @@
+package com.shkpr.service.customgateway;
+
+import com.shkpr.service.customgateway.domain.RouteDefinition;
+import com.shkpr.service.customgateway.properties.GatewayProperties;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+
+/**
+ * 测试启动类
+ */
+@SpringBootTest
+@RunWith(SpringRunner.class)
+public class CustomGateWayApplicationTests {
+    @Autowired
+    GatewayProperties properties;
+
+    @Test
+    public void test() {
+        List<RouteDefinition> routes = properties.getRoutes();
+
+        routes.forEach(System.out::println);
+    }
+
+}