From f6051019e67698e8893e20106896cf88082f03d8 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 24 Jun 2019 18:24:16 +0800 Subject: [PATCH] TODO: Load MediaType --- pom.xml | 76 ++++++++--- usergw-api/pom.xml | 22 +--- .../service/usergw/DemoApplication.java | 13 -- .../cn/ecpark/service/usergw/api/IDemo.java | 5 + .../src/main/resources/application.properties | 1 - .../service/usergw/DemoApplicationTests.java | 16 --- usergw-service/pom.xml | 92 +++++++++---- .../java/cn/ecpark/service/usergw/App.java | 24 ++++ .../service/usergw/DemoApplication.java | 13 -- .../cn/ecpark/service/usergw/biz/Filter.java | 12 ++ .../cn/ecpark/service/usergw/biz/Route.java | 39 ++++++ .../service/usergw/config/ConfigGateway.java | 67 ++++++++++ .../usergw/config/ConfigProperties.java | 121 ++++++++++++++++++ .../service/usergw/impl/rpc/DemoHello.java | 17 +++ .../src/main/resources/application.properties | 6 + .../src/main/resources/gateway.yaml | 12 ++ .../service/usergw/DemoApplicationTests.java | 16 --- 17 files changed, 433 insertions(+), 119 deletions(-) delete mode 100644 usergw-api/src/main/java/cn/ecpark/service/usergw/DemoApplication.java create mode 100644 usergw-api/src/main/java/cn/ecpark/service/usergw/api/IDemo.java delete mode 100644 usergw-api/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/App.java delete mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/DemoApplication.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Filter.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java create mode 100644 usergw-service/src/main/java/cn/ecpark/service/usergw/impl/rpc/DemoHello.java create mode 100644 usergw-service/src/main/resources/gateway.yaml delete mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java diff --git a/pom.xml b/pom.xml index e18ff09..4c6e50a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,35 +5,37 @@ org.springframework.boot spring-boot-starter-parent - 2.1.5.RELEASE + 2.1.6.RELEASE cn.ecpark.service - gateway-parent + usergw-parent 0.0.1-SNAPSHOT - gateway + usergw-parent pom - Demo project for Spring Boot + Demo project for Yame User Gateway 1.8 UTF-8 + UTF-8 Greenwich.SR1 + 2.7.1 - + org.springframework.boot - spring-boot-starter + spring-boot-starter-webflux org.springframework.boot - spring-boot-starter-web + spring-boot-starter @@ -42,22 +44,58 @@ test + + org.apache.dubbo + dubbo-spring-boot-starter + ${dubbo.version} + + + + org.apache.dubbo + dubbo + ${dubbo.version} + + + + org.apache.dubbo + dubbo-dependencies-zookeeper + ${dubbo.version} + pom + + + org.slf4j + slf4j-log4j12 + + + log4j + log4j + + + + + + org.projectlombok + lombok + true + + - + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - usergw-service - usergw-api + usergw-api + usergw-service \ No newline at end of file diff --git a/usergw-api/pom.xml b/usergw-api/pom.xml index 398c87d..1302965 100644 --- a/usergw-api/pom.xml +++ b/usergw-api/pom.xml @@ -4,15 +4,18 @@ 4.0.0 cn.ecpark.service - gateway-parent + usergw-parent 0.0.1-SNAPSHOT ../pom.xml + cn.ecpark.service usergw-api ${project.parent.version} + jar + demo-api - Demo project for Spring Boot + Demo project for Gateway API 1.8 @@ -24,24 +27,11 @@ - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - + - - org.springframework.boot - spring-boot-maven-plugin - - diff --git a/usergw-api/src/main/java/cn/ecpark/service/usergw/DemoApplication.java b/usergw-api/src/main/java/cn/ecpark/service/usergw/DemoApplication.java deleted file mode 100644 index 984a0cb..0000000 --- a/usergw-api/src/main/java/cn/ecpark/service/usergw/DemoApplication.java +++ /dev/null @@ -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); - } - -} diff --git a/usergw-api/src/main/java/cn/ecpark/service/usergw/api/IDemo.java b/usergw-api/src/main/java/cn/ecpark/service/usergw/api/IDemo.java new file mode 100644 index 0000000..1c0d9e9 --- /dev/null +++ b/usergw-api/src/main/java/cn/ecpark/service/usergw/api/IDemo.java @@ -0,0 +1,5 @@ +package cn.ecpark.service.usergw.api; + +public interface IDemo { + String Hello(); +} \ No newline at end of file diff --git a/usergw-api/src/main/resources/application.properties b/usergw-api/src/main/resources/application.properties index c824b5d..e69de29 100644 --- a/usergw-api/src/main/resources/application.properties +++ b/usergw-api/src/main/resources/application.properties @@ -1 +0,0 @@ -server.port= 8888 \ No newline at end of file diff --git a/usergw-api/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java b/usergw-api/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java deleted file mode 100644 index 54f919e..0000000 --- a/usergw-api/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java +++ /dev/null @@ -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() { - } - -} diff --git a/usergw-service/pom.xml b/usergw-service/pom.xml index dc348a3..0317901 100644 --- a/usergw-service/pom.xml +++ b/usergw-service/pom.xml @@ -1,49 +1,91 @@ - - + + 4.0.0 - + cn.ecpark.service - gateway-parent + usergw-parent 0.0.1-SNAPSHOT - ../pom.xml + ../pom.xml cn.ecpark.service usergw ${project.parent.version} demo - Demo project for Spring Boot - + Demo project for Gateway Service + 1.8 - + + + cn.ecpark.service + usergw-api + ${project.parent.version} + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + - + + org.yaml + snakeyaml + 1.21 + + + + cn.ecpark.service + usergw-api + ${project.parent.version} + + - - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - - + + + org.springframework.boot spring-boot-maven-plugin + + + maven-compiler-plugin + + + maven-resources-plugin + + + maven-source-plugin + - - + + \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java new file mode 100644 index 0000000..457898b --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/App.java @@ -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); + } + +} diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/DemoApplication.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/DemoApplication.java deleted file mode 100644 index 984a0cb..0000000 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/DemoApplication.java +++ /dev/null @@ -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); - } - -} diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Filter.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Filter.java new file mode 100644 index 0000000..061197e --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Filter.java @@ -0,0 +1,12 @@ + + + +package cn.ecpark.service.usergw.biz; + +/** + * Filter + */ +public class Filter { + + +} \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java new file mode 100644 index 0000000..dee56f0 --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/Route.java @@ -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 Predicates() { + RouterFunction 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(); + } + +} \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java new file mode 100644 index 0000000..50ae696 --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigGateway.java @@ -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 mediaTypes = new ArrayList<>(); + + @Autowired + private ApplicationContext applicationContext; + + @Override + public Flux 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 configYaml = new Yaml().load((InputStream) inputStream); + if (configYaml != null) { + Map configDefault = (Map) configYaml.get("default"); + Map configDubbo = (Map) configYaml.get("dubbo"); + return Flux.fromIterable(ConfigProperties.getConfigDefault(configDefault)); + } + } + return null; + // return Flux.fromIterable(it); + } +} \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java new file mode 100644 index 0000000..6bbf58e --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/ConfigProperties.java @@ -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 getConfigDefault(Map configDefault) { + if (configDefault != null) { + List RDList = new ArrayList<>(); + + List filters = new ArrayList<>(); + List predicates = new ArrayList<>(); + + // default-filters: 下的相关设置 + Object unknownDefaultFilters = configDefault.get("default-filters"); + if (unknownDefaultFilters != null) { + List defaultFiltersYaml = (ArrayList) unknownDefaultFilters; + for (String filterString : defaultFiltersYaml) { + filters.add(new FilterDefinition(filterString)); + } + } + + Object unknownRoutes = configDefault.get("routes"); + if (unknownRoutes != null) { + List routes = (ArrayList) 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 unknownPredicatesYaml = (ArrayList) iter.get("predicates"); + if (unknownPredicatesYaml != null) { + List predicatesYaml = (ArrayList) unknownPredicatesYaml; + for (String predicateString : predicatesYaml) { + predicates.add(new PredicateDefinition(predicateString)); + } + } + + // filters: 下的相关属性 + List unknownFiltersYaml = (ArrayList) iter.get("filters"); + if (unknownFiltersYaml != null) { + List filtersYaml = (ArrayList) unknownFiltersYaml; + for (String filterString : filtersYaml) { + filters.add(new FilterDefinition(filterString)); + } + } + + rd.setPredicates(predicates); + rd.setFilters(filters); + + RDList.add(rd); + } + + return RDList; + } + } + + return null; + } + +} diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/impl/rpc/DemoHello.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/impl/rpc/DemoHello.java new file mode 100644 index 0000000..2b32a97 --- /dev/null +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/impl/rpc/DemoHello.java @@ -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"; + } + +} \ No newline at end of file diff --git a/usergw-service/src/main/resources/application.properties b/usergw-service/src/main/resources/application.properties index 8b13789..a306709 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -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 diff --git a/usergw-service/src/main/resources/gateway.yaml b/usergw-service/src/main/resources/gateway.yaml new file mode 100644 index 0000000..5f41fb6 --- /dev/null +++ b/usergw-service/src/main/resources/gateway.yaml @@ -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+ + \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java deleted file mode 100644 index 54f919e..0000000 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/DemoApplicationTests.java +++ /dev/null @@ -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() { - } - -}