
Play Store Application link β Spring Boot in 13 steps – App on Google Play
Step 1: Setting Up Microservices
Create Service Projects Using Spring Initializr
Github project link (Order service ) – https://github.com/kuldeep101990/Building-Microservices-with-Spring-Boot-Order
Github project link (Project service ) – https://github.com/kuldeep101990/Building-Microservices-with-Spring-Boot-Product
- Go to https://start.spring.io/.
- Create the following projects:
- Service 1: Product Service
- Dependencies: Spring Web, Spring Boot DevTools
- Service 2: Order Service
- Dependencies: Spring Web, Spring Boot DevTools
- Service 1: Product Service
- Import both projects into your IDE as Maven projects.
Create REST Endpoints
Product Service
package com.example.productservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@GetMapping("/products")
public String getProducts() {
return "List of Products";
}
}
Order Service
package com.example.orderservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@GetMapping("/orders")
public String getOrders() {
return "List of Orders";
}
}
Run each service on different ports (e.g., Product Service: 8081
, Order Service: 8082
).
Step 2: Adding Service Discovery with Spring Cloud Eureka
Github project link – https://github.com/kuldeep101990/Service-Discovery
Create Eureka Server
- Go to Spring Initializr and create a new project:
- Dependencies: Spring Cloud Eureka Server, Spring Boot DevTools
Configure Eureka Server
Add dependencies in pom.xml
:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Enable Eureka Server:
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Configure application.properties
:
server.port=8761
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Register Services with Eureka
Add the following dependency to the Product Service
and Order Service
:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Add Eureka configuration in each serviceβs application.properties
:
Product Service
server.port=8081
spring.application.name=product-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Order Service
server.port=8082
spring.application.name=order-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
Start the Eureka Server, Product Service, and Order Service. Verify that the services are registered in the Eureka dashboard at http://localhost:8761
.
Step 3: Implementing API Gateway with Spring Cloud Gateway
Github project link – https://github.com/kuldeep101990/Spring-Cloud-Gateway
Create API Gateway Project
- Go to Spring Initializr and create a project:
- Dependencies: Spring Cloud Gateway, Spring Boot DevTools
Configure API Gateway
Add the following routes in application.properties
:
server.port=8080
spring.application.name=api-gateway
spring.cloud.gateway.routes[0].id=product-service
spring.cloud.gateway.routes[0].uri=lb://product-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/products/**
spring.cloud.gateway.routes[1].id=order-service
spring.cloud.gateway.routes[1].uri=lb://order-service
spring.cloud.gateway.routes[1].predicates[0]=Path=/orders/**
Start the API Gateway. Access endpoints via:
http://localhost:8080/products
http://localhost:8080/orders
Step 4: Centralized Configuration with Spring Cloud Config
Github project link – https://github.com/kuldeep101990/Spring-Cloud-Config
Create Config Server
- Go to Spring Initializr and create a project with the following dependencies:
- Spring Cloud Config Server
- Spring Boot DevTools
- Enable Config Server:
In theConfigServerApplication.java
class, enable the Config Server by adding the@EnableConfigServer
annotation.package com.example.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args);
}
}
- Configure application.properties:
Update theapplication.properties
file to configure the local folder for storing configurations.server.port=8888
spring.application.name=config-server spring.cloud.config.server.local.uri=file:///C:/config-repo
- Explanation:
file:///C:/config-repo
: Points to the local folder where the configuration files are stored. Update the path based on your system and folder location.
download folder from here – https://github.com/kuldeep101990/config-repo-folder- Make sure the folder contains the configuration files for each service (e.g.,
product-service.properties
,order-service.properties
).
- Explanation:
Connect Services to the Config Server
- Update Product Service and Order Service:
- Add the Spring Cloud Config Client dependency to both
Product Service
andOrder Service
‘spom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- Add the Spring Cloud Config Client dependency to both
- Configure Services to Use Config Server:
- In each service’s
application.properties
, point to the local Config Server:
spring.cloud.config.uri=http://localhost:8888
- In each service’s
Start the Config Server and Services
- Run Config Server:
- Start the Config Server (running on
localhost:8888
). It will read the configurations from the local folder you specified (C:/config-repo
).
- Start the Config Server (running on
- Run Product Service and Order Service:
- Start your Product Service and Order Service. They will fetch their configurations from the Config Server and apply them accordingly.
Test the Setup
- Access the Configured Services:
- The Product Service and Order Service will fetch their configurations from the local Config Server and apply them accordingly.
- You can modify the
application.properties
files in the local folder, and the services will automatically pick up the changes without needing to restart the Config Server.
Note – Run these applications in below order-
- Configuration Server (Step 4):
- This server will provide the settings (like
application.properties
) for the Product Service, Order Service, and API Gateway. - These settings will be picked up from a local folder called
config-repo
.
- This server will provide the settings (like
- Service Discovery (Step 2):
- This is like a phonebook where services can register themselves and find each other.
- The Product Service and Order Service will register themselves on the Eureka Server so other services (like the API Gateway) can find them.
- Example URL for Eureka Server:
http://localhost:8761
.
- Product and Order Services (Step 1):
- These are the main services that handle specific tasks:
- Product Service runs on
http://localhost:8081
. - Order Service runs on
http://localhost:8082
.
- Product Service runs on
- These are the main services that handle specific tasks:
- API Gateway (Step 3):
- This acts as a single entry point to access both services through one common URL.
- Instead of accessing Product Service or Order Service directly, you can access them via the API Gateway at
http://localhost:8080
. - Example URLs via the gateway:
http://localhost:8080/products
(to access Product Service).http://localhost:8080/orders
(to access Order Service).
Conclusion
This guide covered the essential steps to build microservices with Spring Boot:
- Creating microservices.
- Integrating service discovery using Spring Cloud Eureka.
- Implementing an API Gateway with Spring Cloud Gateway.
- Centralizing configuration with Spring Cloud Config.