Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Java + Spring Boot + Microservices Example - Step by Step Guide

Hello guys, if you are wondering how to create a Microservice in Java and Spring Boot then you are at the right place. In the past, I have shared best Spring Boot courses for Microservices, Interview Questions as well as essential Microservice design principles and patterns like API Gateway, Circuit breaker, CQRS, Event sourcing etc and In this article, we will cover the fundamentals of microservices with Spring Boot utilizing Spring cloud. We will cover a few fundamental ideas and then we will make a little microservices to give you an outline. I will also share step by step guide to make Microservices in Java and then deploy them using Docker and Kubernetes on Cloud. 

In coming tutorials, I am also planning to cover different kubectl command you can use to start and stop your Microservices, check their status, see the log files and even edit the configuration using configMap so stay tuned for that. 

What is Microservices?

Microservices implies many little services, fabricating little, independent, prepared to run applications. In solid engineering, a mix of a huge number in an application turns into an enormous application that has many hindrances. For instance, in the event that a solid application down, the whole application will be down. Indeed, even keeping a huge solid application is troublesome.

Microservices breaks a huge application to various more modest parts, so it is not difficult to distinguish where the issue happens and furthermore in the event that a part goes down it won't influence the entire application environment. In this article, we will sum up the fundamentals of building microservices with spring boot and spring cloud.

Microservices depend on the single liability guideline. The guideline is connected with gathering things that are influenced by the change. Single responsibility is connected with SOLID design principles. Robert C. Martin made this guideline which expresses a solitary unit will have just a single liability.

Microservices architecture also assists in making applications that with canning be made and overseen as various parts or services. The parts or services are inexactly coupled and sent independently.

Java + Spring Boot + Microservices Example - How to create a Microservice in Java using Spring Boot?

Each service plays out an alternate sort of work and communicates with others utilizing API. Microservices help in making versatility in the engineering. These services have steering like customary UNIX pipes intensive endpoints for data stream. Spring Boot has highlights to make and convey microservices on the cloud and on the venture premises.

For instance, an eCommerce application can have web and versatile UIs conversing with various microservices, for example, client login, client approval, item inventory, request the executives, shopping basket, installment, and conveyance. Docker is utilized as a compartment for every one of the services. Docker-make helps in the organization of the services which are containerized.

You can create Microservices using different frameworks like Spring Boot, Quarkus, Jersey, Restlet, Swagger, and Dropwizard Frameworks. Microservices depend on plan standards, for example, Domain Driven Design, Failure Isolation, Continuous conveyance, Decentralization, and DevOps. Every microservice can be autonomously deployed, updated, replaced, and scaled. (DURS)

More or less, Microservices allows us to convey application code in little and sensible pieces, free of others.

What is Microservices Architecture?

The idea of microservices is straightforward. It ought to break an enormous service with numerous little free services. We should examine a few significant places of Microservices in view of beneath design:

Each micro service has its own information base.  Client API don't have direct admittance to the services. It can associate through API passage. We will enroll each service with the revelation server. The disclosure has data of all the microservices accessible in the framework.

Design server contains every one of the arrangements for the microservices and we will utilize this server to get setup data like hostname, URL and so on for our microservices.

How to create a Microservice in Java using Spring Boot? Example

2.1 Challenges With Microservice Architectures

While fostering various more modest microservices could look simple, yet there are number of intrinsic intricacies that are related with microservices structures. We should check out at a portion of the difficulties:

  • Speedy Setup required : You can't endure a month setting up every microservice. You ought to have the option to rapidly make microservices.

  • Mechanization : Because there are various more modest parts rather than a stone monument, you want to computerize everything - Builds, Deployment, Monitoring and so on.

  • Perceivability : You currently have various more modest parts to send and keep up with. Perhaps 100 or perhaps 1000 parts. You ought to have the option to consequently screen and distinguish issues. You want incredible perceivability around every one of the parts.

  • Limited Context : Deciding the limits of a microservice is definitely not a simple errand. Limited Contexts from Domain Driven Design is a decent beginning stage. How you might interpret the space develops throughout some stretch of time. You want to guarantee that the microservice limits develop.

  • Arrangement Management : You really want to keep up with setups for many parts across conditions. You would require a Configuration Management arrangement.

  • Dynamic Scale Up and Scale Down : The benefits of microservices may be understood in the event that your applications can increased and down effectively in the cloud.

  • Bunch of Cards : If a microservice at the lower part of the call chain falls flat, it can have thump on consequences for any remaining microservices. Microservices ought to be issue lenient by Design.

  • Troubleshooting : When there is an issue that needs examination, you could have to investigate numerous services across various parts. Concentrated Logging and Dashboards are fundamental for make it simple to troubleshoot issues.

  • Consistency : You can't have a large number of devices tackling a similar issue. While it is essential to cultivate advancement, it is additionally vital to have some decentralized administration around the dialects, stages, innovation and devices utilized for carrying out/conveying/checking microservices.

