JsonToGrpc
GatewayFilter
Factory
JSONToGRPC GatewayFilter Factory 用于将 JSON 负载转换为 gRPC 请求。
该过滤器接受以下参数
-
protoDescriptor
: Proto 描述符文件。
此文件可以使用 protoc
并指定 --descriptor_set_out
标志生成
protoc --proto_path=src/main/resources/proto/ \
--descriptor_set_out=src/main/resources/proto/hello.pb \
src/main/resources/proto/hello.proto
-
protoFile
: Proto 定义文件。 -
service
: 处理请求的服务简称。 -
method
: 处理请求的服务中的方法名。
不支持 streaming 。 |
application.yml。
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("json-grpc", r -> r.path("/json/hello").filters(f -> {
String protoDescriptor = "file:src/main/proto/hello.pb";
String protoFile = "file:src/main/proto/hello.proto";
String service = "HelloService";
String method = "hello";
return f.jsonToGRPC(protoDescriptor, protoFile, service, method);
}).uri(uri))
spring:
cloud:
gateway:
routes:
- id: json-grpc
uri: https://localhost:6565/testhello
predicates:
- Path=/json/**
filters:
- name: JsonToGrpc
args:
protoDescriptor: file:proto/hello.pb
protoFile: file:proto/hello.proto
service: HelloService
method: hello
当通过网关向 /json/hello
发送请求时,请求会使用 hello.proto
中提供的定义进行转换,发送到 HelloService/hello
,然后将响应转换回 JSON。
默认情况下,它使用默认的 TrustManagerFactory
创建一个 NettyChannel
。但是,你可以通过创建类型为 GrpcSslConfigurer
的 bean 来自定义此 TrustManager
。
@Configuration
public class GRPCLocalConfiguration {
@Bean
public GRPCSSLContext sslContext() {
TrustManager trustManager = trustAllCerts();
return new GRPCSSLContext(trustManager);
}
}