Protocol

gRPC uses a special language for protocol declarations.There are two versions of the language—proto2 and proto3. We will use the second as it's more modern. Create a ring.proto file and add the following declaration:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "rust.microservices.ring";
option java_outer_classname = "RingProto";
option objc_class_prefix = "RING";

package ringproto;

message Empty { }

service Ring {
rpc StartRollCall (Empty) returns (Empty);
rpc MarkItself (Empty) returns (Empty);
}

As you can see, we specified the syntax as proto3. Options give you the ability to set the properties for the generation of source files for different languages if you will interact with a service from other applications or other microservices. We don't need to set these options for our example, but you might have this part in a file if you take it from another developer.

The protocol declaration contains a package name set with the package specifier and a package name that we set to ringproto.

Also, we added the Empty message with no fields. We will use this type as the input and output parameter for all methods, but it's better to use different types for real microservices. Firstly, you can't have methods without input and output parameters. The second reason is future service improvements. If you want to add extra fields to the protocol later, you can do it. Moreover, the protocol can easily cope with different versions of the protocol; often you can use both new and old microservices, because Protocol Buffers work fine with extra fields, and you can extend the protocol later when you need it.

The service declaration is contained in the service section. You can have multiple services' declarations in a protocol declaration file and use only the necessary declared services in an implementation. But we need only one service declaration for our ring example. Add the Ring service and include two RPC methods with the rpc specifier. We added the StartRollCall method and MakeItself. The same as we did in the previous example. Both take the Empty value as an input argument and return Empty as well.

The name of a service is important, because it will be used as a prefix for multiple types in generated Rust sources. You can create sources using the protoc tool, but it's more convenient to create a build script that will generate sources with protocol types during compilation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.208