Getting Started
Part 1. Create a gRPC Server
Volo-gRPC is an RPC framework so that the bottom layer requires two major functions: Serialization and Transport.
IDL is short for Interface Definition Language
.
1.1 Why IDL
If we want to do RPC, we need to know what interface is for the server, what parameters to pass, and what the return value is, just like two people talking to each other, we need to make sure we are speaking the same language and doing the same thing.
At this time, we need to use IDL to specify the protocol for both sides, just like when writing code, we need to know the function signature while calling a function.
Protobuf IDL is a full-stack RPC solution for cross-language, the specific syntax can be seen in protocol-buffers/docs/proto3.
1.2 Write IDL
To create a gRPC project, we need to write a protobuf IDL first.
In your working directory, execute the following command:
Subsequently, use your favorite text editor to enter the following content:
After saving and exiting, we execute the following command:
Here we use the init
command, followed by the name of our project, which means we need to generate template code. At the end, you need to specify an IDL used by the server.
If you only need to add an IDL (such as the client IDL) without generating a template, do as follows:
| What’s more, the volo tool also supports downloading IDL from git and then generating code, such as:
| You may directly enter volo to see the detailed usage ~ next back to the topic ~
At this point, our entire directory structure looks like this:
Then we open src/lib.rs
and add the method implementation to the impl
block. The resulting code should look like this:
Then execute:
At this point, you will find volo_gen.rs
file under OUT_DIR Directory.
Then execute the following command to get our server running:
At this point, we have our server running!
Part 2. Create a Client
In the previous section, we wrote a server, now let’s write a client and call the server.
First, create a file called src/bin/client.rs
and type the following:
Then add the required dependencies to the Cargo.toml
file, which looks like this:
Then, create a new terminal and run the following command to start our server:
Finally, we go back to the current directory and execute the following command, and we can see that the execution is successful:
Part 3. Add a Middleware
Next, let’s look at how to add middleware to Volo.
For example, if we need a middleware that prints out the received requests, the returned responses and the elapsed time, we could write a Service in lib.rs
:
Then we wrap a Layer around the Service:
Finally, we add this Layer to client and server:
At this point, it prints out how long the request took at the INFO log level.
Part 4. What’s Next?
Congratulations, you’ve read this far! At this point, we’ve basically learned how to use Volo, and we’re ready to use Volo to kick off our Rust journey
Next, you may need to select the right components, put them together, and interface with your system.
The related ecosystem maintained by Volo will be located in: https://github.com/volo-rs, we are working to build our ecosystem, and welcome everyone to join us ~
If there is a dire lack of components, you are welcomed to raise an issue in: https://github.com/cloudwego/volo, we will support it as soon as possible.
In the meantime, welcome to join our Lark user group and share your experience with us about Volo.
![Volo_feishu](/img/docs/feishu_group_volo.png)
Looking forward to your unique work created with Volo.