JsonToGrpc GatewayFilter 工厂

JSONToGRPC GatewayFilter Factory 用于将 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://: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);
    }
}
© . This site is unofficial and not affiliated with VMware.