From 6037e1a58ab7dd0cfc91292a7871ba88b038fcdd Mon Sep 17 00:00:00 2001 From: huangsimin Date: Tue, 16 Jul 2019 18:50:27 +0800 Subject: [PATCH] =?UTF-8?q?restful=20routes=20predicates=20filters=20?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=E6=B5=8B=E8=AF=95.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../factory/DubboGatewayFilterFactory.java | 27 ++++---- .../service/usergw/config/ConfigGateway.java | 2 - .../ecpark/service/usergw/utils/Extract.java | 4 -- .../src/main/resources/test-gateway2.yaml | 2 +- .../src/main/resources/test-gateway3.yaml | 33 ++++++++++ .../{utils => }/TestHttp2DubboConfig.java | 4 +- .../{utils => }/TestHttp2DubboConfig2.java | 28 ++------ .../service/usergw/TestHttp2DubboConfig3.java | 64 +++++++++++++++++++ .../usergw/{utils => }/application.properties | 0 .../{utils => }/application2.properties | 0 .../service/usergw/application3.properties | 15 +++++ .../service/usergw/config/ParseConfig.java | 10 +++ .../service/usergw/utils/TestExtract.java | 23 ++++++- 13 files changed, 164 insertions(+), 48 deletions(-) create mode 100644 usergw-service/src/main/resources/test-gateway3.yaml rename usergw-service/src/test/java/cn/ecpark/service/usergw/{utils => }/TestHttp2DubboConfig.java (95%) rename usergw-service/src/test/java/cn/ecpark/service/usergw/{utils => }/TestHttp2DubboConfig2.java (57%) create mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java rename usergw-service/src/test/java/cn/ecpark/service/usergw/{utils => }/application.properties (100%) rename usergw-service/src/test/java/cn/ecpark/service/usergw/{utils => }/application2.properties (100%) create mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties create mode 100644 usergw-service/src/test/java/cn/ecpark/service/usergw/config/ParseConfig.java diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java index a0ae60a..c6c9f66 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/biz/filters/factory/DubboGatewayFilterFactory.java @@ -151,26 +151,27 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory filtersYaml = iter.get(yamlField); if (filtersYaml != null) { - filters.addAll(defaultFilters); - for (String filterString : filtersYaml) { filters.add(new FilterDefinition(filterString)); } diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Extract.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Extract.java index 2d43e77..766e0af 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Extract.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/utils/Extract.java @@ -7,10 +7,6 @@ import org.apache.dubbo.rpc.service.GenericService; public class Extract { public static String getReferenceConfigKey(ReferenceConfig referenceConfig) { String iName = referenceConfig.getInterface(); - if (StringUtils.isBlank(iName)) { - Class clazz = referenceConfig.getInterfaceClass(); - iName = clazz.getName(); - } if (StringUtils.isBlank(iName)) { throw new IllegalArgumentException("No interface info in ReferenceConfig" + referenceConfig); } diff --git a/usergw-service/src/main/resources/test-gateway2.yaml b/usergw-service/src/main/resources/test-gateway2.yaml index 80fc7ed..04ee577 100644 --- a/usergw-service/src/main/resources/test-gateway2.yaml +++ b/usergw-service/src/main/resources/test-gateway2.yaml @@ -50,7 +50,7 @@ dubbo: interface: ocean.demo.api.IExchange version: 1.0.0 predicates: - - Path=/dubbo/hello + - Path=/dubbo/say filters: - SetRequestHeader=params, SetHeader diff --git a/usergw-service/src/main/resources/test-gateway3.yaml b/usergw-service/src/main/resources/test-gateway3.yaml new file mode 100644 index 0000000..eef4623 --- /dev/null +++ b/usergw-service/src/main/resources/test-gateway3.yaml @@ -0,0 +1,33 @@ +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar + +restful: # filters 必须 有predicates uri只是预期到达的地址 + routes: + - id: redirect_to + uri: http://localhost:8888/http/xixi + order: 11 + predicates: + - Path=http://localhost:8888/http/xixi + filters: + - RedirectTo=302, http://localhost:8888/http/hello + +dubbo: + routes: + - id: test + 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/utils/TestHttp2DubboConfig.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java similarity index 95% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestHttp2DubboConfig.java rename to usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java index cc67b92..815b688 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestHttp2DubboConfig.java +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig.java @@ -1,4 +1,4 @@ -package cn.ecpark.service.usergw.utils; +package cn.ecpark.service.usergw; import org.junit.Assert; import org.junit.Test; @@ -6,11 +6,9 @@ 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.cloud.gateway.handler.FilteringWebHandler; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import cn.ecpark.service.usergw.App; import reactor.netty.http.client.HttpClient; import reactor.netty.http.client.HttpClient.ResponseReceiver; import reactor.netty.http.client.HttpClientResponse; diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestHttp2DubboConfig2.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java similarity index 57% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestHttp2DubboConfig2.java rename to usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java index e9da23d..5c3e37c 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestHttp2DubboConfig2.java +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig2.java @@ -1,4 +1,4 @@ -package cn.ecpark.service.usergw.utils; +package cn.ecpark.service.usergw; import org.junit.Assert; import org.junit.Test; @@ -6,13 +6,9 @@ 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.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory; -import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; -import cn.ecpark.service.usergw.App; -import reactor.core.publisher.Mono; import reactor.netty.http.client.HttpClient; import reactor.netty.http.client.HttpClient.ResponseReceiver; import reactor.netty.http.client.HttpClientResponse; @@ -33,30 +29,16 @@ public class TestHttp2DubboConfig2 { HttpClient client = HttpClient.create(); ResponseReceiver receiver; String content; - // receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello").headers( - // h -> h.set("method", "Say").add("param-types", "java.lang.String").add("params", "Test-MyHttp2dubbo")) - // .get(); - - // HttpClientResponse resp = receiver.response().block(); - // Assert.assertNotNull(resp); - - // content = receiver.responseContent().asString().blockLast(); - // Assert.assertEquals(resp.status().code(), 200); - // Assert.assertNotNull(content); - // Assert.assertEquals(content, "SetHeader"); receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello") .headers(h -> h.set("method", "Hello")).get(); + HttpClientResponse response = receiver.response().block(); - Assert.assertNotNull(response); - // Assert.assertEquals(response.status().code(), 404); - - int code = response.status().code(); + Assert.assertEquals(response.status().code(), 404); content = receiver.responseContent().asString().blockLast(); - Assert.assertNull(content); - - + Assert.assertNotNull(content); + Assert.assertEquals(content, "Not Found"); } diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java new file mode 100644 index 0000000..50ea3dc --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig3.java @@ -0,0 +1,64 @@ +package cn.ecpark.service.usergw; + +import org.junit.Assert; +import org.junit.Test; +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.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.client.HttpClient.ResponseReceiver; +import reactor.netty.http.client.HttpClientResponse; + +// TODO: 添加附加Dubbo Service for Test启动进程 +@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT) +@RunWith(SpringRunner.class) +@TestPropertySource(locations = { "application3.properties" }) +public class TestHttp2DubboConfig3 { + + @LocalServerPort + private int serverPort; + + @Test + public void Test2RequestHttp2DubboSetHeader() { + + // Test Base Url + HttpClient client = HttpClient.create(); + ResponseReceiver receiver; + String content; + + receiver = client.baseUrl("http://localhost:" + serverPort + "/dubbo/hello") + .headers(h -> h.set("method", "Hello")).get(); + + HttpClientResponse response = receiver.response().block(); + Assert.assertNotNull(response); + Assert.assertEquals(response.status().code(), 200); + content = receiver.responseContent().asString().blockLast(); + Assert.assertNotNull(content); + Assert.assertEquals(content, "Hello Dubbo"); + } + + + @Test + public void Test3RestfulFilters() { + + // Test Base Url + HttpClient client = HttpClient.create(); + ResponseReceiver receiver; + String content; + + receiver = client.baseUrl("http://localhost:" + serverPort + "/http/xixi").get(); + + HttpClientResponse response = receiver.response().block(); + Assert.assertNotNull(response); + Assert.assertEquals(response.status().code(), 302); + content = receiver.responseContent().asString().blockLast(); + Assert.assertNotNull(content); + Assert.assertEquals(content, "Hello Dubbo"); + } + + +} \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/application.properties b/usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties similarity index 100% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/utils/application.properties rename to usergw-service/src/test/java/cn/ecpark/service/usergw/application.properties diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/application2.properties b/usergw-service/src/test/java/cn/ecpark/service/usergw/application2.properties similarity index 100% rename from usergw-service/src/test/java/cn/ecpark/service/usergw/utils/application2.properties rename to usergw-service/src/test/java/cn/ecpark/service/usergw/application2.properties diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties b/usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties new file mode 100644 index 0000000..1dcad2f --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/application3.properties @@ -0,0 +1,15 @@ + +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 + +yame.config=test-gateway3.yaml + diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/config/ParseConfig.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/config/ParseConfig.java new file mode 100644 index 0000000..acc8fda --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/config/ParseConfig.java @@ -0,0 +1,10 @@ +package cn.ecpark.service.usergw.config; + +import org.junit.Test; + +public class ParseConfig { + @Test + public void My() { + + } +} \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestExtract.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestExtract.java index 60f7b2e..f63571e 100644 --- a/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestExtract.java +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/utils/TestExtract.java @@ -1,12 +1,17 @@ package cn.ecpark.service.usergw.utils; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.rpc.service.GenericService; import org.junit.Assert; import org.junit.Test; import org.springframework.boot.test.context.TestComponent; import cn.ecpark.service.usergw.utils.Extract.GenericServiceBase; -@TestComponent + public class TestExtract { @Test @@ -27,7 +32,21 @@ public class TestExtract { } @Test - public void TestUriQueryList() { + public void TestFirst2Case() { + String content = Convert.firstUpperCase("str"); + Assert.assertEquals(content, "Str"); + } + + @Test + public void TestReferenceConfigKey() { + ReferenceConfig ref = new ReferenceConfig(); + try { + Extract.getReferenceConfigKey(ref); + Assert.assertTrue(false); + } catch (Exception e) { + String m = e.getMessage(); + assertTrue(m.startsWith("No interface info in ReferenceConfig")); + } } } \ No newline at end of file