Skip to content

Commit 2ac53f8

Browse files
committed
Change contract of Config for JsonToGrpc filter
1 parent f956df3 commit 2ac53f8

File tree

5 files changed

+33
-51
lines changed

5 files changed

+33
-51
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway-server-webflux/gatewayfilter-factories/jsontogrpc-factory.adoc

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The JSONToGRPC GatewayFilter Factory converts a JSON payload to a gRPC request.
55

66
The filter takes the following arguments:
77

8+
* `service`: Short name of the service that handles the request.
9+
10+
* `method`: Method name in the service that handles the request.
11+
812
* `protoDescriptor`: Proto descriptor file.
913

1014
This file can be generated using `protoc` and specifying the `--descriptor_set_out` flag:
@@ -16,12 +20,6 @@ protoc --proto_path=src/main/resources/proto/ \
1620
src/main/resources/proto/hello.proto
1721
----
1822

19-
* `protoFile`: Proto definition file.
20-
21-
* `service`: Short name of the service that handles the request.
22-
23-
* `method`: Method name in the service that handles the request.
24-
2523
NOTE: `streaming` is not supported.
2624

2725

@@ -33,11 +31,10 @@ NOTE: `streaming` is not supported.
3331
public RouteLocator routes(RouteLocatorBuilder builder) {
3432
return builder.routes()
3533
.route("json-grpc", r -> r.path("/json/hello").filters(f -> {
36-
String protoDescriptor = "file:src/main/proto/hello.pb";
37-
String protoFile = "file:src/main/proto/hello.proto";
3834
String service = "HelloService";
3935
String method = "hello";
40-
return f.jsonToGRPC(protoDescriptor, protoFile, service, method);
36+
String protoDescriptor = "file:src/main/proto/hello.pb";
37+
return f.jsonToGRPC(service, method, protoDescriptor);
4138
}).uri(uri))
4239
----
4340

@@ -48,17 +45,15 @@ spring:
4845
gateway:
4946
routes:
5047
- id: json-grpc
51-
uri: https://localhost:6565/testhello
48+
uri: https://localhost:6565
5249
predicates:
5350
- Path=/json/**
5451
filters:
5552
- name: JsonToGrpc
5653
args:
57-
protoDescriptor: file:proto/hello.pb
58-
protoFile: file:proto/hello.proto
5954
service: HelloService
6055
method: hello
61-
56+
protoDescriptor: file:proto/hello.pb
6257
----
6358

6459
When a request is made through the gateway to `/json/hello`, the request is transformed by using the definition provided in `hello.proto`, sent to `HelloService/hello`, and the response back is transformed to JSON.

spring-cloud-gateway-integration-tests/grpc/src/test/java/org/springframework/cloud/gateway/tests/grpc/JsonToGrpcApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void shouldConvertFromJSONToGRPC() {
6868
final RouteConfigurer configurer = new RouteConfigurer(gatewayPort);
6969
int grpcServerPort = gatewayPort + 1;
7070
configurer.addRoute(grpcServerPort, "/json/hello",
71-
"JsonToGrpc=file:src/main/proto/hello.pb,none:,HelloService,hello");
71+
"JsonToGrpc=HelloService,hello,file:src/main/proto/hello.pb");
7272

7373
String response = restTemplate
7474
.postForEntity("https://localhost:" + this.gatewayPort + "/json/hello",

spring-cloud-gateway-integration-tests/grpc/src/test/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ server:
1010
management:
1111
endpoint:
1212
health:
13-
show-details: when_authorized
13+
show-details: when-authorized
1414
gateway:
1515
enabled: true
1616
endpoints:

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/JsonToGrpcGatewayFilterFactory.java

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package org.springframework.cloud.gateway.filter.factory;
1818

19+
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
20+
1921
import java.io.IOException;
20-
import java.io.InputStream;
2122
import java.net.URI;
2223
import java.util.Arrays;
2324
import java.util.List;
@@ -27,6 +28,23 @@
2728

2829
import javax.net.ssl.SSLException;
2930

31+
import org.reactivestreams.Publisher;
32+
import org.springframework.cloud.gateway.config.GrpcSslConfigurer;
33+
import org.springframework.cloud.gateway.filter.GatewayFilter;
34+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
35+
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
36+
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
37+
import org.springframework.cloud.gateway.route.Route;
38+
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
39+
import org.springframework.core.ResolvableType;
40+
import org.springframework.core.io.Resource;
41+
import org.springframework.core.io.ResourceLoader;
42+
import org.springframework.core.io.buffer.DataBuffer;
43+
import org.springframework.core.io.buffer.NettyDataBufferFactory;
44+
import org.springframework.http.codec.json.Jackson2JsonDecoder;
45+
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
46+
import org.springframework.web.server.ServerWebExchange;
47+
3048
import com.fasterxml.jackson.core.JsonProcessingException;
3149
import com.fasterxml.jackson.databind.JsonNode;
3250
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -42,6 +60,7 @@
4260
import com.google.protobuf.DynamicMessage;
4361
import com.google.protobuf.ProtocolStringList;
4462
import com.google.protobuf.util.JsonFormat;
63+
4564
import io.grpc.CallOptions;
4665
import io.grpc.Channel;
4766
import io.grpc.ClientCall;
@@ -51,28 +70,9 @@
5170
import io.grpc.protobuf.ProtoUtils;
5271
import io.grpc.stub.ClientCalls;
5372
import io.netty.buffer.PooledByteBufAllocator;
54-
import org.reactivestreams.Publisher;
5573
import reactor.core.publisher.Flux;
5674
import reactor.core.publisher.Mono;
5775

58-
import org.springframework.cloud.gateway.config.GrpcSslConfigurer;
59-
import org.springframework.cloud.gateway.filter.GatewayFilter;
60-
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
61-
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
62-
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
63-
import org.springframework.cloud.gateway.route.Route;
64-
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
65-
import org.springframework.core.ResolvableType;
66-
import org.springframework.core.io.Resource;
67-
import org.springframework.core.io.ResourceLoader;
68-
import org.springframework.core.io.buffer.DataBuffer;
69-
import org.springframework.core.io.buffer.NettyDataBufferFactory;
70-
import org.springframework.http.codec.json.Jackson2JsonDecoder;
71-
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
72-
import org.springframework.web.server.ServerWebExchange;
73-
74-
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator;
75-
7676
/**
7777
* This filter takes a JSON payload, transform it into a protobuf object, send
7878
* it to a
@@ -99,7 +99,7 @@ public JsonToGrpcGatewayFilterFactory(GrpcSslConfigurer grpcSslConfigurer, Resou
9999

100100
@Override
101101
public List<String> shortcutFieldOrder() {
102-
return Arrays.asList("protoDescriptor", "protoFile", "service", "method");
102+
return Arrays.asList("service", "method", "protoDescriptor");
103103
}
104104

105105
@Override
@@ -128,8 +128,6 @@ public static class Config {
128128

129129
private String protoDescriptor;
130130

131-
private String protoFile;
132-
133131
private String service;
134132

135133
private String method;
@@ -143,15 +141,6 @@ public Config setProtoDescriptor(String protoDescriptor) {
143141
return this;
144142
}
145143

146-
public String getProtoFile() {
147-
return protoFile;
148-
}
149-
150-
public Config setProtoFile(String protoFile) {
151-
this.protoFile = protoFile;
152-
return this;
153-
}
154-
155144
public String getService() {
156145
return service;
157146
}

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,13 @@ public GatewayFilterSpec circuitBreaker(Consumer<SpringCloudCircuitBreakerFilter
284284

285285
/**
286286
* A filter that transforms a JSON request into a gRPC one.
287-
* @param protoDescriptor relative path to the proto descriptor file.
288-
* @param protoFile relative path to the proto definition file.
289287
* @param service fully qualified name of the service that will handle the request.
290288
* @param method method name in the service that will handle the request.
289+
* @param protoDescriptor relative path to the proto descriptor file.
291290
*/
292-
public GatewayFilterSpec jsonToGRPC(String protoDescriptor, String protoFile, String service, String method) {
291+
public GatewayFilterSpec jsonToGRPC(String service, String method, String protoDescriptor) {
293292
return filter(getBean(JsonToGrpcGatewayFilterFactory.class).apply(c -> c.setMethod(method)
294293
.setProtoDescriptor(protoDescriptor)
295-
.setProtoFile(protoFile)
296294
.setService(service)));
297295
}
298296

0 commit comments

Comments
 (0)