JsonToGrpc GatewayFilter Factory
The JSONToGRPC GatewayFilter Factory converts a JSON payload to a gRPC request.
The filter takes the following arguments:
-
service: Short name of the service that handles the request. -
method: Method name in the service that handles the request. -
protoDescriptor: Proto descriptor file.
This file can be generated using protoc and specifying the --descriptor_set_out flag:
protoc --proto_path=src/main/resources/proto/ \
--descriptor_set_out=src/main/resources/proto/hello.pb \
src/main/resources/proto/hello.proto
streaming is not supported.
|
application.yml.
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("json-grpc", r -> r.path("/json/hello").filters(f -> {
String service = "HelloService";
String method = "hello";
String protoDescriptor = "file:src/main/proto/hello.pb";
return f.jsonToGRPC(service, method, protoDescriptor);
}).uri(uri))
spring:
cloud:
gateway:
routes:
- id: json-grpc
uri: https://localhost:6565
predicates:
- Path=/json/**
filters:
- name: JsonToGrpc
args:
service: HelloService
method: hello
protoDescriptor: file:proto/hello.pb
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.
By default, it creates a NettyChannel by using the default TrustManagerFactory. However, you can customize this TrustManager by creating a bean of type GrpcSslConfigurer:
@Configuration
public class GRPCLocalConfiguration {
@Bean
public GRPCSSLContext sslContext() {
TrustManager trustManager = trustAllCerts();
return new GRPCSSLContext(trustManager);
}
}