Building micro-services in Rust using gRPC!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
rpc which stands for remote procedure call is a way for your code to call an external service using a function call instead of an http request the benefit of this technology is that it simplifies your code because it looks like you're making regular function calls even though you're accessing an external service this is why rpc is commonly used in a microservice architecture grpc which was created by google is the most popular rpc framework one major benefit of grpc is that microservices could talk to each other even if they're implemented in different languages this is because rpc functions and arguments are defined using an interface definition language called protobuf that protobuf is then used to generate function stubs in many different languages rust has great support for grpc in today's video we're going to learn how to use grpc in rust by implementing a grpc server and client but before we get to that if you haven't already make sure to get your free rust chi shi by heading over to let's get rusty dot com forward slash chishi with that said let's get rusty [Music] in this video we're going to simulate a payment service in rust the first thing we want to do is define our protocol buffers so to do that let's create a new folder in the root directory called proto then we'll add a file called payments.proto then we'll specify the syntax version we're using and define a package name the package name will be used by the grpc library we add later on next let's define a service our service is going to be called bitcoin which is a payment method and it's going to have one function called send payment which takes a btc payment request and returns a btc payment response let's define both those messages a payment request is going to have a from address 2 address and payment amount and a payment response is going to have a flag indicating if the transaction was successful and a message now that our protocol buffers are defined let's flip over to cargo.tamil and add some dependencies the first two dependencies we'll add is tonic and post post is a protocol buffer implementation for rust and tonic is a grpc implementation for rust we'll also take a dependency on tokyo which is the most popular async runtime for rust for tokyo will enable the macros and multi-threaded runtime features we'll also take a build dependency on tonic build which will allow cargo to build our protocol buffers during compilation to use this dependency we'll also need to add a build.rs file in the root of our directory this file acts as a build script for cargo and it will configure tonic build here we're telling tonic build to compile our protocol buffers which are located in the proto directory with the setup out of the way let's create our grpc server and client first we'll rename main.rs to server.rs we'll also create a new file in the source directory called client.rs and we'll give client.rs an empty main function then we'll navigate back to cargo.tamul and mark both these files as binary crates server.rs is going to be compiled to a library called payments dash server and client.rs is going to be compiled to a binary called payments-client then we'll implement the grpc server first we'll import the various types tonic generated for us based on our protocol buffer definitions on line six through eight we declare a new module called payments which is going to use the include proto macro to include the types tonic generated for us based on our payments.proto file on line 3 and 4 we bring those types into scope also on line 1 we bring some types defined in the tonic library into scope underneath the payments module let's create a new struct called bitcoin service then we'll implement the bitcoin trait which we defined up here for bitcoin service the bitcoin tray requires that we implement all the methods we've defined on the bitcoin service which in this case is only one method called send payment also note that we add this tonic async trait attribute because the send payment method is going to be async the send payment method is going to take a request which is going to be a grpc request containing btc payment request metadata and return a result which is either going to be a grpc response containing btc payment response metadata or a grpc status in the error case inside the method body we're going to print the request get the request metadata create a reply and then send out the reply because this is just an example we're always going to say successful to true the last thing we need to do is update main let's update main to use the tokyo async runtime then we'll create a new instance of bitcoin service and finally we'll instantiate a new grpc server which uses bitcoin service with that our grpc server is now complete next let's implement our grpc client just like before first we'll import the protobuf types tonic defined for us in this case we only need bitcoin client and btc payment request and again like before we'll update maine to use the tokyo runtime inside main let's instantiate a new instance of bitcoin client then we'll create a new btc payment request finally we'll send the request and print the response with that our grpc client is now complete let's go ahead and test our code by running the grpc server and client at the same time first we'll run the server by typing in cargo run dash dash bin payments dash server then we'll run the client in a separate terminal by typing in cargo run dash dash bin payments client as we can see our grpc server got the request and returned a response that's it for this video but before you leave make sure to get your free rust chi shi by heading over to let's get rusty.com forward slash chi shi also if you enjoyed the content make sure to give the video a like and subscribe to the channel with that said i'll see you in the next one [Music]
Info
Channel: Let's Get Rusty
Views: 54,163
Rating: undefined out of 5
Keywords: rust tutorial, systems programming, rust lang, rust programming, rust programming language, rust programming tutorial, learn rust, rust language, rust programming for beginners, rust programming language tutorial, rust programming language course, rust programming tutorial #1, rust programming full tutorial, learn rust programming, learn rust programming language, learn rust 2020, rust tutorial 2021, system programming linux, rust programming project
Id: JkSa-qA2jnY
Channel Id: undefined
Length: 9min 21sec (561 seconds)
Published: Sat May 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.