Building Microservices with Go: 14. gRPC Client Connections

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Wow but I'm pleased to see you all starting a little bit late today because well we're gonna give it our applause to all the wonderful workers who are looking after us in this current time of pandemic and I think they do a great job far more valuable than me just sitting at a computer and pushing buttons but anyway what we're gonna look at today is we're going to continue our series but we're gonna look at G RPC so in the last episode what we did was we looked at building out G RPC service we built a currency service and in this episode we're gonna look at how we can integrate that currency service with our API service so how we can make sort of client-side calls from go into our currency service sure we begin let's begin all right public service announcement now my good buddy Ivan is looking for a new role and I'm gonna put the link down below there but if you want to hire the best DevOps person the most knowledgeable person around infrastructure cloud and physical infrastructure and all of the stuff that you could ever find then you need to be telling your boss telling yourself and getting in touch there but public service announcement over let's get on with it already so last time when we left things we were looking at our code and what we'd done was we'd build this currency service so the currency service we declared by writing this proto file so if you remember back then protocol buffers are the format by which G RPC users to exchange data so you're gonna use jason a restful service with GRP see you're using protocol buffers so to define a service a G RPC service you write these protocol files and what I have here is a definition of a service for currency and it gets us just the currency rates so we have a request sorry a method called GATT rate it has a request parameter of rate request which is a simple type you specify two strings a base currency and a destination currency and it returns you a message type of rate response and rate response is just a floating point so it's gonna give you the exchange rate so for example if I choose GBP gosh gosh British pounds this is my base rate and I choose euros is my destination rate it'll probably tell me that the pound isn't worth exchanging and don't even bother but that's basically how the service works now what we want to do first before we kind of start looking at our client is we want to kind of look at just a few extra elements of protocol buffers because in addition to being able to do things like specify strings and floating-point numbers you can also specify enumerations and this is incredibly important in our instance because our rate request actually can only take certain values because our currency service is using be the European Central Bank's exchange rates and that only supports a certain number of currencies so let's take a look at at what they are well what we can do is we can define an enumeration so to define an enumeration in protobuf I can use this enum and I give it a name currencies and then I can specify a kind of a string representation and a value so pretty sort of standard sort of enumeration if we just quickly take a look at that inside of the protocol buff definitions and in the documentation and I'll put all the links down below for you but you can see that you're allowed to do this kind of setup you're also allowed to kind of set up some some sort of options custom options and things like that but the very sort of basic example it allows you to constrain the type of data that's coming in to your service so back over to our service an hour or enumeration so we're gonna have these currencies so euro that's going to be our base currency but we're going to cannot define the other currencies which are supported by the European Central Bank which is going to be from their API and I can just paste that in there so some folks are probably asking well can RPC or G RPC completely replace Jason and and I think the answer is maybe maybe the worst answer in the world huh but I think you've got to think about the use cases so one of the things where G RPC is not particularly good at and I would say it's more of an evolving area is things like front end services we've been having a kind of a chat around that in the live chat there just before we started the stream today and yeah it's kind of a I think for back-end services G RPC is really nice it's it's very fast it's very efficient you've got the capability of doing a unary service so you know is kind of a straightforward request response but also streaming you can stream data think like a WebSocket in terms of sort of HTTP the proto file this is your definition because what we want to be able to do is generate our our code from these files we don't want to kind of have to hand code or handcraft these objects we want to create a common interface file a protocol definition file that multiple different language types can then code generate to create their their actual underlying objects so we we've added that enumeration there so what we we need to do again if you kind of remember also before we do that is we're just gonna replace these types here so we're not going to use a string anymore what we can do is we can specify currencies all right so we've now got the ability to to specify an enumeration so when I send a rate request if I try to send a value which isn't in my currencies enum it's gonna bounce it it's not an acceptable request won't even hit my server right now so let's just generate that protobuf and we're going to use that make proto's command that we used setup in the last episode so we're using proto c and produce e is the the kind of the high level tool or the top level tool for for interacting with protocol buffers generating them from those proto files and you give proto's proto see you can use plugins so I can also say hey use the Geo PC plug-in and generate me not just the protocol buffers but also Kochan me the GRP c code right so we've just regenerated our code and if we want to kind of take a look at what that looks like and go let's look at currency dot P B so this is the generated file this is not a file that I'm going to particularly touch it's just automatically coach end and you can see that the definition of that enum inside of the proto file is translated through to this this consonant but also it kind of translates through to this currencies type and the currencies type has certain things like maps you don't necessarily need it directly kind of concern yourself with those but it gives you the ability to translate between a currencies type and a prototype and we'll kind of see how we can we can use that it allows us to construct currencies from strings or from numbers it's it's a very very useful thing so let's go into our service so our service is the implementation of our currency so again kind of just G RPC is going to code Janus a bunch of things and what we need to do to be able to satisfy that and to be able to create Archie RPC services is we implement the interfaces which are generated for us so sorry in that proto PB file and that's what's in our server currency cool and and I'll put the link there for that video so please go back in and can have a look through that if you've missed that one so currency is very simple we have this RPC service that translate through to go function and the go function has a rate request which is a strut you have the base and the destination which are of type currency and we're going to send a response and the response is a very simple floating-point number and we've we've just kind of hard-coded this information for for now what we want to be able to do is we want to be able to call this service and I want to be able to call it from my products API because my products API is gonna have currency as an upstream dependency it's going to allow me to do things like conversion of the currency I'm going to be able to specify the return of a product and have the price automatically convert it for me so in order to do that what I need to be able to do is I need to be able to construct a client which allows me to call the currency service so how do I do that well there's a kind of a number of different ways to do it but what we kind of really need to do is we kind of have to just use that proto file and we're just gonna use the clients that's generated automatically for us and that should I say should it should be fairly straightforward it might be straightforward let's hope it's straightforward we can test it anyway all right so first things first so we need to create a currency client so let's let's dig into that so the currency client in G RPC is defined in that auto-generated filed currency dot PB don't go and you see it up the top here somewhere it'll be further down there we go so we're defining the rate requests and where are we currency client it's an interface so the interface has a method go method get rate takes a context so everything in terms of a Geo PC client call will have a context it allows you to do things like timeouts and counsel and stuff also be able to pass additional metadata across and you can specify things like the rate request object and also these G RPC call options so we'll take a look at those in a second and that's gonna return us a rate response object or an error so we create a currency client using the new currency client method on the proto now the the kind of the one of the real benefits of one of the things that I really like about protocol buffers and we'll we'll kind of I think see this more when it comes to testing is the ability that we can actually we've got clients sort of as interfaces here so it's it's gonna make it really easy for us to to be able to write some tests and do some stuff like that and I'm gonna be excited to kind of show you show you this stuff when we kind of get round to doing this but for now what I want to do is I want to kind of define that proto for you so we're going to back over to our main goal and let's just create our currency client here and then we can pass it into our handler so what I'm going to do is I'm gonna reference that code generated file currency which is this one here and I'm just giving it the whoops oh darn you it removed it because I haven't reference used yet I'm just gonna use this alright so let's create a new instance of our client so create client and we're gonna use proto's proto's dot new currency client so this takes a gr PC client connection interface okay so what is what is this all about well what we need to be able to do is we need to kind of connect to a particular service so in order to connect to a service what we're going to do is we're going to instantiate a gr PC client connection and we're going to pass it to our currency client so just bouncing over to the gr PC Doc's here we can kind of look down here and these documentation form the GRP cio is incredible really really good but you know let's have a look at this so connection error geo PC dial this is what's creating that that connection okay and then we're gonna we obviously need to close a connection now has body kind of seen this pattern before well what about net connection so see if we can have a find find of that there but well assuming about the wrong way look at this we've got net dial and this is a kind of the way that you would create a just standard TCP connection if you look across here at the GRP see pretty much exactly the same very very simple pattern straightforward I like that GOP see people are good people they're playing on the fact that Nick doesn't have a great memory but we need to define that connection so let's define it so we're going to define it we're gonna say G RPC dial a GOP C dial what does that take well GOP C dial is going to require us to specify an address now we've got our server running here and it's just running on localhost and well at a port that I can't remember I think 1992 so that's just define that localhost 1992 yep okay local host 90 92 and well I'm just gonna panic if I get an error because I'll clean that up later because I want to go through kind of get you going with this but then we're gonna look at actually doing some big refactoring in this service which I think you'll find well I hope you'll find useful I always find refactoring some of the most insightful code that I ever write but what I've got the client what I do is I pass that client to my connection okay and this returns me proto's currency client and that's an interface so I'm just gonna call this currency client now now I've got my currency client I'm gonna do what I want to do with this so what I want to do with my currency client is I want to be able to use the the kind of the the currency conversion every time I call one of these these API endpoints so for the moment what I'm gonna do is I'm just gonna kind of well let's let's think where's a good place to put it I think what we'll do is we'll we'll just only want to get real active I think what we'll do is we'll just put it here we'll just create it up here and we're just gonna pass it through to the handler and we'll we'll kind of refactor that in a minute I just want to kind of get this thing going so we can see how it's used so new products that's gonna create us a new product I'm going to be proactive because I know fine well it isn't gonna find the import on that so I better do it myself and then in my new products I'm gonna specify CC which is my proto rotos dot currency client and there we go okay so now began on our product handler where we're doing this kind of constructor style injection which might feel kind of like weird at the moment but honestly it will be a hundred percent apparent once we start getting to testing why I'm following this pattern it keeps things right nice and clean nice and tidy keeps all of sort of the the dependencies isolated and that's always a good thing so we've got new products and we've now got our currency client which we can use from new products so if we take a look at our list single method then what we do with list single is we're going to get the product from the the data the data layer now what I want to do with this product though is I want to be able to change the exchange rate on it so let's just get the exchange rate so get X change rate we're gonna cut some corners here for the moment we're gonna come back and we're gonna refactor this what is our how do we get our exchange rate well anybody remember that we defined it currency service because we sure did and currency service we actually have a currency client called si si si si has a method called gap rate takes a context I'm not gonna kind of do anything fancy with context I'm just gonna pass a standard background and it specifies that I need a rate request so let's create a rate request proto's rate request alright and yep let me just import that okay so we need to construct our rate request our rate request is going to define what our base currency is and it's going to allow us to do a conversion into another currency so base currency now if you remember our base currency is type is a nino of type currency so proto's dot currencies is an enum what gosh I'm just gonna bounce it over here is it type it's in 32 but we need to be able to construct that from a string so in order to construct it from a string what we can do is either use these maps here to do the conversion or something like that well I think we'll just use the map right so the kind of the easiest way to do that though is we're just gonna construct we're gonna say proto's dot currencies because it has to be of type currency so we can we can do it like this and then we can just wrap the integer value which we can get from that map so we can save proto's dot currency currencies value and it's a map so let's say Europe you're the Euro you row get that right Jackson and what we need also need is the destination currency and the destination currency we're going to set up and exactly the same way so proto's dot currencies proto's dot currency value and let's say G B P like that okay so now we've constructed our rate request which is a property here so just recapping there very quickly so we are going to call the GATT rate method which is on our currency client or currency client has been automatically generated for us because we are using a G RPC framework and go and we set out that in our currency proto currency proto when generated creates this currency dot PB so it's all like or Co Janet stuff so we constructed a new currency client using the address of the service and creating a gr PC connection and we've set that up and we're using it here so we're going to call get rate and this is gonna return us a rate request so a rate response so we can say rasp and error it's gonna be like this so we're gonna say if error is not equal to nil and I'm gonna do special episodes purely and simply on gr PC errors because GOP see errors are slightly different geo PC runs over HTTP too but you are not going to be dealing with HTTP error codes like 200 501 in the same way so we need to kind of things slightly different but if we got that we're just going to pre de el door print line and we're gonna specify error and we're gonna have all the regrets that I didn't use a structure error logger in the beginning of this and all of refactoring to do when it comes down to it era getting new rate and then let's just get the era and we need to return an error message because we're taking that restful approach so I'm just gonna return a generic error okay and then we've got this like so now what we we can do now that it was successful is we've got that response and the response has the rate which is going to be our conversion number so we can take our product and we can get the price of our product and we're going to set the price of our product to the price of our product times our rate because this is going to give us a ratio so for example if the base currency is euro and we want to convert into GBP there's I don't know something like 1.1 1.1 euros for every pound so that will get the rate here will be a value of 1.1 so what we're gonna do here is we're mutating that we're just saying give us our price which is in our base of euros and we're just gonna return the new price which is the old one times the currency exchange and that should work if you say a dozen Hail Marys or whatever deity that you believe in because we're gonna need the help hmm okay wearing we're doing good we're getting there I just missed one last thing so one hurdle over go mod love you next thing main dot go so a new currency client we've got a line length 33 because we are creating a dial now dial also takes options so am i bad here we're missing an option so by default gr PC is going to use HTTP to HTTPS by preference wants to use HTTPS now with with go there is a kind of patch on HTTP 2 that means you can use it without HTTPS I would always recommend using secure transport in your services but we don't need to worry about that now what we can do is we can use these special G RPC options and we can use G our PC with insecure so what this is going to do is this is an option which says don't worry about any client or server side certificates you're all good as plain text again not something for production fine for our local so we have our products API running so let's create a new one of these and we can do kill localhost and 1990 and what was the URI again its products and the name of the products we're gonna get single curl kept products one so you can see there that we've got our product and the price is one pound well one because we're using GBP we using conversion 1 2 to 5 let's just quickly go back to our GATT service there and I'm just gonna comment out that line and we'll kind of I just want to show this is working so we've got that running again and we're gonna I killed the wrong service currency rerun our API 69 run that there rerun our curl and you can see to pound 45 and that's because the rate which is coming back from the currency service well we hard-coded it to be 5 so just putting putting that all back in there rasp at that line let me just log that out so you can see what the the object actually looks like l dot print F and I'm just gonna do rasp and I'm gonna do this - where's my percentage percentage hash V and rest just change that back up here and let's see if I can remember to kill the right service there but you can see they're also in the log that we've got that handle request let's rerun okay we've got our service running again curling our service this time 125 because it's that rate of 50 we've just got hard-coded you can see that the currency service there is is all working and you can see the object that was returned from the G RPC service such as rate response and you can see all of that that kind of that object structure just kind of put out but that's kind of the very basics of starting to use a gr PC client I'm gonna start - I think in the next episode going through and I want to show you some maybe let's do something a little different let's go and refactor our code a little bit let's tidy it up and let's implement a proper rate service let's go and fetch some data from the European Central Bank and we'll get some proper rates and we'll build up ourselves a proper object with real data and we'll add some nice endpoints which allow us to to kind of specify the the currency using those restful principles so we're gonna create our currency we're gonna be able to do our curls but what we do our curl this time we're gonna be able to say currency and say G B P so we'll be able to allow our users to specify which currency that they want or response in and we're gonna go through and we're just going to go through and tidy that up and I think it'll be fun I hope you've been enjoying the the kind of the show today and well it's a little bit kind of back to normal as normal as normal is at the moment but I want to thank you for watching I want to say to you if you do with like the content and you're liking things please like and subscribe it really really helps us leave the channel until next time I will bid you goodbye [Music] you
Info
Channel: Nic Jackson
Views: 9,920
Rating: undefined out of 5
Keywords: go, microserivces, gRPC, golang
Id: oTBcd5J0VYU
Channel Id: undefined
Length: 33min 9sec (1989 seconds)
Published: Thu Apr 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.