JsonToGrpc
GatewayFilter
ファクトリ
JSONToGRPC GatewayFilter ファクトリは、JSON ペイロードを gRPC リクエストに変換します。
フィルターは次の引数を取ります。
protoDescriptor
: プロト記述子ファイル。
このファイルは、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
: プロト定義ファイル。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);
}
}