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://: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);
}
}