0% found this document useful (0 votes)
70 views

Build Real-World Microservices With GRPC - The New Stack

Uploaded by

Doan Thanh Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Build Real-World Microservices With GRPC - The New Stack

Uploaded by

Doan Thanh Hung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack

Build Platforms at Scale BOOK A


Build your Internal Developer Platform (IDP), self-serve developers,
and ship applications faster DEMO

MICROSERVICES / NETWORKING / SOFTWARE DEVELOPMENT

Build Real-World Microservices with gRPC


Nov 27th, 2018 10:24am by Kasun Indrasiri

Feature image via Pixabay.

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

Do You Update Kubernetes?

We haven’t updated it.

We’re running the latest version.

We usually update, but we’re not running the latest version now.

I HAVE AN OPINION

We'd love to hear what you think.

WSO2 sponsored this post.

Early microservices implementations leveraged Representational State Transfer


(REST) architecture as the de-facto communication technology. However, RESTful
services are often useful for external-facing services, which are directly exposed to
consumers. As they are based on conventional text-based messaging (JSON, XML,
CVS over HTTP, etc.), which are optimized for humans, these are not ideal choices for
internal service-to-service communication.

Rather, using a text-based messaging protocol, we can leverage a binary protocol


that is optimized for inter-service communication. The Cloud Native Computing
Foundation’s gRPC (gRPC Remote Procedure Call) is an ideal choice for inter-service
communication since it uses protocol buffers as the binary data interchange format
for inter-service communication.

When we build multiple microservices with different technologies and programming


languages, it is important to have a standard way to define service interfaces and
underlying message interchange formats. gRPC offers a clean and powerful way to
specify service contracts using protocol buffers. Therefore, gRPC is probably the
most viable solution for building communication between internal microservices.

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

Founded in 2005, WSO2 enables the composable enterprise. Our


open source, API-first, and decentralized approach helps developers
and architects to be more productive and rapidly build digital
products to meet demand.
Learn More

THE LATEST FROM WSO2

Enhancing Interoperability and Streamlining Prior Authorization: A


Look into CMS-0057-F
18 May 2024

Not All MFA is Created Equal, Especially in CIAM


15 May 2024

Introducing Choreo Copilot


4 May 2024

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

1. How to Deploy Kubernetes with Kubeadm and containerd

2. The Next Evolution of Virtualization Infrastructure

3. Redis Pub/Sub vs. Apache Kafka

4. Researchers Re-Create the Antikythera Mechanism, the World's Oldest


Computer

5. Provision Bare-Metal Kubernetes with the Cluster API

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

Since gRPC is programming-language agnostic, you can use heterogeneous


languages to build services and clients. In this example, we have generated the
Inventory service code using Ballerina (ballerina.io) and the client-side code using
Java. You can try out this example using this source code on GitHub.

The service contract of the inventory(inventory.proto) is shown below.

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

Communication Under the Hood


When the client invokes the service, the client-side gRPC library uses the protocol
buffer and marshals the remote procedure call, which is then sent over HTTP2. On
the server side, the request is un-marshaled and the respective procedure invocation
is executed using protocol buffers. The response follows a similar execution flow
from the server to the client.

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.

A Pragmatic Microservices Use Case with gRPC


Microservices-based applications consist of multiple services and are built with a
variety of programming languages. Based on the business use case, you can pick the
most appropriate technology to build your service. gRPC plays a very important role
in this polyglot architecture. For example, let’s further extend our online retail use
case for something more realistic. As shown in Figure 2, product-search service
communicates with multiple other services, which are built using gRPC as the
communication protocol. So, we can define the service contract for each
service:inventory, electronics items, clothing items, etc. Now, if you want to foster a
polyglot architecture, you can generate service skeletons using different
implementation technologies.

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

A closer look at the microservices communication styles in Figure 2 shows gRPC is


used for all internal communication, while the external-facing communication can be
based on REST or GraphQL. When we use REST for external-facing communication,
most of the external clients can consume the service as an API (leveraging API
definition technologies such as Open API) because most of the external clients will
know how to communicate with an HTTP RESTful service. Also, we can use
technologies such as GraphQL to allow consumers to query the service based on the
specific client needs, which cannot be facilitated with gRPC.

Therefore, as a general practice, we can use gRPC for all synchronous


communications between internal microservices. Other synchronous messaging
technologies such as RESTful services and GraphQL are more suitable for external-
facing services.

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

Kasun is the director of integration architecture at WSO2 and is an author/evangelist on


Microservices Architecture and Enterprise Integration Architecture. He has authored
Microservices for Enterprise and Beginning WSO2 ESB. He is an Apache committer and has
worked as the...

Read more from Kasun Indrasiri

The Cloud Native Computing Foundation is a sponsor of The New Stack.

SHARE THIS STORY

TRENDING STORIES

1. How to Deploy Kubernetes with Kubeadm and containerd

2. The Next Evolution of Virtualization Infrastructure

3. Redis Pub/Sub vs. Apache Kafka

4. Researchers Re-Create the Antikythera Mechanism, the World's Oldest


Computer

5. Provision Bare-Metal Kubernetes with the Cluster API

INSIGHTS FROM OUR SPONSOR

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

Founded in 2005, WSO2 enables the composable enterprise.


Our open source, API-first, and decentralized approach helps
developers and architects to be more productive and rapidly
build digital products to meet demand.

Learn More

Enhancing Interoperability and Streamlining Prior


Authorization: A Look into CMS-0057-F
18 May 2024

Not All MFA is Created Equal, Especially in CIAM


15 May 2024

Introducing Choreo Copilot


4 May 2024

WSO2 to Be Acquired by EQT Partners


3 May 2024

Easier Low-Code Development with WSO2 Micro Integrator


for VS Code
24 April 2024

Unveiling WSO2 APK 1.1: Kubernetes-Native API Management


24 April 2024

T N S DA I LY N E W S L E T T E R

Receive a free roundup of the


most recent TNS articles in
your inbox each day.
https://thenewstack.io/build-real-world-microservices-with-grpc/ 10/13
15:05 22/5/24 Build Real-World Microservices with gRPC - The New Stack

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

Platform Engineering Podcasts


Operations Ebooks
CI/CD Events
Tech Careers Newsletter
Tech Culture TNS RSS Feeds
DevOps
Kubernetes
Observability
Service Mesh

THE NEW STACK roadmap.sh

About / Contact Community created roadmaps, articles,


Sponsors resources and journeys for developers
to help you choose your path and grow
Sponsorship
in your career.
Contributions

Frontend Developer Roadmap


Backend Developer Roadmap

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

© The New Stack 2024

Disclosures
Terms of Use
Advertising Terms & Conditions
Privacy Policy
Cookie Policy

https://thenewstack.io/build-real-world-microservices-with-grpc/ 13/13

You might also like