In this session, let us do a sample project incorporating all the concepts that we have learned till now. Let us create a web application. With the help of the web application, we will access the micro service. Let me explain the flow. First, I'm going to have two micro services. One is order service plus book service.
Mobile services are registered with Eureka server three micro service echoboy. I've got two micro services which are registered with Eureka server. Next, I need to have a web application or web client. Explain it now. web client is a web application. This application is going to access order service or a service is a micro service, which is nothing but a REST API.
You know how to access micro service from a web application. Using the rest template into this RFP to sign to access book service. Already we have learned how to access from one micro service to another micro service. Again here also we will be using this template only. Together with this we will also incorporate hystrix battery in corporate history, I will be adding these six only in order service. What is the point when the web client assigned to access water service, so service is down.
In that case, our FMS will not throw an exception to play, but it will give the fallback method to the client. Let us start now. I'll go back to eclipse and Eclipse. The applications that we need for this particular project is Eureka server book service, order service and the client. Let me start with book service. In book service, I will open palm dot XML.
The dependency what you need over here is only you break up Domain comm Spring Cloud starter Eureka as of now, this is deprecated you need to add Netflix Eureka while selecting some spring starter project itself it will give you the option This is the first thing next Let me open this combat Boca application dot java here it is annotated with at the rate enable discovery client. This is a micro service it registers itself to Eureka server and also it is available for other services. Then I will open controller I have got book controller, the class has annotated this accurate rest controller. And here I've got just two methods get books by author and get one book and the man I mean I have given proper mapping for the first one books by author slash author for the second one book slash book ID. Activision, I have use PATH variable.
See, in this case, I'm trying to use strings. So you You also can do the same thing. What actually is Want to do with I want to add in the stuff books, we don't have a database. So I have created a private method. And I have given a list of books book is my model or my domain class. So you can see the list of books over here.
What actually you need to do for get books by author, you have to I created using a for loop in snap that I'm using strings also got thing, converting the list into a stream, then filtering it based on the author and converting it back to a list. Now available when it's returned back and forget one book also I'm doing the same thing filtering the book by ID. In this case, we want only the book object. It's a smarter list. So here what I have given Can you just have a look, filter dot find any dot get. I'm returning a book object.
I have this part of clear already we have learned how to use things. This is my book micro service. Let me also open application that Yama, Yama has given the port number 8089, the name of book service. This is that next order service, I will open it in order service. Let me go to Palm dot XML first. Again, this is a client, so we need Eureka, then I'm going to add hystrix to this particular application.
So, we need his fix also. Here, let me just scroll it down, your a cost is fixed, if you want to monitor his fix for this particular application, our hystrix dashboard. So I have added hystrix dashboard. Of course, we are not going to monitor this already I have shown you how to do that. Then, let me open our application in our application I have given at the rate enable discovery client, I need this template why because this Auto Service is going to access book service and again we are going to add hystrix command. So we need address enable circuit breaker.
Let me add that next. Let me go into the controller orders controller. Here are two methods, such books may appear as an order book, I have given the proper request mapping here we are not using this template because we want to add to fix his stage can be added only in classes that are annotated with either a service or a component file got one more class like auto service I bought into this class. Here, I walked away address template to method find books by author and find one book what we need to do we have that six common ibotta method. Let me add a six comma America Come on the fallback method. Find Books by authors fault a book by author and for those that have fallback find one.
Now what we need to do we need to create the method with the same signature, original method. So let me add both the method that is for books by author and fallback point one, the politics methods are added just how Look, I have given the name of ibuka dummy for returning a booklet and again for returning one because I have given the title of dummy why I am setting the title of Sammy, let me explain it, I will go back to the solders controller in August controller first books by author it's very simple, I mean you are going to retrieve the books by a particular asset. If the Book is not Available, it is going to return only one book with the name dummy, this is done next let me come to this order book in our book, actually, this is not going to retrieve the book it is going to retrieve the book and of course this book is getting ordered by the user.
So for that what I have done have a new order service that finds one book then I'm checking if the book dot get title equals dummy return no available. Yes, book dot get title abakada successfully supports a mapping Java book, Gacy book, some books, the book titles that are sorry The book title may book order successfully recorded successfully, the people caught up successfully something I want to do, I want to give a good message to the user. That's what I am giving here. I am not returning the book, I am returning a string, okay, because this is being used by a web client. Till now right we have done this, we were calling from one micro service to another micro service. So what actually I want, I want a good object only in this case, I am going to give the information to a client.
So I'm returning a string, it's up to you how you want to frame the method. So, this is our orders controller, I have shown you order service also, let me save this, then let me open this application dot html application that gamble you know it does the same thing. Here I have given the port number 8087 and application name of order service. So our two micro services are ready and you record cover already we have created it and kept it. Let me run all the three replicates. First I need to run Eureka server, then run book service then run order service.
You can see all the three services are getting started. It is ready. Let me go to the browser. First let me open the registry. Both the services have come let me just click this a 089. It says books by author book hyphen by hyphen author slash mo and I bought this name.
Let me just check this out about the output. And again, what other books slash ID the ID is from 9294 I think let me just do 91 I want the output okay. So book service is working perfectly fine. Next, let us take the order service also. Now in this order service slash service Book Search hyphen book slash mogul or I have got something like jack I think she has what is the name here is one so I will just give jack it is also available jack Can you see this and again the next one is one book slash 94 one hyphen book slash 94 okay I bought the output can you see no book ordered successfully? I told you no I'm trying to retrieve the book name face book order successfully am returning a string only.
In this case I'm not trying to show you the Ducati can trying to show you that lesson. So here it is showing not book ordered successfully 94 Okay, if you want you can take it out here what is 94 and minimize appear in book surveys in ViewController here's 94 Can you see this note? on set, everything is working perfectly fine. Now, if you want, we can also check with the fifth six pack. I am just shutting down book service. It is getting terminated, I will go back to the browser.
I'm in Eureka. First let me refresh the registry. Can you see only other services available, I will come back to this order service one book slash 94. Let me refresh it, no book available. So I'm getting the output as expected, both the services are working fine. Now, what I will do is I will go back to eclipse, we already have created a web application.
Let me just show you how to create it and then I will start working on the existing application only. So here first let me run this book service. Let it run. Now I will come back to the package explorer. Here I have created the web client. Let me open this and go to this palm dot XML.
To show you what are the dependencies we want, you know how to create a web application using spring boots, you have to select only web app you need to select, I will just show you go to File, New spring starter project. Here we are just creating a simple web application. So I'm just giving the name as demo one. Let me click next here type web. So this is the dependency which you need to select when you want to create a spring web application using spring boots. So this is done.
Let me just click Cancel. Already I have created an application and I will open SRC main Java. One more thing I forgot to tell when you are creating it you need to select the packaging is what I will open my palm dot XML already now I'm in Palm dot XML only here see the packaging should be one while creating the application itself. You select the packaging as what only then you will be getting In the servlet initializer class, let me open this book web client. Here I have got sublet initializer also, I hope you remember this already we have done that let me just copy this and come here Ctrl V. Again save this okay and I don't need the servlet initializer I will delete it book application is going to access order service. Let us have two pages one is the page where and I'm going to get Mr. Book our data page is the page that it is showing me the final order Okay, so Verizon created SRC main inside webapp right click new search for JSP click Next.
Inside the top I will not be creating it directly I need to have that by in a folder with a hyphen I enough slash views folder inside First I'm going to have some dot JSP Okay, and let it get created. Okay, next I need to create the controller. Let me first rename the starfish. I will go to the combat book client select SRC main Java right click new class. Book controllers are Yeah, book client controller. Click Finish.
Okay, now my controller class at what is the annotation that we have to use here. We have annotated classes at the rate controller, because this is a web application. This is not a REST API. So let me add annotation Next I need to have two methods. The two methods are to get books they are paid Get by ID and again, we are creating all the JSP pages inside view only. So first what I have to do for flash it has to go to Home dot JSP let me add a method.
When any request comes in automatically it has to go to Home dot JSP. Already we have created home dot JSP Let me open it in home dot JSP. I will I am going to have a form in the form I'm just going to get the author name on click up first it will go again to this particular controller and try to connect to other service to get the books by a particular author. Let me add the farm over here first my farm is ready what faction it is first. So in Brookline controller, I need to create a method which will have the record mapping as first and I am retrieving the author value not using PATH variable or do you think request params so let me create a method and book client controller which is going to map the I mean action and also record for the method is very manual record parameter.
What I have to do here, I need to use this template to access a method of order service service template. The next thing is we have to create it as a bean in the main class. I will copy it from some other application here we need to have a trade for the red bean load balance and other stuff. So I will just go to order service in order service already we have done that, so I'm just going to copy it Ctrl C and in Brookline controllers now sorry, not here in book web client Control V. Okay, this is done. Let me come to this book client controller. Now I have this template so it is private read template.
This template. I hope you know how to access a method even if you know that you are and if you know that service name automatically you can access it by just reporting directly. My method is complete I am giving the URL the URL is nothing but the Ura off our order service localhost 8087 slash search books 8089 is for book service, I have given it properly and using this template I'm trying to forget for objects. Till now when you are when you are accessing from one micro service to another micro service, you are returning the list itself, because it's the class of annotated the rest controller here the class is annotated with at the rate controller. So what you need to do you have to return the page name. So my page name is final final dot JSP.
And again here you can just have a look. I have got list books, I need to create the book class which I totally forgot. So I have to create the book class I will copy it from book service average, combat the combat model Ctrl C, and I will come to this book web client going to copy it as such Ctrl V. Oh, it is book app model. As I told you if you want you can change the package name. Now I am just going to rename it refactor rename, I will also change it. It's better combat book line dot model book controller controllers in the same package if you want to get your habit and controller package also.
So it is done, I have to import this book. copy pasting is always a problem because he has imported from somewhere else Ctrl D Ctrl S now I can import it properly. Okay, so I got my and now I need to create final dot JSP in final dot JSP I am going to use jsps with the help of JCL. I'm going to list the books in the form of a table. So first let me create final dot JSP I have created my final dot JSP I have to retrieve the booklets in this particular final dot JSP for that your time That's this booklet to model map already I have done this book line controller, can you see this? It is very simple, just like how we are accessing a rest application using a spring web application, we have done the same procedure only only thing here we have got multiple micro services.
Here I have given map dot add attribute bookless comma bookless. Now, let me come to the final dot JSP I am going to add the code to print the start book using JSP. And for that first you have to add a field to this form dot XML as a dependency and you can list the books in a nice tabular format. Let me add the code and then I will explain you can see this I have added the code I am trying to use AdMob and also I am using JCL part. So, what I have to do I have to add this dependency in Palm dot XML file, open my palm dot XML here it is form dot XML here. I have got What is that VHDL FCL 1.2 version I have added it as a dependency okay.
So, you can just go to the browser and copy paste it, this is done. Let me come back to final dot JSP and just give a space and control this automatically the error is gone Okay. Let me add this get one book also actually, we are not going to show the book details, but we are going to say this particular book have added successfully let me add a method for that. I have added a method here I am going to use PATH variable how actually it is going to come. Let me show you the code that I have written and final dot JSP in final dot JSP. First I'm trying to print the list of books.
Can you see this? I am trying to print a list of books. Then after that, I am giving some action that is next to each book. A button will be available when the user is selecting one particular book automatically that book is getting ordered the is what I have got I have given orders slash book that book ID as a PATH variable. So, this value is taken into this book client controller order slash book ID you will think this PATH variable I am framing the Ura to call the order service okay and again that it is going it is going to the finals okay what is the return type of order so, I mean order book method in order service it is going to return a string so I'm trying to capture this thing you see you are a promising class and capturing the string and then sending the message to the next face here I have not created the third page I'm going to work on the same final dot JSP only.
So, in final dot JSP what dollar message on the top Can you see it is that you are seeing a warning over here this Ronnie has come because it is not able to identify it as jst L. what it can do is you can select it Project and update the project. So that jsps will be added to Maven dependencies and this JSP page can understand that Okay, next are one more thing is left out that is application file, you know what that was should be added to application dot properties. If it is a micro service, you will be giving the port number and the application name by name. But this is a web application, what do you have to do you have to say that I need the prefix and suffix. So, let me add the prefix and suffix. This time, I'm not going to use Yama, I'm going to use application that properties only.
Let me open the file, I have given the prefix and suffix I hope you remember this. This was the app that you put the survey publication. If it is a micro service, we don't need this we are to give the port number and the application name and the application as of now it is going to run on port 8080. All set. Let me run this application. Already my micro services are running.
So this application should open home dot JSP page to select the application, right click run as run on server. Okay, you are running it on server you are that Tomcat server to this particular workspace. You know how to do that already I have done that so my Tomcat server is coming Click Next what book web client click Finish application has started successfully here. This is a web application that is able to see this let me give the name basically this is home dot JSP when it clicks first it has to go to final dot JSP to brought both the book Father Father Mohan let me click the first book. Can you see what Java bukata successfully in final dot JSP we are trying to print the message right? Actually you are supposed to hide all these things.
I have not shown that you can try that out because I have not done it. That title. is coming again. If it doesn't adapt application the scenario is totally different. and here also let me go back whatever page you are seeing this is our final dot JSP only from Final dot JSP it is going to the controller controller accessing the micro service The result is coming back and when I click this order now, again it is coming back to the same page, this is Java This is JC similarly, we can try with some other author also. So, I we have got jack right click jack, I mean type jack, click search order now, I have got deals.
So, this is how you access a micro service using a web application. You can also try one more thing by stopping one service and check whether you are getting no books available and that dummy book book service discriminated. Let me refresh supplicating jack, click search dummy, click off Now no book available, okay of the book service. So I'm getting this dummy book as a booklet. And when I click order now because the book name is dummy, I'm getting no book available. So this is the output.
Thank you.