Spring Boot Microservices Debugging with Intellij IDEA | How to run microservices in Intellij

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lesson first we are going to create and run two spring boot microservices locally from IntelliJ IDEA then we are going to run the Springwood micro services on Docker using Docker compost and finally we are going to debug this two micro services using IntelliJ IDEA now if I think about the overall flow I will download the project from GitHub into my local system and I'll set up it on IntelliJ IDEA then I have to perform mavenclean and install finally I have to run from Eclipse so this is about the local run regarding the docker run it would be the same I have to download the project and set up and perform have and clean and install on top of that I need Docker and there I will use Docker compose image build and then Docker compose up to run this so let me provide you a high level idea of the micro services that I am going to use I have two micro Services remove microservice one and demo microservice 2 from the browser I would send one request to the microservice one microservice one would call microservice 2 and get some response and finally it would return back the response to the browser so let me first download the project set up it in IntelliJ IDEA then we will discuss further the project location is github.com Expo 2015 multiple spring boot micro Services you can click on this code button download zip the project is downloaded here I am moving it to a particular location cut paste it over here right click extract here now I'm going to IntelliJ IDEA here I can click open here I can go to the location where my project directory is present and click on ok now it's loading the project let's wait for a while IntelliJ IDEA is indexing all the modules now to give you an idea of this overall project structure this is not a multi Maven module project this is a project where I have two maven-based projects inside a particular directory so this is my first project demo microservice one okay in the meantime IntelliJ IDEA is asking Maven build scripts found load I am clicking on load so here we can see I have this parent level project multiple spring boot micro Services inside that I have this one maven-based project demo microservice one and then I have demo microservice 2 you can focus on these two files Docker compose yml and Docker compost debug yml these two are actually using these two projects microservice one and microservice 2 in turn we'll go into the details let me go inside this project directories in order to understand what is the code structure it's pretty simple for microservice one I have this micro service one which is the spring boot application file here I have simple main method with spring application run and a log statement let me click on this truss project so so as you can see it's showing all red because the Java compiler is not properly set in order to set the jdk library properly I am selecting this demo microservice one go to file project structure here I have project settings here it is pointing to some jdk version I want to point it to my local jdk let me edit this this is my jdk 1.8 select ok again go to the project structure click on project so the version would be 1.8 click apply okay similar thing I'm going to do for demo microservice to file project structure it's already set to 1.8 click on OK so it's importing the maven projects let's wait for a while now you can see the red markers are gone let me close this this is the first project demo microservice one I have the spring boot application file inside main method simple login statement spring boot microservice once started successfully it is using this controller microservice One controller here we have couple of things which are important first of all I have one app config which I am using to pass the parameters like hostname and Port name for the second micro service I'll go into the details but let's scroll through second I have one rest template which would be used to invoke another micro service from this particular controller I have two methods one is display default message by default if I hit the URL of this microservice one this method would be called and it would be returning a message hello world from microservice one then there is another method with this particular URL slash call microservice to here what I am doing I am creating one URL for microservice 2. let me drag this a bit so this is the URL here I am forming a URL like HTTP colon double slash then hostname then Port name then this slash test call using this URL I am calling rest template dot exchange so that I am invoking another URL of microservice 2 from there I am getting a response and then I am sending the response back now why we need all this this service to host service to Port what is this and where is this inside this config folder inside this app config file you can see there is a spring boot component app config here I have two attribute service to host and service to Port Now by service to this project is demo micro service one and the project demo microservice 2 that I am referring as service 2. so when there is a inter micro service communication if I am running on my local system the host name would be localhost but when I am using Docker or kubernetes the hostname would be different it would be dependent on the parameters or the values I set in Docker compost or in kubernetes so I have kept an option like if I send a particular parameter value with this name demo microservice to host then that value would be taken otherwise by default it would take localhost similarly if I send a parameter named demo microservice to hyphen Port then that value would be taking otherwise it would be 8082 so let me first run these two projects locally we have gone through the structure of the microservice one and another important thing under resources I have this application properties where I am setting the server Port as 8081 similarly if I go to microservice 2 this is my spring boot application class I have simple main method where I have a logger springboot microservice to start it successfully and inside the controller I have microservice to control by default it would send a message hello world from microservice 2 and this is the method slash test call which I called from controller 1 from the other micro service if I invoke this slash call microservice 2 ultimately it is trying to invoke this particular method from microservice 2. if I go back this is the one so let me clean and build this project so I can go to the terminal I can use this section also so for microservice one let me do select this clean and install and click on this particular button run map and build build is successful for microservice one let me close this for microservice 2 also I am going to do clean and install the build is successful for microservice 2 as well now let me run this to microservices let me close all this open this microservice to class and also from here I need to open the microservice one class right click run microservice one main and then go to microservice to right click run microservice to Main okay let's wait for the microservice one to run now the microservice one is starting it has started successfully same thing I need to do for microservice to right click run microservice2.main now microservice 2 is also up so both the micro services are up let me go to the browser and access them from the browser I am going to use the localhost 8081 for the first micro service and localhost 8082 for the second micro service so the response from microservice one has come from microservice 2 has also come now I'm going to invoke microservice 2 from microservice one by invoking this call microservice 2 as you can remember this method will in turn invoke the second micro service using rest template so you can see over here the message from the microservice 2 has come so both are run from the local system using IntelliJ IDEA and they are able to communicate among themselves also if I go back to IntelliJ IDEA you can see this micro service 2 console the first message hello world from microservice 2 it has come and in microservice one here I can see the response has come from the microservice 2. now we would like to run this project in Docker compose so let me stop all of them here we need to understand the docker compose file if I see over here these are my two micro service projects microservice one I'm sorry demo microservice one and demo microservice 2. these are the two files Docker compost yml and Docker compost debug yml these are identical this is little bit different from the first one here I have added debugging related parameter but let us focus on the first one inside Docker compose yml you can see two services are defined demo micro service one and demo micro service 2. here I am providing this image names with my repository name then the image name and the tank container name I am setting as demo microservice one build context Docker file these are all standard let me show you the docker file so this is the docker file for microservice one plain and simple I am using open jdk8 then from the target directory after Maven build this particular jar would be generated I'm copying it final command is Java iPhone jar then this journey similarly in the second micro service I have kind of similar Docker file structure you can see it's pretty identical apart from the difference in the jar name if I go back to the docker compose I was in the demo micro service one I am exposing the port 8081 because this is the port the first micro service uses then I am passing certain environment variables I am telling what would be the host name and what would be the port value when we ran locally these two micro services for them if they want to communicate they could have used the hostname as localhost but when I'm running inside Docker the localhost won't work so that is why I am sending a custom value and that is why in the project I had the code where based on the parameter past I am setting the value of the hostname and the port name if some value is passed it will take the value otherwise for host it will take the default value of localhost and for Port it would take the default value of 8082 unless something is passed now this is the second micro service pretty standard stuff identical to the first one image name is different container name is different and Port exposed is 8082 so what I can do I can use this individual Docker files in Project 1 and project two for image build or I can separately use the docker compost to create the image let me go to the terminal here I am inside this node text file I have all my required commands so what I'm going to do I am going to use this Docker compose build the images are built now another option you can use features of IntelliJ IDEA like here if you are in the docker compose file you can see these are enabled and from here also you can have multiple options to run the docker compose related comments but in this particular lesson I am not focusing on that my images are already created so what I'm going to do I am going to run Docker compose up so that the services are up and running both the micro services are created now it's getting attached the logs are getting generated when the applications are going up I can now see the messages spring boot micro service 2 started successfully similarly springboot microservice one that is also started successfully now I need to go to the browser and I and if I invoke this particular method then it proves all first of all if this is successful it means microservice one is up and it is getting the response in turn from microservice 2. so if this is working properly it would prove that microservice 2 is also running correctly Let me refresh the browser so now I am getting the response from the second micro service if I go back to IntelliJ IDEA here you can see the log is there from the microservice One controller the response received from microservice 2 equal to high external service it's a response from microservice 2. so this setup is working the second thing we have also done we have run the spring boot micro Services multiple micro Services actually from the IntelliJ IDEA in Docker now we need to debug this from IntelliJ IDEA let me cancel the run from here so that the microservice containers that are running in the docker they are stopped here there is one important file we need to have a look which is Docker compose hyphen debug.yml this is similar to Docker compost file but with some additional things number one if you can see over here I am exposing one debug Port here for the first micro service which is five zero zero five then I am passing one additional Java tool options stating the debugging related setup here I am using this address is equal to 5005 now as it is jdk 8 this particular syntax is working in case you are using any higher version than jdk8 which is jdk 9 10 or whatever then your syntax would change instead of equal to 5005 you need to use this one equal to Star colon 5005 similarly I have changed the setup here also for the second micro service but I am using a different port it is using 5006 instead of 5005 so again let me go back to the console I am going to use the docker compose build but this time I would use a separate file hyphen f so I am using the docker compose debug yml and I am going to perform a build to recreate the images similarly when I would be using Docker compose up I have to use this Docker compose hyphen debug.yml instead of the docker compose yml so the image building is complete now instead of build I am going to use up now you can see the services are starting we can see few log messages from the microservice one and microservice 2 we have to wait patiently for the services to be up yes now we can see the second micro Service as well as the first micro service both of them are up and running in debug mode now I need to debug them from IntelliJ IDEA now how to do that for that first we need to go to this project directory here I need to select one source file so here if I go to this micro service one I need to debug this so what I need to do I need to go to run edit configurations here you can see this is the default option that are being provided but we need a separate one click on this plus icon you have this remote jvn debug select this I can name it as microservice1 remote debug now one thing you need to notice over here by default the debugger mode the transport host everything is populated so this 5005 is also there and this syntax as you can see it is 5005 for jdk 5 and 8 in case you are using something different accordingly you have to select then this syntax will change as I mentioned earlier anyways for me it would remain as is click on apply and OK similarly for the micro service 2 I need to go to run edit configuration click this plus icon remote jvm debug but here I need to provide a separate name this is for microservice 2 I have to change the port click apply click ok fine so here I need to select the configuration against which I want to do the debugging I am selecting microservice remote debug which is for microservice one click on debug now microservice one is in a debug mode similarly for microservice 2 I need to select a deeper configuration now this would be micro service to remote debug I have selected this click on this debug icon now both of this micro services are pointed from my IntelliJ IDEA but I need to place some breakpoint so that I can understand things are working properly I have to go here somewhere maybe in any of these methods say before sending the response let me place a breakpoint this is a micro service One controller and if I go to the second controller let me place a breakpoint over here now we need to run this micro services from browser so this would be calling the second method of the first micro service click on refresh so it's waiting if I go back to IntelliJ IDEA you can see the break point is hit now here what I will do I'll change the value of this response set value something like 1 1 1 1 1 1 press enter and here I would click resume program if I go back to the browser I can see the response has come back now let's check microservice to click on refresh again if I go to this controller you can see the breakpoint is hit and if I click on this resume program the response would come back and I can see the final response from here so in this way what we have done we have created and run to Spring boot micro Services together from IntelliJ IDEA locally then we have run them in Docker using Docker compose finally we have done the debugging for both of these micro services so as I mentioned earlier this is going to be the first video of a particular Series where I would be creating debugging related videos using IntelliJ IDEA on Docker kubernetes remote kubernetes engines in different clouds like AWS Azure and Google Cloud so in case you are interested and in case you are liking this video I would request you to subscribe to this channel so that when I upload the associated videos you get the notifications so that is all for now thanks for your time
Info
Channel: LearningFromExperience
Views: 2,527
Rating: undefined out of 5
Keywords: How to run microservices in Intellij, how to debug spring boot application in intellij, debug spring boot application in intellij, docker debug intellij, debugging spring boot application intellij, debug docker container intellij, spring boot debugging intellij, debugging in intellij java spring boot, spring boot, Microservices, IntelliJIDEA, Docker, spring boot microservices tutorial, spring boot docker compose, spring boot microservices, spring microservices, intellij idea, Java
Id: 5dpmyMN25Ts
Channel Id: undefined
Length: 18min 41sec (1121 seconds)
Published: Fri May 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.