Introduction to microservices (Ep. 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome to building micro-services with go I'm Nick Jackson and I am overjoyed to see what so what is this gonna be well what would want to do with this channel and this kind of this city the videos is I want to kind of show you how easy it is to build micro-services and go so the idea is that over the sort of the weeks and probably months what I'm going to do is I'm going to kind of go through all of the concepts of all of the kind of the things that I know about building microservice architecture and specifically how you you apply that with go so I'm going to teach us hands-on I want to be able to provide you with code samples I want to be able to show you coding you maybe need to know a little bit ago I mean I would I would recommend it but you know I can kind of teach a lot of that stuff along the way it's a pretty sort of simple language so I hope this is not too much to pick up and this first episode so what I want to do today specifically is I want to show you like just how easy it is to build a micro service and go I mean okay it's gonna be a pretty sort of like generic hello world example but I want you to sort of just start getting into the area that the sort of the see how to manage the structure and and actually just how good the language is for building this sort of stuff and I think we'll also look at development environments as well because they're quite a lot of questions about those specifically whenever I'm kind of doing this stuff and well I've changed some of my my setup as well so I'm actually running on a on a really new really new set up here so let's see alright okay so we're gonna do this on a Sunday I'm I was thinking about this this morning and I think I might try and also do a 30-minute stream on a Wednesday so maybe a Sunday in a Wednesday will we'll have to kind of see how that goes I'm just sort of working kind of through my schedules at the moment but but we'll sort of see so setup what do you need to to program micro services in go the answer is not a lot I mean what I use at the moment is I'm using Visual Studio code and I'm using a Windows machine that's right that might sound like heresy to some folks but I am using a Windows machine now let me explain why so Visual Studio code first Visual Studio code is a really nice tool it's free you can get it on any platform Windows Linux Macintosh it'll run on Chrome OS with crostini it's just a really nice versatile tool i i've recently switched over to using vs code from what was vim which I've been using for a number of years and the the sort of the main reason why I've started to do that is through collaboration so I'm working with my buddy Eric at work we're collaborating on things and with Visual Studio code if there's some really nice tools like live share which allows me to collaborate with Eric he can kind of use my editor share my I can see my screen in it it's not as one-way as if you were just doing a video share the other thing is that that actually it's really nice id there's quite a lot of productivity in here which yes it exists in vim but actually what I think I've managed to do is I've taken a lot of the best bits from my vim setup and I've moved them over to vs code so things like keybindings for navigation and stuff like that I'm using a lot of these in in vs code now and windows I mean I've I've literally been a Macintosh user for 20 years I've had a macbook pro for again for over 20 years and just recently I've started to move over to Windows and the kind of the rationale behind that is when I look at what was I actually doing on Mac OS well I was I was predominantly coding and I was predominantly coding in a terminal now the thing I liked about my course is that I had a terminal I had all of the capabilities of Linux but I had the ability to do that with a really really nice UI that I can run applications like Photoshop and stuff like that on but recently I've moved over to Windows because Windows has actually introduced Windows 10 with wsl to a lot of the capabilities that I need so I've got the ability to have quite a nice operating system which supports things like Photoshop but also the ability that I can use UNIX based terminal and I can install anything and in fact with Windows I'm using Ubuntu under the hood not BSD on Macintosh so it's it's actually even a little bit more compatible but the key thing is about building and writing micro services in go is it doesn't matter what system you're using you don't need to spend $3,000 on a macbook you could spend a couple hundred bucks on a Chromebook and okay yeah it might be a bit slow but you'd be able to get by if you have a Windows gaming machine actually that's perfect you've got more than enough horsepower and the tooling just works really good as I say WS L and W sell to really excellent pieces of technology and let's begin then so Visual Studio code I don't actually use very many extensions in Visual Studio code but I'm gonna give you a quick run through what I do use because if you want to follow along with this series maybe you want to set up the same way so what I use obviously is is go so I use the the gold language support Visual Studio code and that gives me things like in tally sense and it gives me the ability to debug which actually is a really nice feature long gone am i with format print line the it's very well supported and very well using you know like over two million users and well mike five stars really really great extension i use docker i use docker a lot in my development workflow the the docker vs code extensions quite nice it's got some sort of nice tools we'll look at this a little bit more when we start looking at container izing applications visual studio code live share I really really like live share it just makes collaborating really really easy and if you've actually got vs code live share installed on on your on your computer then you can you can actually collaborate with me so why don't I start a vs code live share here and I can share the link in the chat with you all oh well just paste that straight into public but never mind right so I'm gonna paste that into inter chat if you want to follow along with what I'm doing in your own vs code you can you can do so with with that link there you can just join join my live share um that's pretty much it I've got of like a theme that I use cuz well I want things to kind of look look nice but I don't really have a great deal of complicated setup in terms of what I have installed on my actual environment so if I'm running Macintosh or whether I'm using W SL or W we'll sell to with Windows or if I'm just using pure you know pure Ubuntu or something like that then I don't again have a lot of stuff installed I obviously have go so I'm running go thirteen one point thirteen point five which I think is the the latest release go is really really easy to install you know you can just grab that from golang.org and it's available for well pretty much everything Braz berry pies Windows Linux Mac OS you name it you don't really have to do a lot to install it you just download the package and pop it into a folder your executional path is the only two things that you you do need to setup or your go path and your [Music] go binaries the location so if I just take a quick look at my profile and I use that she'll for for my profile here then you can see that I'm just setting the path there to use a local bin for my go executable I'm setting my go path where my executable goes are always turning on go module I think that's actually switched on by default since 1:13 and I'm just setting my my go path so nothing nothing complicated there the instructions on the gold website will cover you for for all of that and that's pretty much it I mean that's that's all I really need and I can start creating pretty amazing microservices dope docker right I use docker a lot I really like docker I would go as far as say I love docker as a tool for developers and for running containers in production I think it's wonderful it's really really worth learning if you haven't already done so we're gonna cover a little bit of docker or in this and maybe in a few weeks or so but you know get by all means have a have a play with that got some coffee as well I do love coffee right so we have all of that out of the way we've we've got a little bit of intro on the sort of the tooling I'm I'm using I can see a couple of you there have joined the the live share as well welcome are not Neyman but let's look at what it takes to build a service so we're building a service in go so I'm gonna just put this in my my main go so the first thing at the top of a main goal is always going to be the package and this is package main because it's my my main executable what I need also is a func main this is the main entry point for your goal application now nothing much gonna go on there so nothing working in there but what I want to do is I want to create a web server so in go what I can use is the HTTP package and the HTTP packages actually got a huge huge huge amount of capability for for building your own web services it it's it's very very sort of comprehensive that the absolute basics of of creating a sort of a web service is to use HTTP listen and serve and this is a method a sorry a function on net HTTP and it takes two two parameters so we've got an address string and we have a handler so I'll explain these two so firstly the address so this is the bind address so I want to bind this to every IP address on my machine at Port 1990 so I can use this syntax here you see there I have : 1990 if I wanted to explicitly bind it to an IP address I could have typed something like this you know no not one so I could have bound it to local host on port 1990 but I just want every every IP and the the second parameter here is the the handler so we're gonna ignore this for now Edwin's asking if I'm going to talk about CI I will indeed I have a full example of running through continuous delivery so the the intention is that this is not a one-off this is gonna be maybe two 30 minute lectures per week and we are gonna go through everything we're gonna show you the basics we're gonna show you how to deploy your applications how to test them how to link them together how we can use G RPC as well as restful services billing caching reliability patterns some kubernetes and service Mashable all the things but in this one we're just looking at basic services all right so let's look at that so that is a web server that's literally all I need to do to create a web server and we can test that out so if I run this go run main go the server is running so if I go into another terminal window whoa oh I see what it's doing sorry it's completely throwing me their new terminal window there and I curl localhost 99e and let me just put that into verbose mode that's worked but it's given me a 404 because well there's nothing really going on so how do we make things happen how do I handle requests to that very very simple HTTP server well I do that because through the HTTP handlers so let's look at a really really basic HTTP handler and to create a really really basic HTTP handler again I can use the the net HTTP package and I'm gonna do HTTP dot handle func I'm going to give it a path and I'm going to give it a function so when a request comes in that matches this path it's going to execute this this function and the function signature is HTTP response writer and an HTTP request so we will look at these parameters in in a little bit but for now this is this is kind of what what's going to happen so I will look at some some of the gold frameworks as well well look we're just kind of covering the standard package in the sort of the first couple but I'm a big fan of a library called guerilla so we will be looking at gorila for a lot of the restful stuff we're also going to be looking at G RPC all right so I've written my handle func so let me just lock something so I'm just gonna log dot print line gotta be hello world hello world so what's gonna happen here so HTTP handle funk on a path and then it's gonna execute this function and it's gonna log dot print line hello world so let's just see that in operation so go run main go again and let's just rerun that curl alright you see there now I've logged it it's logged it out there so let's break this down so what actually happening under the hood so what is handle funk will handle funk is a convenience method on the go HTTP package and what it actually does is it registers a function to a path on a thing called the default serve MUX so what is the default serve MUX well the the default serve MUX is an HTTP handler and everything related to the the server in go is an HTTP handler so here we call this listen and serve method and again this is a convenience method what this actually does is construct an HTTP server and registers a default handler to it the second parameter is is a handler now if I don't specify something for this parameter like I've done here it uses default serve marks so let's take a quick look at those those things the default serve MUX alright so server so this is inside the go documentation so let me just paste that into the chat for anybody who's interested there so a serve MUX so serve marks as it says in the documentation is responsible for for redirecting paths so you map a function add a path and serve MUX will determine which function gets executed so in that HTTP package there is a default serve MUX and if you don't set anything else up this is the one which is used by the the other sort of functions such as HTTP handle func this convenience method so handle thunk so what what what handle func actually does and again let's look at the the documentation registers handle it to the given pattern to the default serve marks and then we've got an example so pretty much exactly what what we did there right so serve MUX is actually an HTTP handler it does sort of a little bit more than than than that oh if I go here it's actually in terms of an object an HTTP request multiplexer so you can create your own here and it's got methods like handle so handle will register a a handler HTTP handler for a path and what's a handler well handler in go HTTP is is just a an interface so any strut which has a method serve HTTP with HTTP response writer and request as a signature implements the interface handler so really really straightforward so what's going on here in handle funk is this handle funk is a convenience method it takes my function and it creates an HTTP handler from it and it adds it to the default serve marks when I then call listen and serve what's happening is listening servers using the default serve marks as it it's route handler and it's it's going to be using that to determine which code block gets executed when I want to have a request so let's add another handler so this time we can add a goodbye so we can do goodbye world and then let's run that again we're going to just run our code just using go run here I'm not compiling anything go run is more than good enough for testing and whoops then in my other terminal window I'm gonna curl that so curl localhost 1990 so you can see there it's curling and it's calling hello world as a log now if I do hello localhost goodbye again look at my log output you see it's doing now goodbye world and the reason for that is that when the path matches goodbye it executes this function when the path is anything other than goodbye so any it's a greedy matching so anything following that slash which isn't goodbye will match this function let me just quickly quickly show you that so if I do Nik is awesome because he is you can see it as hello world it's gonna execute here right okay so that's that's great so we've got hello world and we've got goodbye world couple of simple paths but it's only logging that's not not really really really useful so how do we do things like read and write to that request so let's look so we're gonna do that by using the response writer and the HTTP request so you can see here a response writer a response writer is an interface used by the HTTP handler to construct an HTTP response so it has a number of methods on it let's look at those methods so response writer ooh if I can get my browser to work come on browser work I'm zoomed in way too far he see response write a response writer smart Mariah there we go type response writer so it can do things like writing the headers the the response body status codes and things like that it's an interface the the actual concrete implementation is injected by the go server when it calls your function and then we have the the request so the HTTP request is is literally the HTTP request yeah right and so it has things like the path what the method is what is the the body which was passed to the the caller to the server and some various other things such as the HTTP version and and stuff like that so we can use these two objects to read and write to our functions so let's look so the first thing we want to do is we want to read from from from the body right so I'm gonna do that so how do I do that well I've already said that I've got this um this HTTP request so an HTTP request has a field called body and body is an i/o read closer so it implements the interface IO read closer what that means is it means that you can use any of the the sort of standard goal libraries for reading and reading from that that stream so one of those is obviously IO util dot read or it's a simplest way to read all from anything that implements IO reader so I owe you till read all will return me some data and it will return me an error if there's been a problem reading from the screen my um my IDE here is not playing nice by creating nice neat import statements it genuinely doesn't matter but my OCD kicks in so I'm gonna read everything from the body and I'm reading it into the variable D which is a data slice of data right so let's see that in action now I'm gonna handle this error in a little bit but first I just wanted to show you this in action select can let me just log I'm just gonna print printf I'm gonna say data percentage yes so I'm just going to print the data as a string and I'm gonna do D there oops there we go I'm gonna make sure that that has a new line on it as well so let's run our run our code and let me just sort that out right run our code so to pass the data to the cur with curl what I can use is I can use - - D what did I press to make that go into full-screen because I really do not want it in full screen well this is this is an interesting an interesting thing I've managed to make vs code go in full-screen mode and I don't know how to get rid of it so I'm just gonna close it right let me open that up again in not full-screen mode I apologize anybody who was on the the live share I've I've just kicked you off there but we're now at the end of this show will will get that so it for next time okay so we're gonna print that data out so let's run our application go run main go and again in our new terminal window we're going to curl verbose mode with some data in this past neck localhost 1990 so again if I look here you can see data Nick because this log table printf has written the data so the body which was passed as part of the HTTP request I've read it using i/o util read all from the request body and then I've just written that through log dot print line but what we really want to see is how do I write that back to the user so how do I do that well how I do that is by using the response writer so response writer is is an interface now what you will find is response writer has a method called write on it and that method right is bite into an error as though as to return things this corresponds to IO writer which means that I can use a response writer anywhere where I could use a response dot writer so for example format dot F print F allows me to use a row or an IO writer such as HTTP response writer with a format string so I'm now saying hello and again percentage as Wow I am now I can't type today can i where is where have you gone percentage as and my data so what this is now going to do is it's going to do exactly the same thing as I was doing before it's gonna print hello whatever is read from the data but this time rather than it printing to the log I'm gonna print it back to the response writer which means it'll be returned to the user let's run that again so we've got curl the boss mode I'm passing the data of Nick to localhost 1990 and you see there in the response it says hello Nick let me just get rid of verbose you see that hello Nick I could say hello world or I could say hello YouTube YouTube and in a really kind of simple very few lines of code that I am reading and writing to the response I've created an HTTP web server actually a relatively performant one as well I'm I have to kind of add and we'll we'll look at some of the more performance tuning aspects in the next show but really really simple really really really easy I haven't had to import anything or download anything or anything that other than the ghost standard library so let's just finish up this example and then we'll we'll kind of leave it for today but I want to kind of just mention one last thing there so we have this read all read all returns an error good go programming defensive go programming is never just swallow error messages like that so I want to handle that error so if error is not equal to nil then I'm going to do something so what am I going to do well I want to return an error message to the e to the to my users so I'm going to do response writer dot write header and what write header does is it allows me to specify the HTTP status code that I'm gonna send back to my to my to my caller so HTTP dot status bad request again the HTTP package has got all of these status codes defined as constants for you I really really really recommend the the documentation for HTTP stayed the HTTP package it doesn't take very long and I can have a quick browse through there but it's very comprehensive and you'll see a lot of really useful stuff back to vs code so I'm gonna write the head of bad request and then what I'm going to do is I'm gonna write an error message so I'm gonna say response start right and I'm just using the base methods here so I need to construct a slice of bytes and when you're doing like that with a string and I'm just gonna do so that and then I'm going to return that's how I could write over an error message to my server but actually goes got me covered because go has again it's got a convenience method in the HTTP package called HTTP error so if we take a quick look at that in the docs you can see that error replies to the request with a specified error message and an HTTP code it does a lot of nice things like sets the the the status code for me automatically in it'll also set some some some headers for the content type so I can replace this line of code quite simply with HTTP dot oops error I specify I'm a response writer my error message oops and my status code status bad request now I do still need this return statement because HTTP error doesn't terminate the flow of my application but it will let me just kind of return I'm just gonna return from this and I'm gonna continue with my next request and that's it that's that's literally how many lines of code there well we've got 27 including all of the imports that's all you need to do to be able to write a basic web server and go next show and I and I think I will try and do an episode on Monday I'm going to look at a little bit how you can structure your code because this is fine for like a really sort of simple hello world example but wewe really need to be thinking about structure because we need to be looking about testability and when we start getting into those things it's gonna be important to have good structured packages for your micro services so we're gonna tackle that right from the beginning do I have some let me see I think what I can do for you is provide you a code repo which is always gonna be useful so on github what I will do is I will continue to keep updating this this repo so you can kind of if you want to follow along at home you can do so and also why not kind of post up any any issues or questions and this github issue repo as well and then sort of between episodes what I can do is I can try and answer those things there I'll stick that in the the description below but it's in the chat there as well for for the moment I want to say thank you I hope this has been been useful I'm gonna try and keep these sort of shortened and concise but I hope you're gonna enjoy this series I'm gonna really enjoy teaching it and and if there's anything specific that you you would like then you know please please please comment down below and make some suggestions for things that you would like me to to cover but again just sort of recapturing my intention is I want to take you from zero right the way through to building relatively complicated multi-tier applications with the go programming language so Wednesday Wednesday I think we're gonna do some more so we'll probably do a little bit later around 8 p.m. GMT Wednesday and then on Sunday next week again back and for GMT we'll look at episode three thank you so much event based communication and G RPC I am absolutely going to cover by the series I will also in the description down below I'll put a little bit more detail on the things that I'm gonna cover across the full series and I'll do that just after this show but thank you for watching again like and subscribe that's the thing you got to do right I keep seeing this on YouTube like and subscribe comment for any subscribe anything thank you see you next time laters
Info
Channel: Nic Jackson
Views: 95,344
Rating: 4.9638691 out of 5
Keywords: go, microservices
Id: VzBGi_n65iU
Channel Id: undefined
Length: 39min 21sec (2361 seconds)
Published: Sun Jan 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.