TODO: 解决要完成的filters的顺序问题
This commit is contained in:
parent
d62a9aee6f
commit
68875c7e34
|
@ -91,7 +91,33 @@
|
|||
<plugin>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</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>
|
||||
</build>
|
||||
|
||||
|
||||
|
||||
|
||||
</project>
|
|
@ -12,7 +12,10 @@ import org.apache.dubbo.rpc.service.GenericService;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
|
||||
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.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
|
@ -90,31 +93,21 @@ public class DubboGatewayFilterFactory extends AbstractGatewayFilterFactory<Dubb
|
|||
Arrays.copyOf(paramTypes.toArray(), paramTypes.size(), String[].class),
|
||||
params.toArray());
|
||||
}
|
||||
|
||||
|
||||
if (result == null) {
|
||||
return response.setComplete();
|
||||
}
|
||||
|
||||
if (result.getClass() == String.class) {
|
||||
return response.writeWith(Mono.just(
|
||||
response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
||||
} 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);
|
||||
return response.writeWith(Mono.just(response.bufferFactory()
|
||||
.wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
if (result.getClass() == String.class) {
|
||||
return response.writeWith(
|
||||
Mono.just(response.bufferFactory().wrap(ByteBuffer.wrap(((String) result).getBytes()))));
|
||||
} else {
|
||||
return response.writeWith(Mono.just(
|
||||
response.bufferFactory().wrap(ByteBuffer.wrap(JSON.toJSONString(result).getBytes()))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -284,8 +284,11 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
|||
Object order = iter.get("order");
|
||||
if (order != null) {
|
||||
rd.setOrder((int)order);
|
||||
}
|
||||
iter.remove("order");
|
||||
} else {
|
||||
rd.setOrder(0);
|
||||
}
|
||||
|
||||
|
||||
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
|
||||
reference.setConnections(3);
|
||||
|
|
|
@ -49,3 +49,4 @@ dubbo:
|
|||
predicates:
|
||||
- Path=/dubbo/hello
|
||||
|
||||
|
63
usergw-service/src/main/resources/test-gateway2.yaml
Normal file
63
usergw-service/src/main/resources/test-gateway2.yaml
Normal 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
|
||||
|
||||
|
|
@ -20,7 +20,6 @@ import reactor.netty.http.client.HttpClientResponse;
|
|||
@SpringBootTest(classes= App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(locations = {"application.properties"})
|
||||
|
||||
public class TestHttp2DubboConfig {
|
||||
|
||||
@LocalServerPort
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -8,10 +8,8 @@ import java.util.Map;
|
|||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.test.context.TestComponent;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
@TestComponent
|
||||
public class TestYaml {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
|
|
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user