Beginners Guide to gRPC in Go!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Love gRPC. Works great for our web apps.

πŸ‘οΈŽ︎ 17 πŸ‘€οΈŽ︎ u/sunny_tomato_farm πŸ“…οΈŽ︎ May 02 2020 πŸ—«︎ replies

Instead of importing ”golang.org/x/net/context”, I think you should really just be importing ”context”. It was moved into the standard library all the way back in Go 1.7, and the x/net package only exists for compatibility reasons at this point. Continuing to use it will just lead to confusion for people watching the videos.

πŸ‘οΈŽ︎ 8 πŸ‘€οΈŽ︎ u/coder543 πŸ“…οΈŽ︎ May 03 2020 πŸ—«︎ replies

As someone who was a beginning with gRPC I learned that gRPC excels in backend to backend communication. There are things like gRPC Gateway to help with http endpoints but the moment you need to deal with http authentication, cookies, or need to do anything with the request or response it becomes quite messy and I didn't feel it was worth the trade off or the cognitive load.

Edit:

Just to clarify I love gRPC. 90% of our services use it but there are times when it's not an ideal choice.

πŸ‘οΈŽ︎ 12 πŸ‘€οΈŽ︎ u/narkeeso πŸ“…οΈŽ︎ May 02 2020 πŸ—«︎ replies

can i use Grpc with go for video calling features? is bi directional will do the work efficiently?

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/theshivamlko πŸ“…οΈŽ︎ May 03 2020 πŸ—«︎ replies

Nice example.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/fgiorgetti πŸ“…οΈŽ︎ May 03 2020 πŸ—«︎ replies

