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.