TODO: Load MediaType
This commit is contained in:
parent
ade3923715
commit
f6051019e6
74
pom.xml
74
pom.xml
|
@ -5,35 +5,37 @@
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.1.5.RELEASE</version>
|
<version>2.1.6.RELEASE</version>
|
||||||
<relativePath />
|
<relativePath />
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
<groupId>cn.ecpark.service</groupId>
|
<groupId>cn.ecpark.service</groupId>
|
||||||
<artifactId>gateway-parent</artifactId>
|
<artifactId>usergw-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>gateway</name>
|
<name>usergw-parent</name>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Yame User Gateway</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
||||||
|
<dubbo.version>2.7.1</dubbo.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -42,22 +44,58 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||||
|
<version>${dubbo.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo</artifactId>
|
||||||
|
<version>${dubbo.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.dubbo</groupId>
|
||||||
|
<artifactId>dubbo-dependencies-zookeeper</artifactId>
|
||||||
|
<version>${dubbo.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>log4j</groupId>
|
||||||
|
<artifactId>log4j</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement></dependencyManagement>
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
<build>
|
<dependency>
|
||||||
<plugins>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<plugin>
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<version>${spring-cloud.version}</version>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<type>pom</type>
|
||||||
</plugin>
|
<scope>import</scope>
|
||||||
</plugins>
|
</dependency>
|
||||||
</build>
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>usergw-service</module>
|
|
||||||
<module>usergw-api</module>
|
<module>usergw-api</module>
|
||||||
|
<module>usergw-service</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -4,15 +4,18 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>cn.ecpark.service</groupId>
|
<groupId>cn.ecpark.service</groupId>
|
||||||
<artifactId>gateway-parent</artifactId>
|
<artifactId>usergw-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
|
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>cn.ecpark.service</groupId>
|
<groupId>cn.ecpark.service</groupId>
|
||||||
<artifactId>usergw-api</artifactId>
|
<artifactId>usergw-api</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>demo-api</name>
|
<name>demo-api</name>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Gateway API</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
@ -24,24 +27,11 @@
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<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>
|
</dependencyManagement>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package cn.ecpark.service.usergw;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class DemoApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package cn.ecpark.service.usergw.api;
|
||||||
|
|
||||||
|
public interface IDemo {
|
||||||
|
String Hello();
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
server.port= 8888
|
|
|
@ -1,16 +0,0 @@
|
||||||
package cn.ecpark.service.usergw;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class DemoApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,41 +1,73 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
||||||
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>cn.ecpark.service</groupId>
|
<groupId>cn.ecpark.service</groupId>
|
||||||
<artifactId>gateway-parent</artifactId>
|
<artifactId>usergw-parent</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>cn.ecpark.service</groupId>
|
<groupId>cn.ecpark.service</groupId>
|
||||||
<artifactId>usergw</artifactId>
|
<artifactId>usergw</artifactId>
|
||||||
<version>${project.parent.version}</version>
|
<version>${project.parent.version}</version>
|
||||||
<name>demo</name>
|
<name>demo</name>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Gateway Service</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.ecpark.service</groupId>
|
||||||
|
<artifactId>usergw-api</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
<!-- <dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency> -->
|
||||||
|
|
||||||
<dependencyManagement>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-dependencies</artifactId>
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
<version>${spring-cloud.version}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
<scope>import</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.yaml</groupId>
|
||||||
|
<artifactId>snakeyaml</artifactId>
|
||||||
|
<version>1.21</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.ecpark.service</groupId>
|
||||||
|
<artifactId>usergw-api</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
|
||||||
|
<dependencyManagement></dependencyManagement>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -43,6 +75,16 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package cn.ecpark.service.usergw;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@SpringBootApplication
|
||||||
|
@SpringBootConfiguration
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@EnableDubboConfig
|
||||||
|
@ComponentScan("cn.ecpark.service.usergw")
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(App.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,13 +0,0 @@
|
||||||
package cn.ecpark.service.usergw;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class DemoApplication {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
package cn.ecpark.service.usergw.biz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter
|
||||||
|
*/
|
||||||
|
public class Filter {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
|
||||||
|
package cn.ecpark.service.usergw.biz;
|
||||||
|
|
||||||
|
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||||
|
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
import org.springframework.web.reactive.function.server.RequestPredicates;
|
||||||
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||||
|
import org.springframework.web.reactive.function.server.RouterFunctions;
|
||||||
|
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Route
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@Component
|
||||||
|
public class Route {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RouterFunction<ServerResponse> Predicates() {
|
||||||
|
RouterFunction<ServerResponse> route = RouterFunctions.route(
|
||||||
|
RequestPredicates.path("/test/hello"),
|
||||||
|
request -> ServerResponse.ok().body(BodyInserters.fromObject("hello")));
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
||||||
|
return builder.routes()
|
||||||
|
.route("path_route", r -> r.path("/about")
|
||||||
|
.uri("http://ityouknow.com"))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package cn.ecpark.service.usergw.config;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.bouncycastle.cert.crmf.RegTokenControl;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||||
|
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||||
|
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.util.UriBuilder;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ConfigGateway
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
|
|
||||||
|
List<MediaType> mediaTypes = new ArrayList<>();
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||||
|
|
||||||
|
NettyWriteResponseFilter nwrf = applicationContext.getBean(NettyWriteResponseFilter.class);
|
||||||
|
|
||||||
|
Object inputStream = null;
|
||||||
|
String[] gatewayConfigPathList = { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
|
||||||
|
for (String gatewayConfigPath : gatewayConfigPathList) {
|
||||||
|
inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
|
||||||
|
if(inputStream != null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inputStream != null) {
|
||||||
|
Map<String, Object> configYaml = new Yaml().load((InputStream) inputStream);
|
||||||
|
if (configYaml != null) {
|
||||||
|
Map<String, Object> configDefault = (Map<String, Object>) configYaml.get("default");
|
||||||
|
Map<String, Object> configDubbo = (Map<String, Object>) configYaml.get("dubbo");
|
||||||
|
return Flux.fromIterable(ConfigProperties.getConfigDefault(configDefault));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
// return Flux.fromIterable(it);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,121 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2013-2019 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package cn.ecpark.service.usergw.config;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.cloud.gateway.filter.FilterDefinition;
|
||||||
|
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ConfigProperties
|
||||||
|
*/
|
||||||
|
@Validated
|
||||||
|
@Slf4j
|
||||||
|
public class ConfigProperties {
|
||||||
|
|
||||||
|
public static List<RouteDefinition> getConfigDefault(Map<String, Object> configDefault) {
|
||||||
|
if (configDefault != null) {
|
||||||
|
List<RouteDefinition> RDList = new ArrayList<>();
|
||||||
|
|
||||||
|
List<FilterDefinition> filters = new ArrayList<>();
|
||||||
|
List<PredicateDefinition> predicates = new ArrayList<>();
|
||||||
|
|
||||||
|
// default-filters: 下的相关设置
|
||||||
|
Object unknownDefaultFilters = configDefault.get("default-filters");
|
||||||
|
if (unknownDefaultFilters != null) {
|
||||||
|
List<String> defaultFiltersYaml = (ArrayList<String>) unknownDefaultFilters;
|
||||||
|
for (String filterString : defaultFiltersYaml) {
|
||||||
|
filters.add(new FilterDefinition(filterString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object unknownRoutes = configDefault.get("routes");
|
||||||
|
if (unknownRoutes != null) {
|
||||||
|
List<LinkedHashMap> routes = (ArrayList<LinkedHashMap>) unknownRoutes;
|
||||||
|
for (LinkedHashMap iter : routes) {
|
||||||
|
RouteDefinition rd = new RouteDefinition();
|
||||||
|
// 设置id
|
||||||
|
Object id = iter.get("id");
|
||||||
|
if (id != null) {
|
||||||
|
rd.setId((String) id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置uri
|
||||||
|
Object uri = iter.get("uri");
|
||||||
|
if (uri != null) {
|
||||||
|
rd.setUri(URI.create((String) uri));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置uri
|
||||||
|
Object order = iter.get("order");
|
||||||
|
if (order != null) {
|
||||||
|
rd.setOrder((int) order);
|
||||||
|
}
|
||||||
|
|
||||||
|
// predicates: 下的相关属性
|
||||||
|
List<String> unknownPredicatesYaml = (ArrayList<String>) iter.get("predicates");
|
||||||
|
if (unknownPredicatesYaml != null) {
|
||||||
|
List<String> predicatesYaml = (ArrayList<String>) unknownPredicatesYaml;
|
||||||
|
for (String predicateString : predicatesYaml) {
|
||||||
|
predicates.add(new PredicateDefinition(predicateString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// filters: 下的相关属性
|
||||||
|
List<String> unknownFiltersYaml = (ArrayList<String>) iter.get("filters");
|
||||||
|
if (unknownFiltersYaml != null) {
|
||||||
|
List<String> filtersYaml = (ArrayList<String>) unknownFiltersYaml;
|
||||||
|
for (String filterString : filtersYaml) {
|
||||||
|
filters.add(new FilterDefinition(filterString));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rd.setPredicates(predicates);
|
||||||
|
rd.setFilters(filters);
|
||||||
|
|
||||||
|
RDList.add(rd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RDList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package cn.ecpark.service.usergw.impl.rpc;
|
||||||
|
|
||||||
|
import org.apache.dubbo.config.annotation.Service;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import cn.ecpark.service.usergw.api.IDemo;
|
||||||
|
|
||||||
|
@Service(interfaceClass = IDemo.class)
|
||||||
|
@Component
|
||||||
|
public class DemoHello implements IDemo {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String Hello() {
|
||||||
|
return "Hello";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1 +1,7 @@
|
||||||
|
|
||||||
|
spring.application.name=gateway
|
||||||
|
dubbo.scan.base-packages=cn.ecpark.service.usergw.impl
|
||||||
|
dubbo.protocol.name=dubbo
|
||||||
|
dubbo.protocol.port=20080
|
||||||
|
dubbo.registry.address=zookeeper://127.0.0.1:2181
|
||||||
|
server.port=8888
|
||||||
|
|
12
usergw-service/src/main/resources/gateway.yaml
Normal file
12
usergw-service/src/main/resources/gateway.yaml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
default:
|
||||||
|
default-filters:
|
||||||
|
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
|
||||||
|
routes:
|
||||||
|
|
||||||
|
- id: path_route
|
||||||
|
uri: http://httpbin.org:80/get
|
||||||
|
order: 12
|
||||||
|
predicates:
|
||||||
|
# - Path=/get
|
||||||
|
- Header=XX, \d+
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
package cn.ecpark.service.usergw;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
public class DemoApplicationTests {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void contextLoads() {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user