= gRPC Component
:doctitle: gRPC
:shortname: grpc
:artifactid: camel-grpc
:description: Expose gRPC endpoints and access external gRPC endpoints.
:since: 2.19
:supportlevel: Stable
:component-header: Both producer and consumer are supported
//Manually maintained attributes
:camel-spring-boot-name: grpc

*Since Camel {since}*

*{component-header}*

The gRPC component allows you to call or expose Remote Procedure Call (RPC) services
using https://developers.google.com/protocol-buffers/docs/overview[Protocol Buffers (protobuf)]
exchange format over HTTP/2 transport.

Maven users will need to add the following dependency to their `pom.xml`
for this component:

[source,xml]
------------------------------------------------------------
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-grpc</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>
------------------------------------------------------------

gRCP requires strong own of the Google Guava version. It's necessary to
add following configuration property to the maven file

[source,xml]
------------------------------------------------------------
<properties>
    <google-guava-version>${grpc-guava-version}</google-guava-version>
<properties>
------------------------------------------------------------

== URI format

-------------------------------------
grpc://service[?options]
-------------------------------------


// component-configure options: START

// component-configure options: END

// component options: START
include::partial$component-configure-options.adoc[]
include::partial$component-endpoint-options.adoc[]
// component options: END

// endpoint options: START

// endpoint options: END


== Transport security and authentication support

The following https://grpc.io/docs/guides/auth.html[authentication] mechanisms are built-in to gRPC and available in this component:

* *SSL/TLS:* gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the server, and to encrypt all the data exchanged between the client and the server. Optional mechanisms are available for clients to provide certificates for mutual authentication.
* *Token-based authentication with Google:* gRPC provides a generic mechanism to attach metadata based credentials to requests and responses. Additional support for acquiring access tokens while accessing Google APIs through gRPC is provided. In general this mechanism must be used as well as SSL/TLS on the channel.

To enable these features the following component properties combinations must be configured:

[width="100%",cols="10%,20%,25%,15%,30%",options="header",]
|=======================================================================
|Num.|Option |Parameter|Value|Required/Optional
|1|*SSL/TLS*|negotiationType|TLS|Required
|||keyCertChainResource||Required
|||keyResource||Required
|||keyPassword||Optional
|||trustCertCollectionResource||Optional
|2|*Token-based authentication with Google API*|authenticationType|GOOGLE|Required
|||negotiationType|TLS|Required
|||serviceAccountResource||Required
|3|*Custom JSON Web Token implementation authentication*|authenticationType|JWT|Required
|||negotiationType|NONE or TLS|Optional. The TLS/SSL not checking for this type, but strongly recommended.
|||jwtAlgorithm|HMAC256(default) or (HMAC384,HMAC512)|Optional
|||jwtSecret||Required
|||jwtIssuer||Optional
|||jwtSubject||Optional
|=======================================================================
TLS with OpenSSL is currently the recommended approach for using gRPC over TLS component.
Using the JDK for ALPN is generally much slower and may not support the necessary ciphers for HTTP2. This function is not implemented in the component.

There might be a need to install additional libraries according to the Operating System of choice.
For more information consult https://github.com/grpc/grpc-java/blob/master/SECURITY.md[the Security page of gRPC].

== gRPC producer resource type mapping

The table below shows the types of objects in the message body, depending on the types (simple or stream) of incoming and outgoing parameters, as well as the invocation style (synchronous or asynchronous). Please note, that invocation of the procedures with incoming stream parameter in asynchronous style are not allowed.

[width="100%",cols="15%,15%,15%,25%,25%",options="header",]
|=======================================================================
|Invocation style |Request type|Response type|Request Body Type|Result Body Type

|*synchronous*|simple|simple|Object|Object
|*synchronous*|simple|stream|Object|List<Object>
|synchronous|stream|simple|not allowed|not allowed
|synchronous|stream|stream|not allowed|not allowed

|*asynchronous*|simple|simple|Object|List<Object>
|*asynchronous*|simple|stream|Object|List<Object>
|*asynchronous*|stream|simple|Object or List<Object>|List<Object>
|*asynchronous*|stream|stream|Object or List<Object>|List<Object>

|=======================================================================

== gRPC consumer headers (will be installed after the consumer invocation)

[width="100%",cols="25%,50,25%",options="header",]
|=======================================================================
|Header name |Description|Possible values

