Using gRPC for Microservices

First, a Short History of Remote Execution

The quest for the most optimal remote communication mechanism android development tutorial has been going on since the time the industry discovered networking. The challenge was to expose a framework to developers while operating systems had internal protocols.

When the gold standard for networking was developed, the focus shifted to cross- platform communication, whereby one computer could initiate an action on another computer across a network. A developer-friendly layer was created by technologies such as CORBA and DCOM. Language-agnostic communication is essential for the client/server architecture.

The evolution of the web began in the early 2000s, and gradually evolved as the defacto standard for communication. The framework for remote communication was offered by the combination of the two. The interoperability of various platforms was promised by the combination.

The internet was hitting the next wave. The combination of the two standards was too restrictive. The time when javascript and json became popular. The preferred wire-format protocol in the Web 2.0 phenomenon was the JSON. There is a new unofficial standard called REST. SOAP was limited to large enterprise applications that demanded strict adherence to standards and REST was a hit among contemporary developers.

HTTP, REST, and Microservices

Thanks to the rise of javascript frameworks and document databases, REST has become wildly popular among web developers Many applications started to rely on REST. Is the most efficient protocol for exchanging messages across services the same as the one for the machine? There is a huge performance trade-off when it comes to the convenience of HTTP.

gRPC is a lightweight communication protocol from the internet giant. It is an open-source framework that works across a dozen languages.

gRPC was adopted by a number of companies within the first year of its launch. CoreOS uses gRPC for peer communication. gRPC is a protocol used by telecom companies to stream data from their networking devices.

What is gRPC?

It reminded me of CORBA when I first saw it. Both frameworks declare the service in a language-agnostic interface definition language.

The clients are supposed to believe the server is on the same machine. The underlying protocol is handled by the method that clients invoke. The similarities mostly end with that

The way the serialization is handled is a secret sauce of gRPC. Protocol Buffers is an open source mechanism for serializing structured data. Protocol Buffers are similar toXML. They are smaller, faster, and more efficient than other protocols. Any custom data that needs to be serialized will be defined as a Protocol Buffer.

Protocol Buffer supports code generation in a number of programming languages. The accessors for each field definition are included in a Protocol Buffer.

gRPC has better performance and security. It promotes the use of sds and ts to protect the data exchanged between the client and the server

Why should they use gRPC? It is possible to support highly performant andScalable APIs. The use of a code keeps the message concise and efficient. Multiple concurrent messages can be in flight without compromising network resources. The size of requests and responses can be reduced.

Getting Started with gRPC

There is a simple way to create a gRPC service.

The Protocol Buffer file has a service definition. The.proto file has a gRPC code in it. The client that invokes the service should be created. Run the server and client.

A simple calculator service will be created in Python. It will be consumed by two clients. The steps are tested.

You can clone the repository to get the source code.

Set up gRPC

python install virtualenv venv source venv/bin/activate python

Install gRPC and gRPC Tools

1 2 3 python – m pip install grpcio python — global

There are directories for the Protocol Buffer.

Proto mkdir server, p Client/Node 1 2 3 4

The Protocol Buffer is in the Proto directory.

The package is called “proto3” and it contains a service calculator.

You can copy the code to the directories.

python -m grPC.tools.protoc –python_out Calc.proto is a python program that runs on a server. py../ Client/Python

Create the Server (Server/Calc_Server.py)

Calculator Servicer: def Add(self, request, context): return Calc_pb2. def Add ( self, request, context): return. Add reply ( n1 + request) Referred to as SubstractReply is self, request, and context. Divide ( self, request, context) return Calc_pb2 Divide reply ( f1 + request + def serve) Add_CalculatorServicer_to_server is a server add.

Launch the Server

python Calc_Server.py 1 python Calc_Server . py

Create the Python Client (Client/Python/Calc_Client.py)

import print_function import grPC import Calc_pb2 def run The response is called CalculatorStub. Add Calc_pb2. Add request ( n1, n2, n1, n2, n1, n2, n1, n1, n2, n1, n2, n1, n2, n1 There is a Substract. SubstractRequest ( n1, n2, n2, n2, n1, n2, n2, n1, n2, n2, n1, n2, n2, n1 Multiply. Print ( response n1, n2 ) Divide by. DivideRequest ( n1 is 20, n2 is 10 )) print if you want.

Launch the Python Client

python Calc_Client.py 1 python Calc_Client . py

The output of the above command will show if the client is able to invoke the methods on the server.

Create the Node.js Client (Client/Node/Calc_Client.js)

var PROTO_PATH is ‘Calc.proto’ and var grPC is ‘Require’. The calculator is located at ‘localhost:50050’ and it has credentials.

Launch the Node.js Client

node Calc_Client.js 1 node Calc_Client . js

The Node.js client does not need a stub to be created. The Protocol Buffer file can be accessed directly by the server.

If the network ports are open, the server and client can be run on separate machines.

In the upcoming articles, I will walk you through the steps of using gRPC.

Feature image via Pixabay.

Leave a Reply

Your email address will not be published.

You May Also Like