diff --git a/usergw-service/pom.xml b/usergw-service/pom.xml index 02ac82a..34909de 100644 --- a/usergw-service/pom.xml +++ b/usergw-service/pom.xml @@ -71,6 +71,11 @@ ${project.parent.version} + + org.springframework.boot + spring-boot-starter-data-redis-reactive + + @@ -112,12 +117,10 @@ - + - - - + \ No newline at end of file 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 2b240b7..aa6bccc 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 @@ -14,6 +14,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.OrderedGatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory; import org.springframework.context.ApplicationContext; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; @@ -97,7 +98,7 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory { - + ServerHttpRequest req = exchange.getRequest(); HttpHeaders headers = req.getHeaders(); 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 d17afcf..3de6efa 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 @@ -390,16 +390,40 @@ public class ConfigGateway implements RouteDefinitionLocator { private void parseAndAddDubboFilters(String dubboUri, List filters, LinkedHashMap> iter, String yamlField) { List filtersYaml = iter.get(yamlField); - + filters.addAll(defaultFilters); if (filtersYaml != null) { - for (String filterString : filtersYaml) { - FilterDefinition fd = new FilterDefinition(filterString); - log.info(fd.getName()); - if (!fd.getName().equals("Dubbo")) { - filters.add(fd); + for (Object filterObject : filtersYaml) { + // 现阶段只支持两种情况 + if(filterObject.getClass() == String.class) { + String filterString = (String)filterObject; + FilterDefinition fd = new FilterDefinition(filterString); + log.info(fd.getName()); + if (!fd.getName().equals("Dubbo")) { + filters.add(fd); + } + } else if (filterObject.getClass() == LinkedHashMap.class) { + Map filterMap = (LinkedHashMap)filterObject; + FilterDefinition fd = new FilterDefinition(); + + fd.setName((String)filterMap.get("name")); + if(filterMap.containsKey("args")) { + Object args = filterMap.get("args"); + for( Entry kv : ((Map)args).entrySet()) { + if(kv.getValue().getClass() != String.class) { + kv.setValue( String.valueOf(kv.getValue())); + } + } + fd.setArgs((Map)args); + } + + log.info(fd.getName()); + if (!fd.getName().equals("Dubbo")) { + filters.add(fd); + } } + } } diff --git a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java index fc643cb..3fae651 100644 --- a/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java +++ b/usergw-service/src/main/java/cn/ecpark/service/usergw/config/bean/ConfigBean.java @@ -1,10 +1,12 @@ package cn.ecpark.service.usergw.config.bean; +import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import cn.ecpark.service.usergw.biz.filters.bean.GenericServicePool; import cn.ecpark.service.usergw.biz.filters.factory.DubboGatewayFilterFactory; +import reactor.core.publisher.Mono; @Component public class ConfigBean { @@ -17,4 +19,9 @@ public class ConfigBean { public DubboGatewayFilterFactory dubboFilterFactory() { return new DubboGatewayFilterFactory(); } + + @Bean + KeyResolver ipResolver() { + return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString()); + } } \ No newline at end of file diff --git a/usergw-service/src/main/resources/test-gateway4.yaml b/usergw-service/src/main/resources/test-gateway4.yaml new file mode 100644 index 0000000..05cebb7 --- /dev/null +++ b/usergw-service/src/main/resources/test-gateway4.yaml @@ -0,0 +1,58 @@ +default-filters: + - AddResponseHeader=X-Response-Default-Foo, Test-Default-Bar +restful: # filters 必须 有predicates uri只是预期到达的地址 + routes: + - id: haha + uri: http://localhost:4444 + order: 0 + predicates: + - Path=/http/hello + + - id: rewrite + uri: http://localhost:4444 + order: 0 + predicates: + - Path=/http/rewrite + filters: + - RewritePath=/http/rewrite, /http/hello + + - id: xixi + uri: http://localhost:8888 + order: 11 + predicates: + - Path=/http/xixi + filters: + - RedirectTo=302, http://localhost:4444/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 + filters: + - name: RequestRateLimiter + args: + key-resolver: '#{@ipResolver}' #SPEL表达式去的对应的bean + redis-rate-limiter.replenishRate: 81 # 令牌桶的容积 + redis-rate-limiter.burstCapacity: 40 # 流速 每秒 + + # - name: RequestRateLimiter1 + # args: + # redis-rate-limiter.replenishRate: 1 # 令牌桶的容积 + # redis-rate-limiter.burstCapacity: 3 # 流速 每秒 + # key-resolver: "#{@ipResolver}" #SPEL表达式去的对应的bean + + \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java new file mode 100644 index 0000000..4644ada --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/TestHttp2DubboConfig4.java @@ -0,0 +1,62 @@ +package cn.ecpark.service.usergw; + +import java.util.Map; + +import com.alibaba.fastjson.JSON; + +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.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory; +import org.springframework.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory; +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 = { "application4.properties" }) +public class TestHttp2DubboConfig4 { + + @LocalServerPort + private int serverPort; + + @Test + public void Test1RequestHttp2DubboSetHeader() { + + // Test Base Url + for(int i = 0; i < 2;i++) { + 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 + @SuppressWarnings("unchecked") + public void Test3RestfulFilters() { + + + } + + +} \ No newline at end of file diff --git a/usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties b/usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties new file mode 100644 index 0000000..fa63143 --- /dev/null +++ b/usergw-service/src/test/java/cn/ecpark/service/usergw/application4.properties @@ -0,0 +1,21 @@ + +spring.redis.host=localhost +spring.redis.port=8379 +spring.redis.database: 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 + +yame.config=test-gateway4.yaml +