Build Real-World Microservices With GRPC - The New Stack
Build Real-World Microservices With GRPC - The New Stack
VOXPOP
Future-Proof Your Apps Save Your
Try our new 5 second
Dive intopoll. It's fast.cloud-native
open-source And it's fun!
solutions with CNCF
Ambassador Phil Estes Seat
https://thenewstack.io/build-real-world-microservices-with-grpc/ 1/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
We usually update, but we’re not running the latest version now.
I HAVE AN OPINION
https://thenewstack.io/build-real-world-microservices-with-grpc/ 2/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
In this article, we will take a closer look at why gRPC is a great choice for building
inter-microservices communication.
Fundamentals of gRPC
With gRPC, a customer can directly call methods on a server application on a
different machine as if it were a local object. gRPC is based on the foundations of
conventional Remote Procedure Call (RPC) technology but implemented on top of
the modern technology stacks such as HTTP2, protocol buffers etc. to ensure
maximum interoperability.
TRENDING STORIES
https://thenewstack.io/build-real-world-microservices-with-grpc/ 3/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
gRPC natively supports the ability to define a service contract using the gRPC
Interface Definition Language (IDL). So, as part of the service definition, you can
specify the methods that can be invoked remotely and the data structure of the
parameters and return types.
Figure 1. illustrates the use of gRPC with an online retail application as part of an
inventory and product-search service. The contract for the Inventory service is
defined using gRPC IDL, which is specified in the inventory.proto file. So, a developer
for the inventory service should first define all the business capabilities using the
service and then generate the service side skeleton code from the proto file.
Similarly, the client side code (stub) can be generated using the same proto file.
Figure 1 Zoom
1 syntax = "proto3";
2 package grpc_service;
https://thenewstack.io/build-real-world-microservices-with-grpc/ 4/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
3 import "google/protobuf/wrappers.proto";
4 service InventoryService {
5 rpc getItemByName(google.protobuf.StringValue) returns (Items);
6 rpc getItemByID(google.protobuf.StringValue) returns (Item);
7 rpc addItem(Item) returns (google.protobuf.BoolValue);
8 }
9
10 message Items {
11 string itemDesc = 1;
12 repeated Item items = 2;
13 }
14
15 message Item {
16 string id = 1;
17 string name = 2;
18 string description = 3;
19 }
The service contract is easy to understand and can be shared between the client and
the service. If there’s any change to the service contract, both the service and client
side code has to be regenerated.
For example, the following code snippet shows the generated code of the gRPC
service for Ballerina. For each operation that we have in the gRPC service definition,
the corresponding Ballerina code is generated. (Ballerina provides out-of-the-box
capabilities to generate the service or client code with “ballerina grpc –input
inventory.proto –output service-skeleton –mode service” or “ballerina grpc –input
inventory.proto –output bal-client –mode client”).
1 import ballerina/grpc;
2 import ballerina/io;
3 endpoint grpc:Listener listener {
4 host:"localhost",
5 port:9000
6 };
7
8 @grpc:ServiceConfig
9 service InventoryService bind listener {
10 getItemByName(endpoint caller, string value) {
11 // Implementation goes here.
12 // You should return a Items
13 }
14
https://thenewstack.io/build-real-world-microservices-with-grpc/ 5/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
15 getItemByID(endpoint caller, string value) {
16 // Creating a dummy inventory item
17 Item requested_item;
18 requested_item.id = value;
19 requested_item.name = "Sample Item " + value ;
20 requested_item.description = "Sample Item Desc for " + value;
21 _ = caller->send(requested_item);
22 _ = caller->complete();
23 }
24
25 addItem(endpoint caller, Item value) {
26 // Implementation goes here.
27 // You should return a boolean
28 }
29
30 }
For the client side, the product-search service, which is a Java (Spring Boot) service,
is again generated from the gRPC service definition of the Inventory service. You can
use the maven plugin to generate the client stub for the Spring Boot/Java service
(the client code is embedded in the Spring Boot service). The client code that
invokes the generated client stub is shown below.
1 package mfe.ch03.grpc;
2 import com.google.protobuf.StringValue;
3 import io.grpc.ManagedChannel;
4 import io.grpc.ManagedChannelBuilder;
5
6 public class InventoryClient {
7 public static void main(String[] args) {
8 ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 9000)
9 .usePlaintext()
10 .build();
11 InventoryServiceGrpc.InventoryServiceBlockingStub stub
12 = InventoryServiceGrpc.newBlockingStub(channel);
13 Inventory.Item item = stub.getItemByID(
14
15 StringValue.newBuilder().setValue("123").build());
16 System.out.println("Response : " + item.getDescription());
17 }
18 }
https://thenewstack.io/build-real-world-microservices-with-grpc/ 6/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
The main advantage of developing services and clients with gRPC is that your service
code or client side code doesn’t need to worry about parsing JSON or similar text-
based message formats (within the code or implicitly inside the underlying libraries
such as Jackson, which is hidden from service code). What comes in the wire is a
binary format, which is unmarshalled into an object. Also, having first-class support
for defining a service interface via an IDL is a powerful feature when we have to deal
with multiple microservices and ensure and maintain interoperability.
Figure 2 illustrates the inventory service with Ballerina lang, the electronics service
with Golang and the clothing service with Vert.x (Java). The client side can also
generate a stub for each of these service contracts.
https://thenewstack.io/build-real-world-microservices-with-grpc/ 7/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
Figure 2 Zoom
https://thenewstack.io/build-real-world-microservices-with-grpc/ 8/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
TRENDING STORIES
https://thenewstack.io/build-real-world-microservices-with-grpc/ 9/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
Learn More
T N S DA I LY N E W S L E T T E R
EMAIL ADDRESS
SUBSCRIBE
The New Stack does not sell your information or share it with unaffiliated third parties. By
continuing, you agree to our Terms of Use and Privacy Policy.
https://thenewstack.io/build-real-world-microservices-with-grpc/ 11/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
ARCHITECTURE ENGINEERING
Open Source AI
Cloud Native Ecosystem Large Language Models
Containers Frontend Development
Edge Computing Software Development
Microservices API Management
Networking Python
Serverless JavaScript
Storage TypeScript
WebAssembly
Cloud Services
Data
Security
OPERATIONS CHANNELS
https://thenewstack.io/build-real-world-microservices-with-grpc/ 12/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack
Devops Roadmap
FOLLOW TNS
Disclosures
Terms of Use
Advertising Terms & Conditions
Privacy Policy
Cookie Policy
https://thenewstack.io/build-real-world-microservices-with-grpc/ 13/13