Microservices Registration and Discovery using Spring Cloud, Eureka
This blog post walks through an implementation pattern and a working setup with multiple microservices where they get registered with Eureka and the client can discover them and invoke them.
What is Eureka
Eureka is a REST-based service that provides support for discovering other services so that clients can use the Eureka service to discover and call them.
Why we need Discovery Service
In a microservices architecture, each service typically runs on a different URL or IP address. Clients need a way to know when new microservices are added or existing ones go down for maintenance. Discovery service provides that mechanism—services register themselves, and clients query the discovery service to know available microservices.
Project Achievement
In this project, we will build:
- A Eureka Discovery Service
- An Article Microservice (CRUD operations)
- A Client Microservice (consumes the Article service)
Goal: invoke a target service without hardcoding URLs, IPs, or ports, by resolving via service discovery.
Technology Used
- Java 11
- Apache Maven 3.5.0
- Spring Boot 2.2.6
- Spring Cloud Hoxton.SR1
- Netflix Ribbon 2.3.0
- Open Feign
- JSON
Discovery Server
Add the Eureka Discovery Server dependency:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
application.properties:
server.port=8761
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
eureka.client.healthcheck.enabled=true
Main Application:
@EnableEurekaServer
@SpringBootApplication
public class ServiceDiscovery {
public static void main(String[] args) {
SpringApplication.run(ServiceDiscovery.class, args);
}
}
Article Service
Add Eureka Client dependency:
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
application.properties:
server.port=9051
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
eureka.client.register-with-eureka=true
spring.application.name=article-service
Main Application:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ArticleMicroservice {
public static void main(String args[]) {
SpringApplication.run(ArticleMicroservice.class, args);
}
}
REST Endpoints:
- GET /api/articles
- GET /api/articles/{id}
- POST /api/articles
- DELETE /api/articles/{id}
Sample Response
[
{
"id": "America-Travel",
"name": "America Travel",
"description": "Places to travel in America ...",
"category": "Travel"
}
]
Article Client Service
This is another Spring Boot service that discovers and calls the Article service using Ribbon/Feign.
Consumer Controller Example:
@RestController
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "render", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> renderArticles() throws Exception {
JsonNode repos =
restTemplate.getForObject("http://article-service-consumer/api/articles", JsonNode.class);
...
}
}
Start Microservices
- Clone repo:
git clone https://github.com/siddharthagit/spring-boot-sdc
- Run each service:
mvn spring-boot:run
- Check discovery server: http://localhost:8761
- Call consumer service: http://localhost:9053/render
Conclusion
We demonstrated how to use Spring Cloud Eureka for service discovery. The client can invoke services without hardcoding addresses, relying on the discovery service to resolve them dynamically.