Package io.micronaut.protobuf.json.grpc
Class GrpcProxyService
java.lang.Object
io.micronaut.protobuf.json.grpc.GrpcProxyService
A service responsible for enabling interaction with gRPC methods through a JSON-based interface.
This class facilitates the invocation of gRPC methods by handling tasks such as locating the appropriate
service and method, converting JSON request payloads into Protobuf messages, executing the gRPC call reflectively,
and then transforming the Protobuf response(s) back into a JSON format for the consumer.
It supports unary and server-streaming responses. For streaming responses, it returns a JSON array.
Responsibilities: - Resolves the gRPC service and method metadata through the
This service is primarily designed to make it easier to expose gRPC methods for external clients and bridge the gap between JSON REST interfaces and Protobuf-based gRPC method implementations.
Thread Safety: - Instances of this class are effectively singletons, managed by the application framework. - While the class itself does not explicitly manage threading, it relies on thread-safe components like
Exceptions: -
Responsibilities: - Resolves the gRPC service and method metadata through the
GrpcServiceRegistry
.
- Manages the lifecycle of gRPC service beans within the application, utilizing the ApplicationContext
.
- Coordinates JSON-to-Protobuf and Protobuf-to-JSON transformations via the ProtobufJsonTranscoder
.
This service is primarily designed to make it easier to expose gRPC methods for external clients and bridge the gap between JSON REST interfaces and Protobuf-based gRPC method implementations.
Thread Safety: - Instances of this class are effectively singletons, managed by the application framework. - While the class itself does not explicitly manage threading, it relies on thread-safe components like
GrpcServiceRegistry
for service and method lookups. Asynchronous gRPC calls use internal
gRPC threading and synchronization primitives (like CountDownLatch) are used here.
Exceptions: -
MethodNotFoundException
: Thrown when a specific method cannot be found in the given service.
- ServiceNotFoundException
: Thrown when a service is not registered or managed in the application context.
- ProtobufTranscodingException
: Thrown for errors during JSON-Protobuf serialization or deserialization.
- GrpcInvocationException
: Thrown when gRPC method invocation fails, or a timeout occurs.-
Constructor Summary
ConstructorsConstructorDescriptionGrpcProxyService
(GrpcServiceRegistry registry, io.micronaut.context.ApplicationContext context, ProtobufJsonTranscoder transcoder) -
Method Summary
Modifier and TypeMethodDescriptioninvokeGrpcMethod
(String serviceName, String methodName, String jsonRequest) Invokes a specified gRPC method on a service and converts the Protobuf response(s) to a JSON string.
-
Constructor Details
-
GrpcProxyService
public GrpcProxyService(GrpcServiceRegistry registry, io.micronaut.context.ApplicationContext context, ProtobufJsonTranscoder transcoder)
-
-
Method Details
-
invokeGrpcMethod
Invokes a specified gRPC method on a service and converts the Protobuf response(s) to a JSON string. This method handles locating the gRPC method, converting the JSON request to a Protobuf message, invoking the method reflectively, and transforming the Protobuf response(s) back to JSON format. If the gRPC method returns a single message (unary), a single JSON object string is returned. If the gRPC method returns multiple messages (server streaming), a JSON array string is returned, where each element is the JSON representation of a message.- Parameters:
serviceName
- The name of the gRPC service containing the method. Must not be null or empty.methodName
- The name of the gRPC method to invoke within the service. Must not be null or empty.jsonRequest
- A JSON string representing the request payload. Must not be null or empty.- Returns:
- A JSON string representation of the Protobuf response(s). For unary calls, it's a JSON object. For streaming calls, it's a JSON array of objects. Never null (returns "[]" for empty streams).
- Throws:
MethodNotFoundException
- If the specified method cannot be found in the gRPC service.ServiceNotFoundException
- If the specified service cannot be located in the context.ProtobufTranscodingException
- If there is an error in converting JSON to or from Protobuf.GrpcInvocationException
- If the invocation of the gRPC method fails or times out.
-