|*CamelGrpcMethodName*|Method name handled by the consumer service|
|*CamelGrpcEventType*|Received event type from the sent request|onNext, onCompleted or onError
|*CamelGrpcUserAgent*|If provided, the given agent will prepend the gRPC library's user agent information|

|=======================================================================

== Examples

Below is a simple synchronous method invoke with host and port parameters

[source,java]
-------------------------------------------------------------------------------
from("direct:grpc-sync")
.to("grpc://remotehost:1101/org.apache.camel.component.grpc.PingPong?method=sendPing&synchronous=true");
-------------------------------------------------------------------------------

[source,java]
---------------------------------------------------------------------------------------
<route>
    <from uri="direct:grpc-sync" />
    <to uri="grpc://remotehost:1101/org.apache.camel.component.grpc.PingPong?method=sendPing&synchronous=true"/>
</route>
---------------------------------------------------------------------------------------

An asynchronous method invoke

[source,java]
-------------------------------------------------------------------------------
from("direct:grpc-async")
.to("grpc://remotehost:1101/org.apache.camel.component.grpc.PingPong?method=pingAsyncResponse");
-------------------------------------------------------------------------------

gRPC service consumer with propagation consumer strategy

[source,java]
-------------------------------------------------------------------------------
from("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?consumerStrategy=PROPAGATION")
.to("direct:grpc-service");
-------------------------------------------------------------------------------

gRPC service producer with streaming producer strategy (requires a service that uses "stream" mode as input and output)

[source,java]
-------------------------------------------------------------------------------
from("direct:grpc-request-stream")
.to("grpc://remotehost:1101/org.apache.camel.component.grpc.PingPong?method=PingAsyncAsync&producerStrategy=STREAMING&streamRepliesTo=direct:grpc-response-stream");

from("direct:grpc-response-stream")
.log("Response received: ${body}");
-------------------------------------------------------------------------------

gRPC service consumer TLS/SLL security negotiation enable

[source,java]
-------------------------------------------------------------------------------
from("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?consumerStrategy=PROPAGATION&negotiationType=TLS&keyCertChainResource=file:src/test/resources/certs/server.pem&keyResource=file:src/test/resources/certs/server.key&trustCertCollectionResource=file:src/test/resources/certs/ca.pem")
.to("direct:tls-enable")
-------------------------------------------------------------------------------

gRPC service producer with custom JSON Web Token implementation authentication

[source,java]
-------------------------------------------------------------------------------
from("direct:grpc-jwt")
.to("grpc://localhost:1101/org.apache.camel.component.grpc.PingPong?method=pingSyncSync&synchronous=true&authenticationType=JWT&jwtSecret=supersecuredsecret");
-------------------------------------------------------------------------------

== Configuration

It's it is recommended to use Maven Protocol Buffers Plugin which calls Protocol Buffer Compiler (protoc) tool to generate Java source files from .proto (protocol buffer definition) files for the custom project. This plugin will generate procedures request and response classes, their builders and gRPC procedures stubs classes as well.

Following steps are required:

Insert operating system and CPU architecture detection extension inside **<build>** tag of the project pom.xml or set ${os.detected.classifier} parameter manually
[source,xml]
-------------------------------------------------------------------------
<extensions>
  <extension>
    <groupId>kr.motd.maven</groupId>
    <artifactId>os-maven-plugin</artifactId>
    <version>1.4.1.Final</version>
  </extension>
</extensions>
-------------------------------------------------------------------------

Insert gRPC and protobuf Java code generator plugin **<plugins>** tag of the project pom.xml

[source,xml]
-------------------------------------------------------------------------
<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <version>0.5.0</version>
  <configuration>
    <protocArtifact>com.google.protobuf:protoc:${protobuf-version}:exe:${os.detected.classifier}</protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc-version}:exe:${os.detected.classifier}</pluginArtifact>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>compile-custom</goal>
        <goal>test-compile</goal>
        <goal>test-compile-custom</goal>
      </goals>
    </execution>
  </executions>
</plugin>
-------------------------------------------------------------------------

== More information

See these resources:

- http://www.grpc.io/[gRPC project site]
- https://www.xolstice.org/protobuf-maven-plugin[Maven Protocol Buffers Plugin]



include::spring-boot:partial$starter.adoc[]
