diff --git a/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json b/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json index 59ed6c5..99e2dc0 100644 --- a/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json +++ b/usergw-service/src/main/java/META-INF/additional-spring-configuration-metadata.json @@ -1,5 +1,7 @@ -{"properties": [{ - "name": "yame.config", - "type": "java.lang.String", - "description": "用于配置网关的路径, 如果不填.默认: gateway.yml, gateway.yaml, Gateway.yml, Gateway.yaml" -}]} \ No newline at end of file +{"properties": [ + { + "name": "yame.gateway.config", + "type": "java.lang.String", + "description": "用于配置网关的路径, 如果不填.默认: gateway.yml, gateway.yaml, Gateway.yml, Gateway.yaml" + } +]} \ No newline at end of file diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java index f164f28..e860b4c 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/events/ConfigRefresh.java @@ -14,10 +14,9 @@ public class ConfigRefresh implements ApplicationEventPublisherAware { public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.publisher = applicationEventPublisher; } - + public void notifyChanged() { this.publisher.publishEvent(new RefreshRoutesEvent(this)); } - } \ 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 index 4b92ea8..16935b1 100644 --- 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 @@ -1,9 +1,14 @@ package cn.ecpark.service.usergw.config; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.Field; import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -30,6 +35,7 @@ import org.springframework.cloud.gateway.route.RouteDefinitionLocator; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Component; +import org.springframework.util.ResourceUtils; import org.yaml.snakeyaml.Yaml; import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool; @@ -45,8 +51,8 @@ public class ConfigGateway implements RouteDefinitionLocator { // List mediaTypes = new ArrayList<>(); private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigGateway.class); - @Value("${yame.config:}") - private String yameConfigPath; + @Value("${yame.gateway.config:}") + private String gatewayConfigPath; @@ -130,21 +136,24 @@ public class ConfigGateway implements RouteDefinitionLocator { } private InputStream loadLocalFileConfig() { - Object inputStream = null; + InputStream inputStream = null; String[] gatewayConfigPathList; - if (yameConfigPath.equals("")) { + if (gatewayConfigPath.equals("")) { gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" }; } else { - gatewayConfigPathList = new String[] { yameConfigPath }; + gatewayConfigPathList = new String[] { gatewayConfigPath }; } + for (String gatewayConfigPath : gatewayConfigPathList) { gatewayConfigPath = gatewayConfigPath.trim(); inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath); - if (inputStream != null) { - break; + if(inputStream != null) { + return inputStream; } } - return (InputStream) inputStream; + + log.error("can't find the config of gateway, like gateway.yaml"); + return inputStream; } diff --git a/usergw-service/src/main/resources/application.properties b/usergw-service/src/main/resources/application.properties index aa0ee59..b37bb8e 100644 --- a/usergw-service/src/main/resources/application.properties +++ b/usergw-service/src/main/resources/application.properties @@ -1,8 +1,8 @@ spring.application.name=gateway -app.id=gateway -apollo.meta=http://localhost:8180 +# app.id=gateway +# apollo.meta=http://localhost:8180 # local.meta=http://localhost:8180 # dev.meta=http://localhost:8180 # fat.meta=http://localhost:8180 @@ -22,7 +22,7 @@ server.port=8888 # logging.level.org.springframework.cloud.gateway=debug logging.file=logs/log -yame.config=gateway.yaml +yame.gateway.config=gateway.yaml diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java index 609f416..ba0b099 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java @@ -6,6 +6,7 @@ import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.PropertySource; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -15,10 +16,9 @@ import reactor.netty.http.client.HttpClientResponse; -// TODO: 添加附加Dubbo Service for Test启动进程 @SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT) @RunWith(SpringRunner.class) -@TestPropertySource(locations = "test.properties", properties = {"apollo.meta=", "app.id="}) +@TestPropertySource(locations = "classpath:/resources/test.properties", properties = {"apollo.meta=", "app.id="}) public class TestHttp2DubboConfig { @LocalServerPort diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml b/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml deleted file mode 100644 index 6ee7e1b..0000000 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/test-gateway.yaml +++ /dev/null @@ -1,51 +0,0 @@ -default-filters: - - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar - -restful: - routes: - - id: path_route - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - -dubbo: - routes: - - id: test - order: 0 - application: dubbo-exchange - # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 - - # - name: Say - # param-types: - # - java.lang.String - # - name: Hello - - connections: 4 - group: test - # registry: zookeeper://127.0.0.1:2181 - interface: ocean.demo.api.IExchange - version: 1.0.0 - predicates: - - Path=/dubbo/hello - - - id: test2 - order: 0 - application: dubbo-exchange - # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 - - # - name: Say - # param-types: - # - java.lang.String - # - name: Hello - - connections: 4 - group: test - # registry: zookeeper://127.0.0.1:2181 - interface: ocean.demo.api.IExchange - version: 1.0.0 - predicates: - - Path=/dubbo/hello - - \ No newline at end of file diff --git a/usergw-service/src/test/java/resources/application.properties b/usergw-service/src/test/java/resources/application.properties deleted file mode 100644 index 71b9ee3..0000000 --- a/usergw-service/src/test/java/resources/application.properties +++ /dev/null @@ -1,13 +0,0 @@ - -spring.application.name=gateway -dubbo.scan.base-packages=cn.ecpark.service.usergw.impl -dubbo.protocol.name=dubbo -dubbo.protocol.port=20999 -dubbo.registry.address=zookeeper://127.0.0.1:2181 -dubbo.config-center.address=zookeeper://127.0.0.1:2181 -dubbo.metadata-report.address=zookeeper://127.0.0.1:2181 -server.port=8888 - -# logging.level.org.springframework.cloud.gateway=debug -logging.file=logs/log - diff --git a/usergw-service/src/test/java/resources/gateway.yaml b/usergw-service/src/test/java/resources/gateway.yaml deleted file mode 100644 index 7e9c5b4..0000000 --- a/usergw-service/src/test/java/resources/gateway.yaml +++ /dev/null @@ -1,51 +0,0 @@ -default-filters: - - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar - -restful: - routes: - - id: path_route - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - - - id: path_route12; - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - - - id: path_route13 - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - # - id: redirect_to - # uri: http://localhost/test/** - # order: 11 - # filters: - # - RedirectTo=302, http://httpbin.org:80/get - -dubbo: - routes: - - id: test - order: 0 - application: dubbo-exchange - # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 - - # - name: Say - # param-types: - # - java.lang.String - # - name: Hello - - connections: 4 - group: test - # registry: zookeeper://127.0.0.1:2181 - interface: ocean.demo.api.IExchange - version: 1.0.0 - predicates: - - Path=/dubbo/hello - \ No newline at end of file diff --git a/usergw-service/src/test/java/resources/test-gateway.yaml b/usergw-service/src/test/java/resources/test-gateway.yaml index 39bd62a..6ee7e1b 100644 --- a/usergw-service/src/test/java/resources/test-gateway.yaml +++ b/usergw-service/src/test/java/resources/test-gateway.yaml @@ -1,44 +1,26 @@ default-filters: - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar - + restful: routes: - id: path_route uri: http://httpbin.org:80/* order: 9 predicates: - - Path=/get - - Header=XX, \d+ - - - id: path_route12; - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - - - id: path_route13 - uri: http://httpbin.org:80/* - order: 9 - predicates: - - Path=/get - - Header=XX, \d+ - # - id: redirect_to - # uri: http://localhost/test/** - # order: 11 - # filters: - # - RedirectTo=302, http://httpbin.org:80/get + - Path=/get + - Header=XX, \d+ dubbo: routes: - id: test order: 0 application: dubbo-exchange - methods: # 如果没填就从 request拿 意味着所有接口都可以使用 - - name: Say - param-types: - - java.lang.String - - name: Hello + # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 + + # - name: Say + # param-types: + # - java.lang.String + # - name: Hello connections: 4 group: test @@ -47,4 +29,23 @@ dubbo: version: 1.0.0 predicates: - Path=/dubbo/hello + + - id: test2 + order: 0 + application: dubbo-exchange + # methods: # 如果没填就从 request拿 意味着所有接口都可以使用 + + # - name: Say + # param-types: + # - java.lang.String + # - name: Hello + + connections: 4 + group: test + # registry: zookeeper://127.0.0.1:2181 + interface: ocean.demo.api.IExchange + version: 1.0.0 + predicates: + - Path=/dubbo/hello + \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties b/usergw-service/src/test/java/resources/test.properties similarity index 91% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties rename to usergw-service/src/test/java/resources/test.properties index 31e134b..905c6b3 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/test.properties +++ b/usergw-service/src/test/java/resources/test.properties @@ -10,6 +10,5 @@ server.port=8888 # logging.level.org.springframework.cloud.gateway=debug logging.file=logs/log - -yame.config=test-gateway.yaml +yame.gateway.config=test-gateway.yaml