Microservices tutorial with Spring boot 3 | Full course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys and welcome back to my channel in this video we will be building together a microservices architecture using springboot 3 which is the latest version of this awesome framework also we will be using spring Cloud such as API Gateway Eureka or AKA Discovery service and also I will show you how to distribute configuration using the spring Cloud config server also we will be building two microservices I will show you how to communicate between them and finally we will see how we can distribute all the traces to Zipkin and all these micro services will be running on Docker and now with no further Ado let's get started but just wait one second if you're new to my channel just go ahead hit the Subscribe button and join me to enjoy the videos that I'm publishing every week and finally I want to invite you to follow me on social media you will have all the links Below in the description of this video so now with no further Ado let's get started first of all let's understand what is a microservices architecture so to answer this question the microservices architecture is a software development approach that structures an application as a collection of loosely coupled small and independently Deployable services each service is designed to perform a specific function and communicates with other services through a well-defined API and API is application programming interface or a messaging protocol these architectural style promotes modularity flexibility and scalability as each service can be developed deployed and maintained and dependently of the others this allows for easier updates faster release cycles and improved fault tolerance as a failure in one service does not necessarily affect the entire application the microservices architecture also Fosters the use of diverse Technologies and programming languages for different Services depending on their specific requirements and functionalities next let's dive and understand the architecture diagram of our future implementation this is a global overview and a global architecture of our future application so the microservices architecture is based on several components that they should be there so first of all here as we can see when we send a request from the Internet or from the outside of our ecosystem so we need to have our first entry point which is API Gateway so this API Gateway is responsible of intercepting or receiving all the requests and then redirecting them to the to the correct service and then inside we will have two microservices one microservice is for students and another one is for school so this means like a student studies in school and school has many students so it's going to be a small business application but this is not our Focus but we need because we need to understand first what is microservices architecture and each microservice will be communicating with a databased running on Docker and then by the end of the vid of this video I will show you how you can Implement distributed distributed tracing means to trace all the requests of your application to zebkin then when we talk about microservices we mainly talk about Discovery server and also config config server or configuration server so here let's understand globally this diagram so we mentioned that we have an API Gateway which is our entry point or the Gateway for all the incoming requests and outgoing requests for our ecosystem and then we see right here that we have a config server and Eureka or a discovery server so each micro service in here when it starts it needs to register itself into the discovery server so this discovery server will be also used by the API API Gateway to determine what are the running services and also to have a better control on the configuration and have a distributed configuration we can create and we will create of course this config server where we will externalize all the configurations of our applications including the Eureka server the each microservice and API Gateway so as you can see here all the micro services that we have here they will pull the configuration from this config server including the Eureka and also the API Gateway and when each application starts it should resist register itself in the discovery or our registry server so this is overall our future architecture of the future application that we will Implement and first of all let's start by creating or preparing our workspace and then we will be creating each of these microservices and then we will Implement each microservice or like the business logic or the business services that we have here student and school after that we will insure and I will show you how to communicate to microservices together and finally I will show you how to add this distributed tracings and I will show you how to add Zipkin and how to run it and also how to log and Trace all the logs to this tracing application so to bootstrap our application first of all I created an empty project in my working space and I called it microservices and then using ntdj I just clicked on file and then open and I selected the the folder where I want to have my workspace and just opened it because in this project and in this video we will be using a mono repo approach this means that we will have all the code in one place it's not a modular application but it's just monorepo having everything and all the code in one place what I did also is I just created a readme.md file containing this title microservices sample project and it's by alibu and here you can also find the url for my website where we can where you can find so many useful courses about spring boot spring data jpa also how you can integrate PayPal payments and so and so forth so I really invite you to have a look there and also you can get certified from from my platform now let's create the different projects and the different microservices that we need for this architecture so as you can see here we will start by creating an API Gateway project and then the discovery server and then the config server project so we will have the the tools or the side microservices ready and then we will create the business microservices and finally I will show you how to integrate zipcad so let's go ahead and do it first of all let's start with our API Gateway so it's going to be of course the project it will be Maven and Java language and at this time it's 3.0.5 for the spring boot version and for the group ID it's going to be alcom.alibu and here I will call it Gateway or API Gateway so this is the Gateway and we can leave the rest as it is and here for the dependencies let's select Gateway and also we need the Eureka client the Eureka Discovery client because as I mentioned before the API Gateway also needs to read the configuration from our Discovery client and since our API Gateway also will read the data from the config server so we need to import also the config client and all of them are from Spring Cloud all right and finally we need to add the spring actuator this one so this is for operations and also to expose metrics of our applications we will see that later on alright so now we have our Gateway ready so let's generate it and then we let's just move it to our workspace and I will show you how you can import this within ntdj so in my workspace here I will just paste the generated zip file and I will extract it in here so then I can remove this one so I have my Gateway application now I will open my ntdj and here we see that the Gateway is now in the workspace but it I cannot run this application so for example if I go right here I see that the even the SDK is not set up so I can click first on setup SDK and you can use the one already installed but it's not yet imported so what to do is you go to file and then new and after that module from existing source and then open your workspace and just select the poem.xml file and just click on open and you will see first of all that you will have this Maven icon popping up and now even the icon and the display of this project is changing so now we have our mono Ripple project and inside this mono Ripple we have the Gateway so for the next services that you will be creating it will be exactly the same way so every time you need to go file and then new and then module from existing source and you just need to select your pom.xml file or Gradle if you if you are using Gradle and then just click on import and you will have your your application imported in here now let's create the second micro server which is the Discovery server so let's call it again com Dot alibu and of course Maven and the latest stable version of spring Boot and here I will call it discovery and I will keep the rest as it is and now I need to search for Eureka and for Eureka we have Discovery client and we have the discovery server so we want to create a server right now so this is the Microsoft microservice responsible for the for the server that will be our Discovery server so just create or select the Eureka server and then since also Eureka will be reading from the config server we need to import our config client dependencies which is spring Cloud config after that we will need our actuator of course we need it for uh for the metrics and that's it so now just click on generate and import the the service in your entityj now let's create our config server again it's Maven project and here I will use a com.alible and then I will call it config Dash server as an artifact and here I will just rename the package to make it config server as one word and here simply I need one annotation which is config server which is this one right here be careful between choosing the config client and the config server now we are creating our config server so we need this one and then click on generate and of course import your project in ntdj so after creating the three basic microservices that we have here config server Discovery and Gateway now we we created one part of our application and as we can see here we have our API Gateway our Discovery server and also our config server so the the next steps would be creating this business microservices but before that let's configure our Discovery server and also the config server will we will create it and we will be implementing this config server by the end of this course so stick to the to the end and you will see how we can configure and externalize all the configurations but now let's go ahead and implement this Eureka or Discovery server now within my IntelliJ I will go to Main and then resources and then have application properties so first of all I will just rename this and use yaml representation I will make it full screen and now I will add some properties so first of all since it's a discovery server I will add yurica.client dot so first of all I will create eureka.instance.hostname so this is this will be the host name of our Discovery server so it's localhost and then I have eureka.client dot register with Eureka so by default this is true and I need to turn it to false because eurika does not need to register itself with itself this this is what it means and then we have fetch registry also we we will set it to false because we don't need to fetch the registry and then we have the service URL and our service URL we have our default Zone and then the default zone is going to be HTTP slash slash and then I will dynamically inject our or my Eureka instance hostname so this one so this will refer to this one so in case I have a different environment so it will automatically read from this one and then I will need of course the port so it will be server dot port and then slash eurika slash so this is the first configuration of the configuration of my Eureka server then I need to specify my server.board so for this one I will be using uh 8761 as as a port and then since we have our config server now I want to tell the Eureka to read the configuration from my config server and to do that it will be spring dot config dot import and here to import we have we have several options and I would be using this optional optional means if if this application or if this microservice doesn't find the configuration on the config server it will not fail to start up and then it will be config server and column and then the location so it's going to be HTTP current slash and localhost since my my confixel will be running locally and then I will give it the sport so I will also when I create or when we configure our config server we will give it the support 8888 and then after adding these properties we will go back to our Discovery application.java and here we need one annotation which is enable Eureka server so this will enable or will activate the auto configuration of our Eureka server now let me show you when you correctly configure and Implement your Discovery server how it looks like so here I will go to services or you can just run it from here but for me I want to run everything from one place so here I see in this view right here Services otherwise if you don't have it you can go to view to tool window and then you have the list of the services that you can display and then I will select My Discovery application and I will run it so the application is up and running now if I go to the browser and go to localhost colon and then 8761 so you see right here that we have this is the spring Eureka and then we see that currently we have no instance available so then when we will create our business microservices and start them they will be automatically registering themselves in here and we will see the list of these microservices now let's create our student service so here I will just create a new project it will be mavenproject the same spring boot version and here I will use com Dot alibu and then I will use student for this one and also the package it will be com.alibu.student and then I will just add some dependent agencies so for the application we will need first of all post degree SQL it will be our database and then I will use a lombok and we have spring data jpa so all this you already know otherwise you can just go and check my website for more insights about this and of course since we are building a rest API we need spring web and now let's come to the cloud dependencies so first of all we need our config client to read the configuration from our config server and then we need the discovery client and finally we will need our spring boot actuator so then click on generate and import the project in your entity so now let's start configuring our student project so first of all let's go and add some configuration and here the first thing I will use the yaml representation as you know my styling and then let's add the configuration so first of all let's add the Eureka configuration so it will be eureka.instance.host and it will be localhost since our Discovery service is running or will be running on locally and then we have client and of course we have the service URL and the default Zone so the default Zone it will be HTTP current slash slash localhost and then 8761 which is our Eureka port and then slash Eureka so this is the configuration for our Discovery client and then I will just move to the bat to the bottom and I will add a port so I will be running this application on the port 8990 and then let's add some spring properties so the first one it's gonna be spring dot application.name so I will give a name to this application so I will call it students and this is the name that will show up on on the Discovery uh or the Eureka interface I will show it to you later on so then I will add the config the location for the config so it will be spring config dot import and then we have optional and then our config server and then colon and then HTTP colon slash localhost and then we have the port which is four time eight now I will also configure my data source so I have here uh data data source and then driver class name so it it will be post degree SQL and this is the the normal configuration or the classic configuration that we know and then we have of course the URL and then it's jdbc colon post degree SQL colon slash localhost and then the port 435432 and then slash students I will call my database students and then of course we need either username which is username for my case and sorry it's not Hikari but just username and password so it's username and password so then we will add some jpa properties so jpa dot hibernate.ddl Auto I will just say create and then database it's post degree SQL and I will add also the database platform which is org dot hibernate dot dialect dot post degree SQL dialect so this is the configuration of my application now if I run it I'm expecting this student service the new microservice to be registered on my Eureka Discovery service so let's go ahead and do it now I will open services and I will run this one because I need it running let's enable the loanbook processing and then I will be running my student service so now if I go back to my browser and refresh this page so we see here that we have the students application because we gave it the name student and then the status is up and also we see that it's running on this port or this location so it's this is the port that we chose which is 80 90. so now we have our student application up and running now we can move on and implement the logic or the small Logic for our students microservice when implementing a monolith application so we will have a class diagram looking like this so we will have our student entity and also our school entity and then they will be related one to many because one school has many students and a student only studies in one school so when we create our relationship and on the database side we will have a student with a foreign key the ID of the school and that will be our class diagram but now since we are in a microservices architecture we need to handle this one so let me show you how we can do this so within our student microservice or project now I will create an entity and I will call it students and it will has the following annotations so first of all it will be entity and then I will need the Getters and Setters from lombok let me make this full screen and then Setters and also all arcs Constructor and the no arcs Constructor and also I might need the the Builder so now for the students first of all I will create a private integer ID and I will give it the ID annotation so if you want to know more and to learn more about the spring data jpa you can go and check the new course that I recently created which is the spring data jpa master class and this master class it's more than five hours of spring data GPA and you will learn everything about spring data jpa including inheritance how you can create the relationships hibernate versus jpa also you will you will have a better understanding of of the hibernate entity life cycle how the repository works and so and so forth it's more than five hours of video content so if you're interested just go ahead and and check it now let's go back and finish our student class also I will have a private string first name and I will also also have a last name and let's say we can also add private string email to our student class and now since we have a relation between the student and the school and since the student is a whole domain and the whole application it will be only referenced by a private integer school ID and then when we work in microservices we need to ensure the data Integrity manually all right so now we have our student entity let's go ahead and create the repository now let's create a new class and it will be an interface let's call it student repository and this interface will be extending the jpa repository and then it will be of type students and also our ID is of type integer so this is all we need to have a repository working after that let's create our student service so I will create a new class I will call it student service and of course this one we need to add the service annotation and we need the required rxcon annotation from lombok and first of all I need to inject my repository so student repository and then I call it repository and in this application it will not be a really business huge application but I will have just two endpoints one for saving a student and the other one for finding all students and also I can give you the freedom and the time if you want to implement more and more endpoints so let's create the first one it will be avoid save student and then we need student as an input also as I mentioned you need to to follow the dto pattern and you need to create a different object but here just because we don't want to make this part so long I'm just using the entity to to create and to expose all the information so now all I need to do is repository.save student and the second method it will be a public list of students and I will call it find all students and simply I will return repository dot find out so now we have our two methods let's now go and create our controller now let's create our student controller and they will create a new class student controller and then I will need the rest controller annotation and of course request mapping and it will be slash API slash V1 students and I will create of course I will need the required RX Constructor from lombok and I will inject my student service I will call it service and first of all I will have a post mapping and then I will have a response status and for this one I will just return accepted or created and then it will be a public void save for example or save student and then we will have a request body which is an object of type student and all I need to do is calling my service dot save student and I pass the student as a parameter and then I will create a get mapping and this is going to be the find all method so it will be public response entity of type list of students and then find all so here it will be just returning response entity.okay and then service dot find all students and that's it now we have our student API ready to use we can either go ahead and test it or let's me let's move on and create the school service and use it so here just one thing don't forget to add the final the final keyword otherwise it will not be automatically injected or added to the Constructor Now to create our school microservice I will keep the same interface like here like we did for the student with the same dependencies and I will just change this one to school and here we will have our project ready to download let's generate it and open it with our entityj now I have my school project ready to use and in order to make it easy for you all I will all I will need to do is to copy everything from the students so first of all I will copy the properties Ctrl a and then I will paste it in this school so here I will open Main and then resources and then this application properties I will just use yaml and then I will paste just here so now I will just change the port from [Music] 1890 to 80 70 and then I will be using the same configuration except here it will be schools as database and the rest I will keep it as it is also don't forget to change the application name so let's call it schools and the rest nothing to change now let's just copy the implementation so I will expand this one and I will copy student and service and controller so Ctrl C and then let's paste this one and rename what needs to be renamed paste it in here so now instead of this student we will call it school and then it will ask us if we want to rename the parameters of course we want so let's just do refactor and then for the information of the school we need the ID this one it will be name we can keep the email and we don't have any other school ID so the school is just a simple one it's an ID name and email also let's rename this one from student repository to school repository and we see already that the school was already renamed and then let's rename this service so let's call it school service and we see here that we are using School repository this one I will just rename it to save school and here I will call this one find all schools then we need also to rename the controller so I will make this one full screen click on the name and then just rename it to school controller and here instead of students it will be schools all right so we have almost everything so here we have save and let's rename this one to find all schools and everything is ready now so now let's just start the application and see what what will this uh microservices what they will give so now let's start the different microservices that we have and see what are the changes so first of all we will need our Discovery application so let's run it and then we will need this cool and the student so I will be running these three microservices and as you can see here the discovery application is running on the port 8761 School application is 8070 and the student application is eight zero nine zero now let's go and open our browser and see what are the changes on the Eureka config server so now if I refresh this page I see that I have two running micro Services the first one is schools and the second one is student now since we have these two Services up and running and they can be running even outside of the microservices ecosystem this means that we also from our local application or our local machine we can directly access the student and we can also directly access this school microservice even if both of them are inside our microservice architecture but the way to protect this is to have all these applications right here running in a private Network and we only Expose and we only expose to the public only the API Gateway which is our only entry point so this is how you can fix this one but since it's running only locally and it's only on our local machine so it's also possible and it's also okay to be running um and using this microservices from the inside now let's go and try this so within Postman I prepared the the end point so that I created or I prepared the first a first endpoint which is uh save student sorry the first one is so within Postman I prepared all the necessary queries and the first one is to save a new school and I have another one is to create a student and then I have another one is to get for example the list of schools or the list of of the students so now let's go ahead and try the creation of a new school so I will click on send and we see that we have 201 created for example I will create a school tool and then school three so here I will just create three schools and now I can go back to my database and make sure that I have three schools created so here within our database we can go to schools refresh this one we have our table and now we see that we have these preschools and these three random emails let's go back and also check the students so this is our student table which is empty for now now let's go and insert some students in here using this endpoint I have a random first name random last name and a random email and I will be inserting some students for the school with ID number one so let's insert them let's insert insert insert and also let's insert some students for the school number two and some of them also for the school number three now if I go back and check my database and I will refresh here I see that I have all the students that I just inserted all right so now we have um okay so now we have this microservice running this one is running and also both of them are registering themselves to the Discovery server now let me show you how we can ensure communication between the student and the school and we will be creating a new endpoint we will call it a school with students this means when if when I try to get a school I want to get the school and all the students that are in this school so let's go ahead and implement this so now let's go back to our school microservice and then within the school controller I will create a new endpoint and it will be a get mapping also I can just duplicate this one and it will be as I mentioned it will be a get mapping and I will call the this endpoint with students for example and then slash I will I need to pass the school ID as a path variable so this one is going to be school ID and here it will be a path variable and then school ID and here it will be integer school ID so then instead of find old schools I can do fine schools with students so find schools with students and then I need of course to pass the school ID so now I need to generate this method so let's go to the service and create this find school with students so I will just use the auto completion from ntdj to generate this method and this one it will be a list of and here instead of a list I will just return a new object and I will call it a full School response so here I will say full School response and this this will be or this object will be the return type so let's go back to our controller and instead of a list of school and it will be just a response entity of full response so this full School response it's a new object that I don't have right now let's go ahead and create it so I will create a class new school or a full School response in the same package and then I'll just paste it and this full School response it will hold the same information as we have in this school.java so it will be the name and the email and then we need a list of students let's call it students and for now we don't have yet this student object so I will just open this student.java class and I will just copy these three information right here and then let's create a student object so here let's create a class student if they're within the same package so here let me show you how this looks like so here we have this full School response and we also have this student object So within this student I will paste these three um attributes and I will add some lombok annotations so it will be getter Setter and then all arcs Constructor and also no RX Constructor and annotation builder in case we need it so I will just copy this annotations and also paste them here so now I need to import this list and now we have our full School response representation when we ask for it or when we invoke this endpoint we will be returning the school information within all the students that are in the same school so now let's go back to our school service right here so what we need to implement first of all we need our school equals our repository dot find by ID and then it will be a school ID and what we can do we can do or else for example we can just return a new school object so for example let's say School dot Builder dot build so just it this just to avoid node pointer exceptions or like unhandled exceptions so let's say it's going to be something like this dot name so it will be uh not name let's say just not found for example and then dot email and also we can call it not found this is just to return something which is readable and then what we need to do here we need students equals and here let's say null for the moment and now what we need to do is to find all the students from the student service or microservice so this is what we need to implement right now so let me show you how we can communicate or how we can Implement a communication between two microservices but first of all we need to go to the student microservice and prepare this method that we need to consume now within our student controller right here let's create this new endpoint so I will just copy this get method and I will call it or give it the URL slash school this means I want all the students slash school and then I need to pass this cool ID as a path variable so here it will be also a list of students and now we need our path variable and then School Dash ID and then integer school ID so here it will be find all students by school and then we need to pass our school ID so now let's go to the service and create this method and it will be a list of students so it will be repository dot find all by school ID and then school ID so since we don't have yet this method and now let's just generate it so if you want also to deep understand how these methods works and how these queries are generated in the background I invite you again to have a look on the new course that I just released which is the spring data jpa Master Class all right so now we have our new endpoint that will allow us to fetch all the students by a school ID now let's go back and finish the implementation of the communication between these two microservices before starting the implementation of the establishing the communication between the two microservices there is one dependency that we need to add and to do that let's go back to our spring initializer and in here just add a new dependency and search for open Fame so this open pane is a declarative rest client openfame creates a dynamic implementation of an interface decorated with Jack's arrays or spring MVC annotation so let's just add this and here let's go explore and let's search for this open Pane and then we can copy and paste it in our project so now let's open this pom.xml from the school project and now let's paste this and then don't forget to to refresh the maven project in order to download the new dependency so now we have the required dependency let's let's start implementing the first thing we need to do after adding the dependency let's go to our package right here and create a new Java class and then select interface and it would let's call it student client and let's move it to a package and call it client so it will be client.student client and here the first thing that we need to do is faint client annotation and this Phoenix line annotation it will be responsible of establishing a communication and making an HTTP call to our distant API or our third-party API so here let's give it a name or a value and here let's call it student service and then we have the URL information and for this URL I will just externalize this one and I will move it to application dot config dot let's call it a student's URL so I don't have this one yet I will just copy it and now I will go to my application yaml and scroll back scroll to the bottom and here it will be HTTP colon slash and then localhost and then 80 90 which is the port for the students URL and then slash API V1 slash students so this is the URL of our student endpoint or student microservice now let's go back here so what we need to implement is a get request so it will be a get mapping right here and then it will be slash with or slash School so this is the end point that we have in here let me just remind you so if we go back to student controller it's slash school ID let's copy this and I will just paste paste it to avoid typos and now it will be just a public or we don't need public with interfaces so it will be a list of students so the student here just make sure and be careful that it's from the alibu.school which is this cool project and let's import also this list and then let's say find all students by school and here we have our path variable which is our school ID and then we need the integer school ID and that's it so this interface and this method right here will be or will have a an automatic implementation provided by feign or openvane and it will establish a communication with our student microservice so now let's go back to our student or School service in here so instead of this null first let's go and inject our student client let's call it just client and then the student it will be client dot find all students by school ID or by school so now we have let's just rename this one so find all students by school and we pass the school ID so now we simply need to return this full School response dot Builder dot build and now we will have the name which is the School dot get name and then we have the email which is also the school.get email and finally we have our students which is the students that we just fetched now let's start the application and we can try and test this one and see and make sure that you are making a communication between these two microservices so again let's restart these three applications so all of them are up and running let's check on the discovery of the Eureka user interface and here we see that we have these two microservices up and running let's go to our Postman and then I will insert some a new school so SQL 3 and also I have school 2 and school one for example and I will insert just few students for the School Number One so we have always 201 and now I will just use this one so it schools with student and for this one it's just I will be using the normal Port which is this one so now if I do send so now we are getting a 500 internal server or error so let's go and check this so here just click on the the application that you want to see the logs from so it's cool application and let me make it a bit wider so here we see that we have a null pointer exception which is cannot invoke this student client find all student and so and so forth because the client is null even we injected this bin but we are missing something so let's go to our school application and here within the school application file we need to enable Fane clients so if you don't add this annotation you will not be able to run or to execute any of the faint clients now all we need to do is just to restart this cool application and let's go back to our Postman so again I need to insert at least one school and now if I click on send we see that we have this school and we see all the students from the school so let's try to insert some other schools let's say School alibu for example I will insert this one and I will go back to the database and check the ID so if I open this and I open the schools and this one has the ID number two so let's go back and insert some students for this school so here ID number two and they will click Send few times and now if I try to get all the students from this School we see that it's called alibu and we see the list of students so we establish we established a communication between these two microservices and we were able to communicate the school with the students so when we fetch a school with all the students lists so we are able to establish this communication now let's move on and chain made some changes and now let's make our two micro services available via this API Gateway because for now we are accessing these two microservices directly using the endpoints now let's access them using the API Gateway so now in order to have our API Gateway up and running we need to add some properties so let's go to the resources and here instead of properties I will always use yamu just as you know it and now I will make this one full screen so first of all I will add the server.port so I want to run this application on the port 8 3 times 2 and now let's add some configuration so first of all it will be eurika dot client and then register with Eureka I will set it to false because I don't want to register this this micro servers or the API Gateway with Eureka otherwise if you need it for some reason you can also remove this property right here now let's add some spring configuration so we have spring dot cloud dot Gateway and then we have the discovery locator enabled so by default it's false and I want to set it to true so I want to fetch the information or the the data from the discovery server and then within the same level as Discovery let's add some routes so for the routes it's it's a list of routes so it will be first of all we need to give an ID to each route so the first one it will be students and then we have a URI so the URI is HTTP so where the API Gateway should redirect the call so it's localhost and then 80 90 for the students and then we have a list of predicates so the predicates is when the path contains the following so when the path contains API slash V1 students slash anything I want to redirect this to the this request to the students endpoint now I will just duplicate this one and here we have schools and then instead of 8 90 80 it's going to be 80 70. and then we have this predicate so instead of students it will be and then let's add the application name let's call it Gateway I'll just move it to the top to have this alphabetic order and don't forget to add the config dot import and then optional from our config server and then it's located in the HTTP colon slash localhost and then three uh four times eight all right so now we have our API Gateway up and ready so all we need to do is to run this Gateway and try to invoke all the endpoints that we did before from this API Gateway now let's run all the services and test them so we need first of all the discovery the school and the student so let's run all of them at the same time and we also have this not started and we need to start also the API Gateway so the API Gateway is also up and running so if we open again our Postman and now if we go back to this endpoint for example to create a school we know that the school is running on the sport but if I change it to 8 3 times 2 and click Send I'm expecting all a 201 so this is what we see and we can also let's create School two and maybe score three and also let's do the same with the other endpoints and then we can go and check that everything or all the requests are passing through the API Gateway so let's click on send and here I will also assign them to the school number one and so I'm sending a few of them and now within this endpoint instead of this port I will use the eight three times two so and I click on send and we see that we have this response also if I change it we also see that we have a response so now what we can do as a next step is also when we want to communicate these two macro services in here we can when the school needs to fetch the students we can send the request to the API Gateway and then the API Gateway will redirect this request to the student and then it will go back so otherwise we can even make an internal communication since these two microservices are within the same the same private Network all right so in order to do that it's quite easy so if you want to do it let me show it to how how you can do it so now let's go to our school project and then let's open the pro the properties and here instead of this port all we need to do is to use this one now if I restart the school and test again you will see that everything will be working fine so now I want to show you the next step is externalizing all this configuration that we see here I want it to be external I want to have all this configuration in this config server so I don't want to have any local configuration and why I need that because the config server will ensure that we every time we need to change some configuration we just change it in one place first of all and then all the changes will be propagated to all the microservices in order to ensure the distributed configuration for our application first of all we need to configure our config server so first let's go to the resources and let's use Yaman representation in here and now let's add some properties so first of all as we mentioned before in the previous configurations so we want to use the port for time 8. and then let's add some spring configuration so here I have spring and then profiles and then active so I want to set the active profile to uh to Native this means that it will be or automatically use the active the native active profile and then let's also give a name for our application let's call it config server and then within spring cloud and then config and then server and then native and finally search location so the search locations means where spring config server is going to fetch all the configurations that we want and it will be class path and then colon slash configs or configuration so these configurations I will just copy the name right here and I will create under resources I will create a new folder and I will call it configurations so spring config server will automatically start fetching the configurations from this location right here so now the next step is is to enable the config server so open the config server application and here add The annotation enable config server so now we have our config server up and correctly configured all we need to do as a next step is to add the configurations for each microservice so let me show you how to do that so now let's externalize the configuration for our school and then first of all I will be just copy pasting these properties so I will just Ctrl a Ctrl C I'll just copy this and then I will go back here and under these configurations I will create a new file call it schools Dot yaml and here make sure that this file name is exactly the same file name that you which is this one so let's paste everything and now all we need to do is to clean up a little bit so we don't have this config import and so on so forth because it's already in the config server and we can keep the rest as it is so now all we need to do just to make sure that everything is working is restarting the application but we will do it once we externalize all the configurations now from the this application yaml in the schools all we need to do is just to remove everything and all we need to leave is this application name and config import and the rest we can just remove it so now as you can see we have a really clean and few lines of code in our application.yaml file for the school project so let's do the same for the other project which is the Gateway we can also do it for the discovery and we also we will do it for the students so let's do it one by one let's now do the same for students so I will just go here and just extract all this and then in the same place I will create a new file and I will call it students dot yaml so if for example if you choose to call your services School service or student service just make sure you give it the same name here and now let's just remove this one and keep the rest alright let's go back to our student and let's clean it up so the port we don't need everything we don't need except the application name and the config and we can just remove the rest and now we have again one clean config file for our microservice now let's externalize the Gateway and again the same way let's open our application and here we just gave it the name Gateway within the config server source and then Main and then resources and configuration let's create first of all our file and give it the name gateway.yaml and now let's copy all this configuration and now we need to remove this config and the rest we keep it as it is so now let's also clean the original one so we don't need this and the rest also we can clean up so again we have our final application yaml for this Gateway we still need only to migrate or externalize the discovery properties so from the discovery here we just need to give our application name and let's call it discovery and then let's create a configuration file for it discovery.yaml and let's externalize all this configuration so here and then all we need to do is just to remove this and now let's go back and clean up all this we can just remove this one and leave only this all right so we externalized everything now let's start the application and make sure that this configuration is correctly read from the config server and that the application is still up and running so now first of all we need to run our config server since we will be reading all the configuration from there so it's the first service that we need to start and now we are up and running with this config server and now we can just start the rest at the same time so I'll just select all of them and click on run and it will be running and if everything is up and running we will see all the applications starting on the correct ports and so on and so forth and now to make sure that everything and all this configuration was correct now we can just go and try to insert some data and play with our microservices architecture so in here I will just insert insert again a school so click insert and we see that we have 201 send again and also we I can send or insert some students and then you can see that we can fetch or I can fetch all the information from our backend alright so now at this level we implemented all the parts of the of our application so we have our API Gateway the different microservices they are correctly communicating with each each other and we also have our Eureka and config server so now all we need to do finally is this distributed logging so we need to add an install Zipkin and then we need to redirect all the logs and the tracings from our application to Zipkin so first of all as I mentioned we need to install Zipkin so let me show you how to do that so to help you and make your life easier so I just created a Docker compose file for you containing all the services that will that we will need to run this application so first of all we have this post degree SQL so this is the database container that we are using and here you have all the configuration needed also if you don't have the ultimate version of ntdj and you don't have this menu right here you can just access your database using this postgrades admin available on this part right here so then we have the service for our Zipkin so it's just a container also you can find the official image and how you can just simply run it or even if you want to install it on Prem directly on your laptop you can just go to that to the docker Hub to find the the information for that or to the official website and just run it so this ZIP can service we gave it a container name Zipkin and then it's from this image so this one is the image that you can find on the on the docker Hub and then it will be running on the port 9411 and then we have a list of networks which is this configuration right here so I will not go so much into the details of this Docker compose file you can just find it in the source code and then you can just copy it okay so once you copy and once you have this Docker compose file so you can just click on services and run all of them or for example if you have a post degree post degree SQL already installed and you have everything you need except for Zipkin you can just click and run only Zipkin and then you can access the Zipkin interface on the following URL so all you need to do is just go to localhost and then the port 943 times 1 sorry 9411 and then you will see the Zipkin interface so for the moment we have nothing we don't have any logs because we did not Implement logs yet so let me show you how we can add or how we can distribute the logs to Zipkin so the first step we need to do is to add the Zipkin dependencies so here let's search for Zipkin and then click on explore and you will see that when you add Zipkin you will have two different annotations dependencies sorry not annotations but we will have two different dependencies the first one is the bridge Brave and the second one is the reporter Brave so let's just copy them and add them to all our micro services so now first of all within our API Gateway let's add this dependency go just go to the bottom and paste paste it and of course don't forget to reload the project also we can add it to the school and then students now we have Zipkin already available and since it's starter it has an auto configuration and all we need to do is just to run the application and then we will have all the the tracings going and distributed to Zipkin but to ensure that that we get all everything what we need to do is just to add some configuration some management configuration to tell that the probability of Distributing all the logs and the and the tracings to zepken I want it to be 100 so let me show you how to add that so now to let's go to to our config server and then to main resources and then configuration and for the Gateway School and students let's add this configuration so I want to add management and then tracing and then sampling and let's add the probability and let's make this probability equals to 1.0 so I will just copy this one and paste it also in here for the school and also for the Gateway since the Gateway also will have some tracings and now let's run our application and let me show you how all the tracings and all the logs would be going directly to Zipkin all right now let's first of all run our config server and then we can run the rest of the applications so let's run them one by one and now let's go and make any requests so let's open our Postman and that's for example create a school so here it's 201 now let's go to Zipkin and I will just run query and as you can see right here I have the first log a few seconds ago and if I click on show we see that here we have a post and it went through the API Gateway and from the API Gateway it went to the slash API V1 schools and also we can search by Trace ID and here we have all the information that we need for example we have these tags we have the URL and the method what was the outcome and so and so forth so also if we for example insert some students and then we perform a get request and go back to zepkin and here we do find query we see that we have all the tracings right here so here we see that we have two for Gateway One for school and so and so forth so now all the logs and all the traces are centralized in into zipkey
Info
Channel: Bouali Ali
Views: 58,906
Rating: undefined out of 5
Keywords: spring, jpa, data jpa, mapping, onetoone, one to one, spring data, many to one, manytoone, class, generatedvalue, persistence, repository, service, jparepository, jpa repository, uml, class diagram, design, software, engineer, software engineer, java, jakarta, javax, spring boot, springboot, security, spring security, aliboucoding, spring boot 3.0, spring boot 3, spring 3, jwt, filter, authentication, authorization, bearer, jjwt, oauth2, github, social connect, social login
Id: KJ0cSvYj41c
Channel Id: undefined
Length: 68min 10sec (4090 seconds)
Published: Mon Apr 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.