{"id":79433,"date":"2025-05-02T11:45:02","date_gmt":"2025-05-02T06:15:02","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=79433"},"modified":"2025-09-03T16:28:59","modified_gmt":"2025-09-03T10:58:59","slug":"guide-to-microservices-and-spring-cloud","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/guide-to-microservices-and-spring-cloud\/","title":{"rendered":"A Beginner&#8217;s Guide to Spring Cloud and Microservices with Practical Examples"},"content":{"rendered":"\n<p>Modern applications need to be <strong>fast, scalable, and easy to manage<\/strong>. This is where <strong>microservices<\/strong> and <strong>Spring Cloud<\/strong> come in. Instead of building one big monolithic app, we break it into smaller parts \u2014 each part is called a <strong>microservice<\/strong>. <strong>Spring Cloud<\/strong> provides tools to manage these parts smoothly.<\/p>\n\n\n\n<p>Let\u2019s explore what microservices are, how Spring Cloud helps, and build a basic example using <strong><a href=\"https:\/\/spring.io\/projects\/spring-boot\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Boot<\/a><\/strong>, <strong>Eureka<\/strong>, and <strong>Spring Cloud Config<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Are Microservices?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Are-Microservices_.jpg\" alt=\"What Are Microservices\" class=\"wp-image-81892\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Are-Microservices_.jpg 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Are-Microservices_-300x158.jpg 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Are-Microservices_-768x403.jpg 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Are-Microservices_-150x79.jpg 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/guide-to-microservices-architecture\/\" target=\"_blank\" rel=\"noreferrer noopener\">Microservices<\/a> are small, independent services that communicate with each other to form a complete application.<\/p>\n\n\n\n<p><strong>Example: Online Shopping App<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Service<\/strong><\/td><td><strong>Role<\/strong><\/td><\/tr><tr><td>User Service<\/td><td>Manages registration, login<\/td><\/tr><tr><td>Product Service<\/td><td>Lists products<\/td><\/tr><tr><td>Order Service<\/td><td>Handles orders<\/td><\/tr><tr><td>Payment Service<\/td><td>Manages payments<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Each of these runs independently, can be developed by different teams, and deployed separately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Use Microservices?<\/strong><\/h3>\n\n\n\n<ul>\n<li><strong>Easy to develop and test<\/strong><strong><br><\/strong><\/li>\n\n\n\n<li><strong>Scalable<\/strong> \u2014 only scale services that need it<br><\/li>\n\n\n\n<li><strong>Resilient<\/strong> \u2014 one service can fail without breaking the whole app<br><\/li>\n\n\n\n<li><strong>Flexible<\/strong> \u2014 each service can use different tech stacks (<a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>, Node.js, etc.)<\/li>\n<\/ul>\n\n\n\n<p><em>Are you interested in starting your career in microservices? Enroll in HCL Guvi&#8217;s course on <a href=\"https:\/\/www.guvi.in\/courses\/database-and-cloud-computing\/application-development-using-microservices-and-serverless\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=A+Beginner+Guide+to+Spring+Cloud+and+Microservices+with+Practical+Examples\" target=\"_blank\" rel=\"noreferrer noopener\">Application Development using Microservices and Serverless.<\/a> This course covers all the necessary topics of microservices from the basic to the advanced level. You will gain hands-on experience in building real-world applications using serverless architecture and industry-recognized certifications.<\/em><\/p>\n\n\n\n<p><strong>Explore: <a href=\"https:\/\/www.guvi.in\/blog\/spring-boot-3-x-interview-prep\/\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Boot 3.x Interview Prep: Latest 50 Common Questions<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is Spring Cloud?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Is-Spring-Cloud_.jpg\" alt=\"What Is Spring Cloud\" class=\"wp-image-81891\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Is-Spring-Cloud_.jpg 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Is-Spring-Cloud_-300x158.jpg 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Is-Spring-Cloud_-768x403.jpg 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/What-Is-Spring-Cloud_-150x79.jpg 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Spring Cloud is a collection of tools built on top of <strong><a href=\"https:\/\/www.guvi.in\/blog\/what-is-spring-boot-ai\/\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Boot<\/a><\/strong> that help you build microservices. It provides solutions for:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Feature<\/strong><\/td><td><strong>Tool<\/strong><\/td><\/tr><tr><td>Service Discovery<\/td><td>Eureka<\/td><\/tr><tr><td>Centralized Configuration<\/td><td>Spring Cloud Config<\/td><\/tr><tr><td>Routing<\/td><td>API Gateway<\/td><\/tr><tr><td>Fault Tolerance<\/td><td>Resilience4j<\/td><\/tr><tr><td>Load Balancing<\/td><td>Spring Cloud LoadBalancer<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step-by-Step Example<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/Step-by-Step-Example.jpg\" alt=\"Step-by-Step Example\" class=\"wp-image-81893\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/Step-by-Step-Example.jpg 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/Step-by-Step-Example-300x158.jpg 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/Step-by-Step-Example-768x403.jpg 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/Step-by-Step-Example-150x79.jpg 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s build a simple microservices setup with:<\/p>\n\n\n\n<ol>\n<li><strong>Eureka Server<\/strong><strong><br><\/strong><\/li>\n\n\n\n<li><strong>Config Server<\/strong><strong><br><\/strong><\/li>\n\n\n\n<li><strong>Order Service<\/strong><strong><br><\/strong><\/li>\n\n\n\n<li><strong>Product Service<\/strong><strong><br><\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Setting Up Eureka Server<\/strong><\/h3>\n\n\n\n<p><strong>Eureka<\/strong> is like a phone book. It keeps track of all services and their locations.<\/p>\n\n\n\n<p><strong>Add dependency in pom.xml:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&lt;<strong>dependency<\/strong>&gt;<br>&nbsp; &lt;<strong>groupId<\/strong>&gt;org.springframework.cloud&lt;\/<strong>groupId<\/strong>&gt;<br>&nbsp; &lt;<strong>artifactId<\/strong>&gt;spring-cloud-starter-netflix-eureka-server&lt;\/<strong>artifactId<\/strong>&gt;<br>&lt;\/<strong>dependency<\/strong>&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Main Class:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>@SpringBootApplication<br>@EnableEurekaServer<br>public class EurekaServerApplication {<br>&nbsp; public static void main(String[] args) {<br>&nbsp; &nbsp; SpringApplication.run(EurekaServerApplication.class, args);<br>&nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>application.yml:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>server:<br>&nbsp; port: 8761<br><br>eureka:<br>&nbsp; client:<br>&nbsp; &nbsp; register-with-eureka: false<br>&nbsp; &nbsp; fetch-registry: false<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Run this app and visit <code>http:\/\/localhost:8761<\/code> \u2014 The Eureka dashboard will show up!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Setting Up Config Server<\/strong><\/h3>\n\n\n\n<p>A <strong>Config Server<\/strong> allows you to keep all configuration files (like port numbers, database URLs) in one central place (usually a Git repo).<\/p>\n\n\n\n<p><strong>Add dependency:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&lt;<strong>dependency<\/strong>&gt;<br>&nbsp; &lt;<strong>groupId<\/strong>&gt;org.springframework.cloud&lt;\/<strong>groupId<\/strong>&gt;<br>&nbsp; &lt;<strong>artifactId<\/strong>&gt;spring-cloud-config-server&lt;\/<strong>artifactId<\/strong>&gt;<br>&lt;\/<strong>dependency<\/strong>&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Main Class:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>@SpringBootApplication<br>@EnableConfigServer<br>public class ConfigServerApplication {<br>&nbsp; public static void main(String[] args) {<br>&nbsp; &nbsp; SpringApplication.run(ConfigServerApplication.class, args);<br>&nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>application.yml:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>server:<br>&nbsp; port: 8888<br><br>spring:<br>&nbsp; cloud:<br>&nbsp; &nbsp; config:<br>&nbsp; &nbsp; &nbsp; server:<br>&nbsp; &nbsp; &nbsp; &nbsp; git:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uri: https:\/\/github.com\/your-repo\/config-files<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Place your microservices&#8217; config files in that Git repo (e.g., <code>order-service.yml<\/code>, <code>product-service.yml<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Create the Order Service<\/strong><\/h3>\n\n\n\n<p><strong>Dependencies:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&lt;<strong>dependency<\/strong>&gt;<br>&nbsp; &lt;<strong>groupId<\/strong>&gt;org.springframework.cloud&lt;\/<strong>groupId<\/strong>&gt;<br>&nbsp; &lt;<strong>artifactId<\/strong>&gt;spring-cloud-starter-netflix-eureka-client&lt;\/<strong>artifactId<\/strong>&gt;<br>&lt;\/<strong>dependency<\/strong>&gt;<br><br>&lt;<strong>dependency<\/strong>&gt;<br>&nbsp; &lt;<strong>groupId<\/strong>&gt;org.springframework.cloud&lt;\/<strong>groupId<\/strong>&gt;<br>&nbsp; &lt;<strong>artifactId<\/strong>&gt;spring-cloud-starter-config&lt;\/<strong>artifactId<\/strong>&gt;<br>&lt;\/<strong>dependency<\/strong>&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>application.yml<\/strong> (fetched from config server):<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>spring:<br>&nbsp; application:<br>&nbsp; &nbsp; name: order-service<br><br>server:<br>&nbsp; port: 8081<br><br>eureka:<br>&nbsp; client:<br>&nbsp; &nbsp; service-url:<br>&nbsp; &nbsp; &nbsp; defaultZone: http:\/\/localhost:8761\/eureka<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Main Class:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>@SpringBootApplication<br>@EnableEurekaClient<br>public class OrderServiceApplication {<br>&nbsp; public static void main(String[] args) {<br>&nbsp; &nbsp; SpringApplication.run(OrderServiceApplication.class, args);<br>&nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Controller:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>@RestController<br>@RequestMapping(&#8220;\/orders&#8221;)<br>public class OrderController {<br><br>&nbsp; @GetMapping<br>&nbsp; public String getOrders() {<br>&nbsp; &nbsp; return &#8220;List of Orders&#8221;;<br>&nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Create the Product Service<\/strong><\/h3>\n\n\n\n<p>Repeat similar steps as Order Service, with a different port and app name.<\/p>\n\n\n\n<p><strong>application.yml (via config server):<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>spring:<br>&nbsp; application:<br>&nbsp; &nbsp; name: product-service<br><br>server:<br>&nbsp; port: 8082<br><br>eureka:<br>&nbsp; client:<br>&nbsp; &nbsp; service-url:<br>&nbsp; &nbsp; &nbsp; defaultZone: http:\/\/localhost:8761\/eureka<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Controller:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>@RestController<br>@RequestMapping(&#8220;\/products&#8221;)<br>public class ProductController {<br><br>&nbsp; @GetMapping<br>&nbsp; public String getProducts() {<br>&nbsp; &nbsp; return &#8220;List of Products&#8221;;<br>&nbsp; }<br>}<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing the Setup<\/strong><\/h2>\n\n\n\n<p>To test the setup, you have to follow the steps mentioned below for smoother execution.<\/p>\n\n\n\n<ol>\n<li>Run Eureka Server \u2192 http:\/\/localhost:8761<br><\/li>\n\n\n\n<li>Run Config Server<br><\/li>\n\n\n\n<li>Run Order and Product Services<br><\/li>\n\n\n\n<li>Visit the Eureka dashboard \u2014 you\u2019ll see both services listed<br><\/li>\n\n\n\n<li>Access:<br>\n<ul>\n<li><code>http:\/\/localhost:8081\/orders<\/code><\/li>\n\n\n\n<li><code>http:\/\/localhost:8082\/products<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Bonus: Add API Gateway (Optional)<\/strong><\/h3>\n\n\n\n<p>Add Spring Cloud Gateway to route requests to the proper services:<\/p>\n\n\n\n<p><strong>application.yml:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>spring:<br>&nbsp; cloud:<br>&nbsp; &nbsp; gateway:<br>&nbsp; &nbsp; &nbsp; routes:<br>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; id: order-service<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uri: lb:\/\/order-service<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; Path=\/orders\/**<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; id: product-service<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uri: lb:\/\/product-service<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; predicates:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; Path=\/products\/**<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now you can call everything from a single endpoint like:<\/p>\n\n\n\n<ul>\n<li><code>http:\/\/localhost:8080\/orders<\/code><br><\/li>\n\n\n\n<li><code>http:\/\/localhost:8080\/products<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>You\u2019ve now gained a solid understanding of how to break down a monolithic application into microservices, allowing for better scalability and maintainability. You\u2019ve also explored key Spring Cloud tools such as Eureka for service discovery and Config Server for centralized configuration management. Finally, you\u2019ve learned how to connect and run multiple services together, enabling them to communicate effectively within a distributed system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modern applications need to be fast, scalable, and easy to manage. This is where microservices and Spring Cloud come in. Instead of building one big monolithic app, we break it into smaller parts \u2014 each part is called a microservice. Spring Cloud provides tools to manage these parts smoothly. Let\u2019s explore what microservices are, how [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":81894,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[843],"tags":[],"views":"2481","authorinfo":{"name":"Lavish Jain","url":"https:\/\/www.guvi.in\/blog\/author\/lavish-jain\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/A-Beginners-Guide-to-Spring-Cloud-and-Microservices-300x116.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/06\/A-Beginners-Guide-to-Spring-Cloud-and-Microservices.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79433"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=79433"}],"version-history":[{"count":7,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79433\/revisions"}],"predecessor-version":[{"id":86298,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/79433\/revisions\/86298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/81894"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=79433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=79433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=79433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}