跳到主要内容
版本:5.0.1

JsonToGrpc GatewayFilter 工厂

DeepSeek V3 中英对照 JsonToGrpc GatewayFilter Factory JsonToGrpc GatewayFilter Factory

JSONToGRPC 网关过滤器工厂将 JSON 负载转换为 gRPC 请求。

过滤器接受以下参数:

  • service: 处理请求的服务的简称。

  • method: 服务中处理请求的方法名称。

  • 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
备注

streaming 暂不支持。

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

当通过网关向 /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);
}
}