It streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide our applications and services. Containers are great for…
Protobuf, gRPC and Code Generation
We will be using examples from official grpc repositories for Server and Client code.
In layman’s terms, protobuf is a way of converting data from data structure in a program to a format capable of storing or transmission (data serialization) or vice-versa (data deserialization). It has been introduced as a replacement to XML in 2001. When protobuf is used along with grpc for communicating with services, it has found to be very useful and efficient.
There are two packages you need to install now :
Install the protobuf compiler (protoc), written in C++ for generating the code from .proto files.
2. Protobuf runtime & gRPC
Now you need to install Protobuf runtime for each of the languages you wish to use protoc to generate code for. In this case, we are going to use Python and Go.
For Python :
For Go :
Let’s define a simple protocol buffer for Hello world.
Here, the syntax refers to the version of protobuf we are using, in this case we use version 3. This is required for ensuring backward compatibility with previous versions of protobuf. Message is one of the base objects in protobuf. Each Message can have multiple fields or components, in our case, it’s just Name for Request message. You can also see that field has a type, which in this case is string for Name.
Now, here comes the magic of using protobuf. You can run a single command and get your server & client code auto-generated.
You can see files generated in same folder
If you want to generate code for Go (optional since we fetch client proto from grpc examples), run the following code:
This means anyone who wants to consume the service that you build can just import the auto-generated client code and start using it, saving a lot of time for you as well as them. Using auto-generated code also makes sure the client sends the request that the server expects including the data type if the request contains any attributes. Also, you might have noticed something while executing the compiler, it automatically generated code for Python as well as Go. This is where Language Independence comes into the picture. Having proto to define the schema of your API means you can generate server and client code in any of the supported languages and start from there. This saves a lot of time while building the application as well as while making changes. Also, even if you want to change your server or client written in python to Go, that’s easily possible now, thanks to protobuf. Also, the services that use your API have the independence of writing code in any language, as long as client code for that language is generated.
This will start the grpc server on localhost port 50051. Now, let’s run a client that connects to this server for greeting message. The repository you cloned for installing grpc already has code for Go client.
This gives the output :
Try changing your Go client to send an integer instead of string for the name field. Executing the client will give you the following error:
That’s some useful feedback that the client is getting from generated code.
nessa noite eu achei que nunca ficaria bêbada mesmo depois de tanta cerveja e tanta cachaça nada batia ou batia tão forte que eu ignorava a questão é que percebi muita coisa; primeiro: homem…
It looked being painting in my opinion. Then apply another coat of eyeliner that are on your upper decrease lash styles. The damp brush will slightly liquefy the shadow, making it simple to apply as a
Agile software development requires us to think fast, be flexible and efficient. If you lack in any of these principles, you are probably doing something wrong.