How to validate inputs?

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/dumindunuwan πŸ“…οΈŽ︎ May 02 2020 πŸ—«︎ replies
Captions
everyone it's a life insets were alleged on it and unless tutorial we're going to be looking at the wonderful world G RPC now at the iPad pool earlier in the week asking you what you'd prefer me to record next and G RPC was the clear winner and I just like to say thanks to everyone that forces did not pull now as always the full text version of this tutorial will be available on my website so I'll leave a link to that in the description below but for now let's dive into the wonderful world of G RPC now we'll start off at the fitti and ye barons in some situations when compared to more traditional setups like rest api now once we have a firm handle as to how GP RPC can benefit our systems will then be taking a look at how you can build simple G RPC clients and servers that are written in gold now if you haven't already I would recommend checking out my other tutorial which I've covered in a video on post called buffers angle and again I'll be leaving a link to that in the description below now before you can complete this tutorial you'll have to have the pro to see buffer v3 installed in your machine and you can do that by running gold yet - you get hub comm / goal line / put above / / to see Jen go and you'll also have to ensure that the go pass / been directory path is on your environment path so that you can use the pro to see - that is installed in a directory so before we dive in we first need to understand what G RPC as how it works and so on now the definition as G RPC as a modern open source remote procedure call framework that can run anywhere the remote procedure calls are something that we use within distributed systems that allow us to communicate between the applications more specifically allows us allows us to expose methods within our application they've run other applications to be able to invoke it's similar to REST API communication in the sense that you are effectively exposing functionality within your app to other apps using HTTP slash TLS as a means of communication now the differences between G RPC and rest and well rest and the RPC are somewhat similar there are some fundamental differences and how they were that you should be aware of the first one is your PC utilizes HTTP - whereas rest utilizes HTTP 1.1 now G RPC also utilizes the protocol buffer data format as opposed to the standard JSON data format that is typically used within REST API s this makes a far more performant and it makes the amount of data that you're transferring across the network a lot smaller now with G RPC you can also utilize HTTP 2 capabilities such as server-side streaming client-side streaming or even bi-directional screaming should you wish which is really cool you should bear in mind that Wells grx PC does allow you to utilize these newer passive technology there's also more challenging prototyping at grcc service due to the fact that tools like postman HTTP client cannot be used to easily interact with your exposed G RPC service you do have options that make this possible but it's not something that's really are readily available natively there are options to use tools such as envoy to reverse proxy standard JSON requests and transcode them into the right their format but this is an additional dependency that can be tricky to set up for simple projects don't know that we've got the video of the way let's start with defining a really simple G RPC server and cool once we have a simple server set up and running we can then set about creating a gr PC client that will be able to interact with it now we're gonna start off by creating a new file called server core and this will have package mean and we're gonna import log and Nate for now like so and then we're going to define the main function which is simply going to set up a connection or a lesson connection on the host port to 9000 so we're going to do net dot listen and it's gonna be using TCP and as I said it's gonna be named thousand like so now as we're getting an error we're gonna want to handle that so F air does like Cornell we're just gonna do log dot fatal and field to listen on port 9000 and then we're gonna pass in the air cool next we're gonna want to import the official T RPC package from golang.org that's all allow us to effectively create a new G RPC server and then register the endpoints that we want to expose before serving us over our existing TCP connection that we've defined here so the package is located at google.com angle org slash G RPC and with this in place we can then go into the main function just below our lesson logic and we can do G RPC server as equal to G RPC II thought new server and then we want to do the following so F air and equals G RPC server dot Sarah then we're going to pass in the existing Nate built listener and if that returns an error so if air it's not equal to null then we want to do logged or fatal F field to serve G RPC server over port 9000 and again we want to pass on the error like so cool so that's right here is the absolute minimum for a G RPC server Retton in goal however at present it doesn't exactly do much so let's see about how we can change that now so let's say about defining a simple chat service no I want this chat service to receive messages from clients and then respond with its own message back now this is a really simple example as their gr PC works but Li it's a solid fun foundation upon which she can build more complex applications and I'm expecting to go into more complex examples and the coming weeks now the first thing we're going to do is the fame or contract or chat bot pro tool fail now with an S we're going to send tax not send text is equal to pro 2 or 3 and we're gonna say this is the chat package and with an S we're gonna define the message format that we were expecting from clients and that were also going to be subsequently sending back to clients and this is going to have a string called body and this will be the first possession now we're also gonna define the service in here so the service as chat service and with unless it's gonna have a solitary see Hello function which can be called by any G RPC client written and any language so RPC say hello take send a message and returns note this is plural not singular a message and that's all we need now let's go ahead and generate the cool code or the specific GRP C code using the proto Seto based on this contract now I'm going to open up the terminal and I'm gonna do put to see I say first of all I'm gonna do make their chat which is going to contain our chats package and then I'm gonna do a pull to see coole underscore out equals plugins equals G RPC chat and then I'm gonna pass in chat both total so if we open up the directory we can then see the best is created at chat PPO fail and you can have a look through this the best basically ethical declares all of the the functions that we need to things like talk to our chat service or register our chat service and our G RPC server or client go cool so with this in place we now have the code needed to automatically register and call the likes of the say hello function within our chat service however with not actually the fame that let's see hello what procedure call yet so let's do that note by creating new fail within our chat directory called Chateau go this is again gonna be part of the chat package and I'm gonna want to do the following some of the import log so we can log every incoming requests and golang.org / x / net / context so that we can get the context for the request and within this I'm going to define a tape server struct which will just be empty for now we're going to be defining methods off of the server stopped the first method we're going to do is X which will take in a pointer to the server and we're going to define the sea hello RPC methods so take context or context and I will take an a message which will be a pointer to a message and this is going to be part of the chat package or part of the auto generated code and here so we don't have to impor anything here this is gonna be at the faint struct that we can use and this is gonna return a message or error go so with an S hello or say hello method we want to do logged open F and received spell that correctly received a message body from clanked and this is gonna be what are saying our percentage s and then I'm gonna do a message dot body I'm gonna return a new message to the client that's calling this so message and the body is going to be hello from the server cool now I'm gonna pass an null as the final return just for the error we don't have any errors right now okay with us in place we're then gonna want to modify our server go fail we don't want to we're gonna first want to go mod in it with a nested a 3 so get hub comm / tutorial edge / go GRP see a tutorial and that's all this will then allow us to do Capcom / tutorial age / cool RPC tutorial / chat so that we can pour old all the code within this chat package so let's do that now so get hub comm / tutorial edge / cool do you actually see tutorial / chat now within less we're then going to want to do the fine so find a new server so s equals chat bot server then going to want to register the chat service server so chat dot register chat service server passing in our G RPC server that we want to register this over and our server struct now with this in place we then have a G RPC server that is able to expose this function to G RPC clients so we can test running that name by going into our terminal and running run server local and that should hopefully print out nothing as it has successfully started listening on localhost port 9000 cool so we've got the code for our G RPC server up and running but let's quickly recap what we've done here before we start going into the client-side code now we've defined our chat profile which effectively acts as our G RPC server contract and says that we're going to be exposing a chat service that has a single method called say hello that expects a message and returns a message now we've then implemented this server and that's Method here so this is the actual functionality of our chat service say hello method we've also generated the code needed to register these methods to our G RPC server we've also updated our server that goes so that it first creates a new instance of this chat server and then registers that using the auto generated method that's quite a lot to get your head around and it took me quite a while to get my header and as well when it was initially learning it but beer with it because it does eventually get simpler and does eventually start to make sense in your head go so know that we have a G RPC server up and running as time to build the client they could interact less server now I'm gonna do this within the seam directory just to keep things nice and simple and I'm gonna call this client local now again this is gonna be a separate executable so I'm gonna do it packaged mean this is going to import the log package it's gonna import going org slash X slash knit slash context oops ins gonna need to spell that right sartet market and we're gonna want to import the GRP c package from golang.org so google dot golang.org / g RPC and then we want to do github.com / tutorial edge / goal G RPC tutorial / chat perfect let's define our main function now we want to do VAR connection as a pointer to a gr PC client connection this connection or error is going to equal to G RPC dial so this is effectively trying to connect into the running G RPC server on localhost port 9000 and we're gonna do dot with and secure which creates an insecure connection so it disables transport security for this cloud connection cool now as we're getting an error let's again handle it so f error does not equal nil what's do log door fatal and dead orchid all connect passing the air like so and want to defer the connection clause so the next thing we want to do it below this as to the feign a chat service client so we can do that by calling chat got new chat service client passing on the connection and then we can start interacting with our G RPC server so we can do response or error is equal to C dot C hello and we can pass on context or background and we want to do a message so we'll do the message above actually just to keep it nice and simple so at chat door message actually will do message cause chat doc message and the body is gonna be hello from the client awesome so then we can do ampersand and message passing that go once again we get an error so let's handle it better and does not equal nil look fethiye F add our when calling see hello and again passing the error and we get the response from this method so logged up an F response from server as equal to this I hope he responds thought buddy cool so I'm gonna split my terminal and I'm gonna try and run this client cause I know so go run client go and what we've done wrong client got caught line 23 the comma try run again and as you can see the client has successfully sent a message to our G RPC server which is already up and running and I've received the message hello from the server back to the client awesome that's what we're going to cover in this tutorial now we've been able to successfully create G RPC server and go as well as create a client which is able to interact with that server now hopefully that says domestic ID some of the inner workings of how G RPC works if it dead and you enjoy that then please let me know in the comment section below or if you have any comments as to what this what it was you'd like to see next then again please let me know in the comment section below as always please like and subscribe and I'll catch you in the next video Cheers
Info
Channel: TutorialEdge
Views: 57,741
Rating: undefined out of 5
Keywords: programming, tutorials, coding tutorials, tutorialedge, grpc go, client server grpc golang, golang grpc tutorial, beginners grpc tutorial in go
Id: BdzYdN_Zd9Q
Channel Id: undefined
Length: 18min 15sec (1095 seconds)
Published: Sat May 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.