Creating a Simple Microservice Project in Java and Spring

Here is a step by step guide to create Microservices in Java using Spring Boot:

Step 1: Create a Maven project utilizing Spring Initializr      //name of application 

Step 2: Choose the Spring Boot form 2.2.0 M6 or higher adaptation. Try not to pick the Snapshot adaptation.

Step 3: Provide the Group name.

Step 4: Provide the Artifact id. We have given demoProject.

Java + Spring Intializer + Microservice Example

Step 5: Add the accompanying conditions: Spring Web, Spring Boot DevTools, Spring Boot Actuator, Config Client.
Java + Spring Framework + Microservice Example

Step 6: Click on Generate the project button. A compress record will download, separate it into the hard plate.

Step 7: Now, open the eclipse. Import the made maven project. It requires an investment to download the expected records.

Step 8: Once the project is downloaded, go to src/primary/java. Open the DemoProject.

Step 9: Now run the as Java Application.

It began the Tomcat on port(s) 8080 (http).

Presently we will add two or three services in the above project. For this we should follow the accompanying steps:

Step 1: Open document and compose the accompanying code: //name of application

Step 2: Create a class document with name in the envelope src/fundamental/java under the bundle com.example.demoProject and compose the accompanying code:

1.	package com.example.demoProject;  
2.	import org.springframework.web.bind.annotation.GetMapping;  
3.	import org.springframework.web.bind.annotation.RestController;  
4.	import com.example.demoProject.bean.DemoProject;  
5.	@RestController  
6.	public class DemoProjectController   
7.	{  
8.	@GetMapping("/demo")  
9.	public DemoProject retriveValuesFromConfigurations()  
10.	{  
11.	return new DemoProject(100, 1);  
12.	}  
13.	}  

Step 3: Create a class document with name in the envelope src/fundamental/java under the bundle com.example.demoProject.bean and compose the accompanying code:

1.	package com.example.demoProject.bean;  
2.	public class DemoProject   
3.	{  
4.	private int max;  
5.	private int min;  
6.	//no-argument constructor  
7.	protected DemoProject()  
8.	{  
9.	}  
10.	//generating getters  
11.	public int getMax()   
12.	{  
13.	return max;  
14.	}  
15.	public int getMin()   
16.	{  
17.	return min;  
18.	}  
19.	//generating constructor using fields  
20.	public DemoProject(int max, int min)   
21.	{  
22.	super();  
23.	this.max = max;  
24.	this.min = min;  
25.	}  
26.	}  

Type the localhost:8080/demo in the program and press enter, we get the JSON reaction as result.


Java + Spring Boot + Microservice Example

That's all about how to create Microservice in Java and Spring Boot. In this article, we saw the steps of building microservices with Spring Boot. Microservices architecture provides big development and deployment advantages. Here, engineers can chip away at their autonomous modules without an excessive amount of reliance. What's more, on the off chance that a server goes to down it won't down the entire system

Other Java and Spring articles you may like
  • 20 Spring Boot Interview Questions for Java Programmers (questions)
  • 5 Spring Boot Annotations for full-stack Java developers (tutorial)
  • 5 courses to learn Spring Boot and Spring Cloud ( courses)
  • Top 5 Books and Courses to learn RESTful Web Service (books)
  • 10 Tips to become a better Java developer (tips)
  • 5 Courses to learn Spring Cloud and Microservices (courses)
  • 10 Tools Java Developers use in their day-to-day life (tools)
  • 5 Spring Boot Features Every Java Developer Should Know (features)
  • 5 Course to Master Spring Boot online (courses)
  • 10 Things Java Developer should learn (goals)
  • 10 Courses to learn Spring Security with OAuth 2 (courses)
  • 3 ways to change Tomcat port in Spring Boot (tutorial)
  • Top 5 Courses to learn Microservices in Java? (courses)
  • 10 Spring MVC annotations Java developers should learn (annotations)
  • 10 Advanced Spring Boot Courses for Java Programmers (courses)
  • 3 Best Practices Java Programmers can learn from Spring (best practices)

Thanks for reading this article so far. If you found this Java + Spring Boot + Microservice Tutorial useful and helpful then please share them with your colleagues and friends. If you have any questions or feedback then please drop a note. 

P. S. - If you are keen to learn about Microservice but new to this field and looking for free online courses then you can also checkout this list of the 5 best Microservice courses for beginners. In this post you will find free Microservice courses from Udemy, Coursera, and YouTube. 


  1. Just a simple suggestion, I think the colour contrast and theme on your webpage needs to be updated, doesn't matter how much good content you have and it feels like these color stings in the eyes so lots of people just leave the page without reading the content. That's my personal opinion I m could be wrong.

  2. Noted, I will probably change? what color you think would be better suited for readdability?

  3. This is not Microservice program

  4. type localhost:8080/demo in which program.. in browser its not giving any result


Feel free to comment, ask questions if you have any doubt.