TODO: 解决要完成的filters的顺序问题

This commit is contained in:
huangsimin 2019-07-10 18:19:47 +08:00
parent d62a9aee6f
commit 68875c7e34
9 changed files with 190 additions and 34 deletions

View File

@ -52,18 +52,18 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId> <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.yaml</groupId> <groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId> <artifactId>snakeyaml</artifactId>
<version>1.21</version> <version>1.21</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.2.58</version> <version>1.2.58</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.ecpark.service</groupId> <groupId>cn.ecpark.service</groupId>
@ -91,7 +91,33 @@
<plugin> <plugin>
<artifactId>maven-source-plugin</artifactId> <artifactId>maven-source-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

View File

@ -12,7 +12,10 @@ import org.apache.dubbo.rpc.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponse;
@ -65,7 +68,7 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
// 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错 // 判断全部函数允许, 必须带参数类型, 而且要匹配, 否则报错
if (uri.charAt(5) == '-') { if (uri.charAt(5) == '-') {
paramTypes = headers.get("param-types"); paramTypes = headers.get("param-types");
if(paramTypes == null) { if (paramTypes == null) {
paramTypes = this.emptyList; paramTypes = this.emptyList;
} }
} else { } else {
@ -90,31 +93,21 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class), Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class),
params.toArray()); params.toArray());
} }
if (result == null) { if (result == null) {
return response.setComplete(); return response.setComplete();
} }
} else {
result = String.format("paramTypes.size %d is not equals to params size %d", paramTypes.size(),
paramsSize);
log.warn((String) result);
}
} else {
result = String.format("(mehtod: %s, param-types: null) is not exist or method is not allowed",
methodString.get(0));
log.warn((String) result);
}
} else {
result = String.format("queryParams.get(\"method\") is null");
log.warn((String) result);
}
if (result != null) { if (result.getClass() == String.class) {
if (result.getClass() == String.class) { return response.writeWith(Mono.just(
return response.writeWith( response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes())))); } else {
} else { return response.writeWith(Mono.just(response.bufferFactory()
return response.writeWith(Mono.just( .wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes())))); }
}
} }
} }

View File

@ -283,9 +283,12 @@ public class ConfigGateway implements RouteDefinitionLocator {
// 设置uri // 设置uri
Object order = iter.get("order"); Object order = iter.get("order");
if (order != null) { if (order != null) {
rd.setOrder((int) order); rd.setOrder((int)order);
iter.remove("order");
} else {
rd.setOrder(0);
} }
iter.remove("order");
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setConnections(3); reference.setConnections(3);

View File

@ -48,4 +48,5 @@ dubbo:
version: 1.0.0 version: 1.0.0
predicates: predicates:
- Path=/dubbo/hello - Path=/dubbo/hello

View File

@ -0,0 +1,63 @@
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: 10
application: dubbo-exchange
methods: # 如果没填就从 request拿 意味着所有接口都可以使用
- name: Say
param-types:
- java.lang.String
group: test
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
filters:
- SetRequestHeader=params, SetHeader
- id: test2
order: 0
application: dubbo-exchange
methods: # 如果没填就从 request拿 意味着所有接口都可以使用
- name: Hello
group: test
# registry: zookeeper://127.0.0.1:2181
interface: ocean.demo.api.IExchange
version: 1.0.0
predicates:
- Path=/dubbo/hello
filters:
- SetStatus=404

View File

@ -20,7 +20,6 @@ import reactor.netty.http.client.HttpClientResponse;
@SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@TestPropertySource(locations = {"application.properties"}) @TestPropertySource(locations = {"application.properties"})
public class TestHttp2DubboConfig { public class TestHttp2DubboConfig {
@LocalServerPort @LocalServerPort

View File

@ -0,0 +1,58 @@
package cn.ecpark.service.usergw.utils;
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 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;
// TODO: 添加附加Dubbo Service for Test启动进程
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@TestPropertySource(locations = { "application2.properties" })
public class TestHttp2DubboConfig2 {
@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", "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);
content = receiver.responseContent().asString().blockLast();
Assert.assertNull(content);
}
}

View File

@ -8,10 +8,8 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.context.TestComponent;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
@TestComponent
public class TestYaml { public class TestYaml {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test

View File

@ -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-gateway2.yaml