a DevOps engineer tries out Java Springboot for the first time

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so the last time I worked on Java was many years ago I think at that time I learned how to print a Christmas tree using a for loop that is about how far I took Java now I get a lot of questions about spring boots and Java micro-services especially in the world of DevOps so I've worked with many various complicated programming languages but not Java and I believe as an engineer you should always understand the strengths and weaknesses of each programming language so I wanted to see how easy it is to get a spring boot application built and running in a modern container based cloud native environment what I love about spring dot IO is that it has a ton of rich information on where to get started and as a new developer you have a ton of options on where to get started and what path to follow and this is something that other programming language frameworks really lack so the getting started guide is great since it tells us exactly what options we have so I'm gonna go ahead and build a restful web service since we're building a micro service so I'm going to go ahead and click on that and then let's see what you will build ok so this is obviously functionality that we're gonna build and what do we need so we need a whole bunch of stuff installed we need Java we need maven and all of these things so since we have to build all of these things the first thing that comes to my mind is docker so let's get a nice little isolated and develop an environment up and running so since the getting started guide says we are gonna need Java what I've done is at the roots of my repository I've created a spring boot Java folder this is gonna be where I do all my work so let's go ahead and change directory to Springwood Java folder and you can see I have a docker file in here so I went and created a docker file and I'm going with a newer version of Java so I just picked open JDK 14 because normally with a dev environment you install Java Java development so what I've done is just pick that one call it dev and that's our docker file so what I'm gonna do now is I'm gonna say docker build dot dash T I'm gonna tag this as spring Java go ahead and build that and there we go Dave environment is built so let's go ahead and run that so I'm gonna say docker run - IIT to get an interactive terminal - - RM to get the container removed when I'm done with it and I want to mount my local filesystem into the container so I'm just gonna say amounts of volume and I'm gonna mount the volume into the container into a folder called work so that makes it really simple that's a working directory I'm gonna set that as the working directory and then I'm just gonna run my image which is spring Java and I want to bash terminal so there we go we're inside the container so we're now inside the container inside this Java folder so we should see the docker file if we do LS minus L we see the docker file is right there so now we can act access to the full Java development kit so we can get started building a restful web service what we need so we should have all of the stuff now defined in our docker container so our dev environment should be up and running all good to go so how to complete this guide like most bin repetitions and stuff from scratch so this is the cool part it says we can start from scratch move on to the spring initializer so when I was looking at this this is really really awesome and there's actually a UI pretty awesome UI we you can define metadata about your application you can also add all the dependencies and spring will go ahead and provide you with a zip file so we go to add dependencies and we select like an web so this is if you want a web server in your application but there's also a ton of other things that you can do like there's a security section here you can build like an OAuth server or you can have like a sequel integration or various other storages as well as no sequel message queues iOS ops testing frameworks cloud-based stuff I was looking at AWS and Azure stuff there's some Google SDKs and things as well so you can define all your Linc's in here I wish more programming languages had this kind of features which is pretty awesome and then you can select like your we want maven project we want Java with this is the version we want Java 14 and when you click generate it goes and it downloads the source code to your system so we can go ahead and extract that source code compile it and have an application up and running but there was something else I discovered what I found was that this whole UI has an API behind it so there's a Python utility that makes it really easy to interact with this API so I'm gonna say app get update and I'm gonna install Python 3 which is gonna give me access to that utility and I'm also going to install unzip to interact with a zip file and then I'm gonna use pop3 to install the utility it's called HTTP pi now this is pretty amazing with this utility installed I can say HTTP and I can point it to start at Spring dot IO and this is gonna give me all the options so you can see the whole spring initializer it tells me all the parameters it expects so you can say the application name the base directory the dependencies in a comma separated list you can also define the description things like the package name the version of Java which is important and the language as well and then we go down you can see this is the list of dependencies that we can select from and it has a massive list all the different cloud providers that we can interact with different data backends Redis and sequel and all those both clients as I mentioned before there's some WebSocket stuff here which is really cool we're going to be looking at this web one to build web and restful based applications and it gives you some examples on how to get started so following those instructions what I've come up with is HTTP we point to system startup Spring dot IO to the started of zip file and what we do is we say our application name is example app my dependencies as web site on a web server running the name of my application and the version of Java that I want and if I go ahead and run this it goes ahead and does exactly what the browser does so it's like fully automated and just gives me like a demo dot zip which I have right here and I can run all sorts of Linux stuff and now I can say unzip demo dot zip and we can go ahead and extract that and check this out all the maven dependencies stuff all the source code including my example app is all here ready to go even generated some tests it's also got a getting more on what files to not check in to get this file looks like our dependencies XML file and yeah we're good to go all the files are here so we can start adding this to our container and build up a docker file and try and see if we can compile this so I've been reading on spring dot ayahs website going through all the guides and I found the spring boot docker and when I was looking at this they have a basic docker file and they the thing that struck me and that was interesting was that all the getting started guides do this they all give you an executable jar file and they give you the maven installation command so basically what that allows us to do is take the source code that we got from the spring initializer we're able to compile that using the executable the command the maven dependency that they gave us and produce a jar file and then we can take that jar file and bake it into our application and just started our so if we take a look at the code and we can see that may may even binary was given to us and because we're inside the docker file we can go ahead and just say maven install and that will go ahead and looks like it is going to go and download the internet so it's going to download all the dependencies into this docker environment and it's gonna compile it and run the tests and then we can start the application up and see see how it goes so it looks like we have a build success so once that is done we can also use the maven binary to run the application so we do it like that [Music] and there we go so starting example app so there's a process ID it using a tomcat server running on port 8080 very interesting and it's up and running so the application doesn't do anything there's no real logic if we take a look at the source code we've got main Java example - Java so this is just a little hello world basic application that it gave us and we were able to run it so what I'm going to try and do now is pack all of these commands and things that we run into the docker file so the docker file becomes our source of truth that developers can compile and run and deploy anywhere in a cloud native environment so now that we have the application up and running we want to work on the docker file so the docker file is the source of truth that means any new developer that wants to work on the project can look at the docker file and all they have to do is say docker build and that'll allow them to do all of these steps that we've just done but without having to know how to compile the source code so what I'm going to do is just exit out of this and we're going to start with a few very basic things so we're gonna say work directory and let's make it slash work since that's the working directory that we've just tested so we define a working directory inside of the container image and then what we're going to do is we're gonna say copy and we're gonna copy our source code into the slash work and like that so we copy every all our source code into the working directory and then what we do is let's run that command that says dot slash maven install so we'll take this command here and we'll run that and then what we'll do to start the application is we've created an entry point and we'll create an entry point to tell docker how to run our application using the spring boot run command so this is a very basic simple docker file that we'll go and build our application and then run it so let's test that out so I'm just gonna exit out of the container and now I'm back on my machine I'm gonna I'm gonna run the same bolt come on just docker build dot dash T and I'm gonna still call this image spring Java but this time it's gonna automatically set a working directory copy the source code run maven install and then do all of that stuff you know run the buildin test and then basically define an entry point okay so I had to change the entry point I realized that the maven install command actually gives us a jar file so if we take a look in this target folder we can see demo dot Jara's is output over here so all I needed to do was to say chmod to give that jar file execution rights and then I just tell Java to run that file and that can start up our executable application that is very similar to the getting started guide of the basic docker file where they tell you how to execute the jar file with Java so that's what I've gone ahead and done and all we needed to do now is build that with docker build then we say docker run and this time I'm going to expose port 8080 and if we run that we can see spring boot just launching our application and then if we take a look in the browser and there we go so we're getting a 404 error page that's because our endpoint is not found we haven't defined any end points so we can easily solve that by following the spring boot guide on creating a simple web application which means we just need to create a hello controller and basically copy this the stuff out of here so all we need to do is go to our source in our demo create a new hello controller dot Java and then paste that guy inside of there this is can be our message hello world we've defined our hello controller I've gone ahead and built and run the application again and you can see this time when we go to localhost 8080 we now get a hello world message okay so now that our application is running there's one pretty frustrating part about compiling this that I notice every time I do docker build I run env a maven install the problem is when maven installed runs it pulls all the dependencies over and over and over and this kind of ruins Dockers ability to cache and also makes our full time really long especially if you're changing files in your source code all the time like I was so there's a couple of things we can do to fix that but we'll need to optimize the docker file alright so this is actually pretty straightforward so what we want to what we want to do is rather than copy all the source code in we're gonna set our work directory and then we're gonna copy in three files so the maven binary we're gonna copy the maven directory where it caches all its files and we're gonna copy pom not XML that's all our dependencies listed in an XML folder and makes it mal fast so what we gonna do then is we then run the maven command so there's a command called maven dependency go offline which will pull all the dependencies kind of like how maven install does it but at all it'll pull it all once off and what will happen then is we can then use docker and image layer in to cache those dependencies so that'll help the build go much faster so what we'll do next is then we'll copy the remaining source code files in and then we'll run the maven install so I've go ahead and save that and what we want to do is do another build now this time you'll see that all the dependencies are being downloaded so it's gonna take a while for the first time but then hopefully what will happen is all of these dependencies will be cached in that folder and since we're not changing that folder structure and we're only changing our code docker we'll catch this layer and there we go so once that is built you can now notice notice if we rebuild it look how fast the builds it's like lightning fast so in order to make our application cloud native so we can deploy it to any cloud provider will be using kubernetes so if we take a look at the folder I've created a folder called deployment and inside there I have a deployment llamó now this is how I tell kubernetes how on my application to be deployed so we have a type deployment file and what we do here is we tell kubernetes we want to replicas of our application running so two copies and I've tagged and pushed my image to docker hub using this instead of using spring Java so that's the image we tell kubernetes what we want to run and then we say we want to run it on port 8080 because that is the port that spring boot will be running and then we tell kubernetes how much resources we want so kubernetes can make smarter decisions about scheduling our applications onto compute and then we also have a service dog Yammer and this is basically we describe how we want kubernetes to deliver traffic to our application so good Modena the cloud native means we can deploy it to any cloud provider and if we put type load balancer in here that means kubernetes depending on the cloud provider we deploy it to it will go and issue a load balancer for that cloud provider so that makes it very agnostic you can deploy it anywhere because we're running docker for windows here we'll get a local host load balancer so what I'm gonna do is I'm gonna create a namespace called example app and then inside that namespace I'm going to apply these deployment files so I'm gonna go and do that that's gonna create a deployment gonna create a service of type load balancer and then what I can do is I can say get pods so we can see we have two copies of our applications running so kubernetes has gone and created two instances for our application and then we can also say cube CTL get service and we can see it created a load balancer type service now there's multiple ways we can expose traffic in kubernetes we can use an ingress controller which is like a proxy load balancer basically an API gateway or we can just go ahead and use a service of type load balancer which will make kubernetes issue a load balancer in the cloud or we can just use a virtual machine port to expose the traffic so in this case I'm just gonna use type load balancer which is going to give me this service running on localhost and then I can go into the browser and I can see my application running on localhost in kubernetes so I hope that gives you a little bit of a different perspective when it comes to building cloud native applications now I need you to let me know down in the comments what you'd like me to cover in the future would you like me to pursue java and spring boot would you like to see more building deployments maybe something around networking or security whatever it is let me know down in the comments below and like and subscribe and until next time peace [Music]
Info
Channel: That DevOps Guy
Views: 1,961
Rating: 4.9574466 out of 5
Keywords: node, nodejs, react, angular, frontend, dev, developer, web, api, docker, linux, reactjs, bootcamp, code, coding, visualstudio, vscode, visual, studio, apps, ui, webdeveloper, kubernetes, windows, devops, learning, courses, k8s, java, spring, cloud, javascript, springboot, microservices, micro, sun, jar, jdk, jre
Id: 7-n6E4d51M0
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Wed May 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.