How To Build And Structure A Microservice In Golang?!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what are people of today in this video I'm going to show you how to create a microservice how to create and structure a microservice in golang so we're going to structure it that we don't have a mixture of business logic into our logging into an instrumentation I'm going to show you how to uh Implement different transports without the complication of all these things being mixed together so we're going to make a nice separation of concerns but before we start if you like the videos I'm providing to you subscribe to my channel give me a thumbs up ring the notifications Bell and put a question in the comments so we can boost this video into the YouTube algorithm and jump into the Discord so you can be educated 24 7. so without any further Ado let's go so the first thing I'm going to do is basically what you're going to make is we're going to make a cat fact service right so what is a cat Factor cataract is basically a stupid fact about a get and it's just an HTTP get the request we're gonna do and we're going to make that a service uh a very simple small microservice because that's what they do they just do one thing and they do it in a very nice job all right so the first thing I want to do is uh we have this mean I'm going to make a service go file so I'm going to say it's going to be package me no need to make folders here and so what you're going to do is we're going to make a service which is going to be an interface right very important we're going to make an interface and that interface is going to I'm going to say get vect or get get fact maybe and that's going to take in a context very important always provide context just context is what's going on it and it's going to return I think it's going to return a pointer to a cat fact like this and an errors right of course we don't have a cat fact but that's no problem let's really quick make that uh we're going to say e types dot go right I'm going to say package main type get fact struct I think it will return effect which is going to be a string and we're going to say here Json fact smaller case right that's the only thing we need to do let's go back to service right so this is our interface very important and now we're going to actually make or get fact service which is going to be a structure of course um in our case the structure doesn't need to have any state attached to it so we can leave it open and we're going to actually implement the interface the service interface we're going to say func I'm going to be a service uh a cat fact service and we're going to say um get get fact which will already receive a context uh context context like this will return a cat fact and another just like this all right so what we're going to do is you're going to say response at is going to be HTTP gets I think it's uh https get facts Nina slash fact could be wrong we will see what you also could do is basically uh because we are hard coding this um this URL right so what you could do is you could say URL it's going to be a string right and then if you say funk new get fact service it's going to take in a URL it's going to be a string and it's going to return a service right you could say it could return a cat fact service but we're going to return the interface so we're going to say um return and Cat fact service and the URL is going to be the URL right that's that and then of course heat you could actually um say that it's going to fetch from the URL right if there is an error and that's not a nil we're gonna return nil and an Eros and then here we're gonna say that the cat fact or facts whatever it doesn't really matter is going to be a pointer to a cat fact and I'm going to say if it's going to be Json new decodage we're going to decode the response body and let us refresh response body close we're gonna decode that stuff into our fact and if the error is not nil we're gonna basically return null and the errors just like that and otherwise we're gonna of course I need to return here like this and then here we're gonna basically return the fact and the mill right no error right so that's basically our cat effect service pretty fine so now we could say yeah but we need some logging right it's very important in a microservice that you have some kind of logging that we know what's going on so how we do this is um instead of a lot of people are making these logging sheet right but that's bad that's a bad practice what we're going to do is we're going to make a logging service Dot a login service let me quickly see login dot go boom I'm going to say package main right I'm going to say type logging service which is a structure and that is going to take a next and that's going to be a service right so we're going to basically make some kind of a middleware chaining middleware decorated stuff and that will allow us to basically separate the business logic from logging the business logic from instrumentation uh separation of concerns right so we're going to say funk new logging service we're going to take a next it's going to be a service it's going to return a service I'm going to say return and logging service and the next is going to be the next which is going to be or another service right now we're going to implement the interface so we're going to say s logging service get get fact I guess we're gonna be a city X we're gonna milk mug that mock that I'm gonna say context gum text context and in this case we're going to do something special we're going to do a named return and you will see why we do this so we're going to say that effect is going to be the pointer to a cat fact like this and then we're gonna have an edge which is an errors right so in golang you can do something like named returns and uh I it's not about practice to do but in this case it's gonna it's gonna be amazing so the first thing we're going to do is basically at the bottom we're going to actually return I'm going to say s next get get fact right because uh this next thing in our logging service is basically uh also a service and a service implements the get get vector function so we can say get get fact are we gonna actually I need the CTX right we're gonna pause in the settings here inside of that thing and then we're gonna make a deferred funk like this uh and we're gonna say it's going to take in a Time actually it's going to take in start which is going to be a time time we could say time now and then we can do something like fmt printf you could use your logger uh you could actually make a logger inside of here right you could say for example a logger and use your own logger but we're going to do something simple going to say print f and we could do something like percentage S no V uh uh and then we're gonna say you could say fact is going to be this and then we're gonna have the errors which is going to be this and I'm gonna have took which is going to be I don't know maybe V or D I have no clue so what am I going to say the fact is going to be the fact right and that's why we do this name returns right fact here Factor and the arrows right that's why we have this name to return um Eros right and uh took is going to be time since start right so it's going to measure how long this function took because we have distort in this deferred thing and we say time now so we're gonna can we can perfectly do time since start and then this function is going to defer but basically means that this function is going to trigger when this function basically returns right and it will capture all the values uh the names the names returns it's a it's a special trick it can be complicated but it's one of the best tricks you can ever do it's one of the most important things you you should actually remember because this pattern is basically it's insane and then uh I think only 90 um only 10 percent of the golden developers know know that stuff right but now you know also right okay so that's our logging service that's fine right the next thing we need to do if you go to main right if you go to main for example then we can say um you can say the service right and that's going to be a new uh get back service I'm going to say the URL is going to be https get effect um dot Ninja slash fact right that's the URL and now we're going to say again you're going to say the service is going to be a new logging service and we're going to put that other service inside of it right and now we have a nice A little service so we can actually test this already right we could test this uh the microservice already and then we need to provide some kind of a transport right because people are going to access our microservice right and most of the time uh a lot of the time microservice is just over a Json transport Json HTTP but especially if you have a lot of microservice that need to do some intercommunication with each other a lot of companies in the industry are using Proto buffer grpc drift and all that other transports that have a very um and a better binary serialization format so it's going to be faster because Json is it's slow right slow wish so what you could do is basically say for example uh facts error is going to be SVC get get fact I'm going to say a context to do or something because we don't care right now right and then we could say f is uh not mil OG fatal of the ad and otherwise we're gonna just fmt print f percentage plus v man what am I doing man plus V new line and we're gonna say defects inside of this thing right I'm quite sure it's gonna work next thing we're going to do is basically make a make file that's what's very important I'll do this all the time it's a very good practice to make a make file in your project and we're going to say build uh yes built it's going to be go build minus output bin effect right or cat fact whatever and I'm going to say a run which is going to be it's going to build first right and you could prevent these other thingies so it doesn't actually log out um the command right then we're going to say dot slash bin effect just like that and then you could also do a test or something right and that could be equal test uh maybe even both and the whole ship bang right uh the question is going to work make run right you see it already is working uh of course we have uh so we see our logging right this is coming from the logger right and it's telling us this fact what I'm going to do real quick is to make this a little bit better um so you see this is our cat fact and I don't need to be in here right I want to change some stuff for the logging so it's all only going to be in the logging what I'm going to do in this fact is going to be a string right and I'm going to say it's going to be the fact uh facts right and the error is a problem that should be a percentage fee and D I know not quite sure uh took is fine and of course what we need here is a new line right and we can uh make run once again boom and then you can see the fact an adult line the error is nil right you could see there is no error so if you log that you could have final elasticsearch you can basically filter on on errors but it's nil and it took us 315 milliseconds right uh what is this we are printing uh another fact oh yeah I see it it's heat right it's because in main heat we are printing the fact out but we also see our logging there so hey all working fine uh but now what you need to do is provide an API so we're going to say because people need to access it right so we're going to say uh API go uh no need for folders right all in main all in main the only folder you can make as a client right as a client and maybe we're going to do that uh depends on the time we have left uh so we're going to say type you could say API server uh which is going to be a structure right and that's going to hold the service and it's going to be an interface right so why it's an interface because if you want to make a new service for example get fact service 2 V2 you can make a new one you don't need to touch the old one and you can just plug it in because it's an interface right easy peasy API server which then we're going to say funk new API server and it will take in a service which is going to be a service all right it's going to return a pointer to an API server like this are trying to be as fast as possible um gonna return an API server like this and you're gonna say that the SVC is going to be the SVC here perfectly fine um yes then we're gonna say funk s API servers handle get get fact which is going to be a w HTTP response writer or HTTP requests like this right fine all right let's save this let's move this mouse cursor out of the way because we don't use that and I'll get cat fact so what we're going to do is basically um we're gonna say that effect and the error is going to be s Service get get fact we need a context so we we could make our own context but we're going to say context background real quick because you can already see it coming right you can you can use this context for for cancellation because sometimes this cat fact can take too long so you could use a context with timeout with cancel with deadline to fix that potential problem but that's I already made a video about that so we're gonna say FR it's not mil right we need to provide an error right so we're going to say real quick I think we're gonna make a function write Json real real quick it's going to be http a response writer yes you're going to have a status which is going to be an INT and we're going to have a v which is going to be in any it's going to return an adult and we're going to say first of all we are right header it's going to write a status it's going to be S then we're going to say we uh headers at content type what's going on it application application Json and I'm going to say we write actually we don't we're going to say return uh Json new decoder encoder actually w encode V right that's the thing in this case you're gonna return here then we're gonna say I'm going to write Json it's going to be the W the status is going to be I have no clue HTTP um I don't know unprocessible entity or something forgive me my bad status returns status quo returns but hey and then uh we're gonna it's gonna be an adult right so we could say a map string you could make your own out of type but then we're going to say it's going to be a map any I'm gonna say Eros which is going to be add or so it's a string and it's all fine right okay then we have the spec so the next thing we're going to do is basically um we're gonna Json actually you could write Json right right Json here you're going to say uh W is going to be an HTTP status okay and we're gonna return the facts so it's going to be encoded and you should actually catch the error in right Json in my opinion it should basically wrap your uh handlers with an errors and uh decorate them so you can return Network and all that stuff and handle all the errors in a single place but that's out of scope so we have a nice uh handle kit or nice Json transport the next thing we're going to do is say s API server start right I'm going to turn an errors and we're going to say HTTP handle funk and you don't need to do this here you could do it in your main or something but hey as a handle get get fact and then we're going to say return http listen and surf we need to have a thing we could say the lesson added Heat it's going to be a string I'm going to say a lesson address and then nil so it basically means stop me in HTTP server listen to that address and there is no handle because we already provided the Handler all right that's fine then in our main um we have this fact we don't need the shenanigans heat what we could do is basically say that the um API server is going to be a new API server we're gonna pass in or nice service we wrapped Heat and then we're gonna say uh lock fatal API server start and we're gonna say three thousand let me open up tender client let me make run right so it's running do I have a tender client real quick tender new requests um http localhost HTTP the colon colon slash localhost 3000 just like that I think do we need facts it was just slash I think yeah and it's working fine you see wait it's actually uh in my um in my screen here how can I how can I fix this I can't where's this full screen that's nasty something is going on all right but anyway this is basically the response uh so we and then we can see actually in our terminal uh we have this nice logging right it took us 461 milliseconds let me see how can I make this this thing better uh so we can you see each time we do a request now you see the logging perfectly fine and um how long it took so basically our service is complete so uh let me open up the folder structure so you can see no big of a deal uh don't uh structuring a microservice is a very small piece of code everything it should just do one thing and one thing perfectly uh with instrumentation with logging and all that stuff and make sure that everything is has separation of concerns don't mix up your business logic with uh the transport don't mix up your logging with your business logic and all that stuff that's what we did here uh what you could do here is basically make a new folder which is basically a client right you could make a client that's the only folder you can actually make and in this client you could um provide a client for your team right because people will need to interact with that microservice and uh instead of if they are also using go if they're using python or something else which can be perfectly fine then they will need to make their own client and python of course it's very simple it's just to get a request that they need to provide and and all that stuff but if your team is also using go you could make a client and you could put it inside this so people can actually import your client into other services so they can just call uh this this cat fact and have the correct types and everything right another thing that could be a problem are the types if you make this client right because if you have a client you will need to have the cat fact type we have in here right so uh then you should basically make an other folder with types so people can also import your types in their projects right but don't over engineer your folders only make new folders when you need to do one all right and the previous use case could be a very good time to make these folders if you need to export these to your team right so that was it if you like this video consider subscribing to the channel give me a thumbs up leave some questions in the comments and jump into the discourse if you have more questions and I'm happy to help you out for the people that want to level up I also have a patreon page look I highly consider just checking it out what you can find there and if that's something for you if you really want to level up as a golang or rest engineer or blockchain just to build its Quant trading whatever just check it and uh maybe it's something for you right thanks for watching and I'm looking forward to see you in one of my live streams or future videos peace out
Info
Channel: Anthony GG
Views: 48,259
Rating: undefined out of 5
Keywords: golang microservices, microservices in go, learn golang, programming, microservice, golang tutorial, golang, go, microservices, building microservices golang, golang rest api, microservices architecture, go programming tutorial, go language, golang tutorial for beginners, go programming language, learn go, golang programming, microservices tutorial, golang tutorials, go lang
Id: sqj4UzN4OpU
Channel Id: undefined
Length: 23min 3sec (1383 seconds)
Published: Thu Nov 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.