Implementing applications with gRPC
With Code Engine, you can configure your applications to run as gRPC servers to handle client calls by taking advantage of gRPC features.
gRPC is a modern open source high-performance remote procedure call (RPC) framework that can connect services in and across data centers. Provides for efficient binary serialization, bidirectional streaming, load balancing for client and servers, tracing, health checking, and authentication. For more information, see gRPC documentation.
Benefits of using gRPC
gRPC provides several advantages in facilitating communications between clients and servers. A fundamental aspect of gRPC is to define a clear interface that encapsulates all the methods and parameters available for a remote invocation. On the server side, this interface is implemented to handle and respond to client requests effectively.
This approach of working with gRPC with your Code Engine applications offers benefits.
-
Interoperability - gRPC clients and servers can run and communicate with each other in various environments. For example, you can define your servers with the Go language, while your clients are defined in Java.
-
Serialization - gRPC uses protocol buffers for message serialization, which is more efficient in terms of size and speed, than using JSON in HTTP APIs.
-
Streaming - gRPC supports bidirectional streaming so that clients and servers send and receive messages simultaneously over a single connection.
-
Error detection - gRPC enforces a strongly typed contract between the client and server through the protocol buffers (
.proto
files). This contract makes sure that the client and server agree on interfaces and data structure, which provides for better code generation, validation, and error handling. -
Security - gRPC supports various authentication and encryption methods, including mutual TLS, which provides secure communication between the client and server.
Configuring Code Engine applications to use gRPC
gRPC is supported for Code Engine applications at a project level, which means that your Code Engine gRPC server application can only serve traffic to clients that run in the same project.
You must use the Code Engine CLI to configure a Code Engine gRPC application.
Before you begin
From Code Engine:
- Set up your Code Engine CLI environment.
- Create a project.
-
Create and deploy an application with the
ibmcloud ce application create
command. To indicate that this application supports gRPC, which uses HTTP/2 as a transport mode, you must specify the--port
option with the format[NAME:]PORT
; for example,--port h2c:8080
. When[NAME:]
ish2c
, the port uses unencrypted HTTP/2. By default, Code Engine assumes that apps listen for incoming connections on port8080
.In the following example, use
myapp-grpcserver
as the name of the server application and specifyicr.io/codeengine/grpc-server
as the image to reference.ibmcloud ce application create --name myapp-grpcserver --port h2c:8080 --min-scale 1 --image icr.io/codeengine/grpc-server
-
Run the
application get
command to display the details about the app.ibmcloud ce application get --name myapp-grpcserver
Example output
[...] OK Name: myapp-grpcserver ID: abcdefgh-abcd-abcd-abcd-1a2b3c4d5e6f Project Name: myproject [...] Port: h2c:8080 [...]
Now that your server app is created and deployed to use gRPC, you must deploy a client application within the same project to access the server with gRPC. For more information about Code Engine sample code for deploying a gRPC server and client
application, see Code Engine grpc
sample.
Code Engine sample images that are built from the IBM Cloud Code Engine samples repository on GitHub are available in IBM Cloud® Container Registry in the public
icr.io/codeengine
namespace.