Eureka Service Discovery
This article covers:
Why Service Discovery?
Setting up a Eureka Server and Eureka Client
Service Discovery
Why Eureka Service Discovery?
In a distributed architecture, it is very crucial to know the physical address (i.e. IP address) of machine where your application is running on cloud. When a microservice is brought up in the cloud, it is assigned with a dynamic IP address. Now other microservices should know this dynamic IP address in order to utilize it, we need a mechanism. This is where service discovery comes into picture.
Spring Cloud Eureka is a service discovery tool for microservices architecture. It is part of the Netflix OSS (Open Source Software) suite and is built on the Spring Framework. In this article, we will explore how to use Netflix Eureka with Spring Boot for service discovery and registration using code examples.
Setting up a Eureka Server and Eureka Client
Eureka Discovery Server
The first step in setting up Eureka is to create a Eureka Server. To create a Eureka Server, we need to create a Spring Boot application and add Spring Cloud Netflix Eureka dependency to the pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Next, we need to add the @EnableEurekaServer annotation to the main class of the project:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
Finally, let's see the configuration for Eureka instance server in the application.properties file:
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Eureka Dashboard
Eureka Dashboard can be accessed at http://localhost:8761. It enables you to check which all services are up and registered with the Eureka Server. Also, you can see all the instances of each service.
Eureka Client
The next step is to create a Eureka Client. A Eureka Client is any microservices in the distributed architecture. This client needs to register with Eureka Server so that other microservices can discover it and use it.
To create a Eureka Client, we need to create a Spring Boot application and add the following dependencies to the pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Next, we need to add the @EnableEurekaClient annotation to the main class of the project:
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
Finally, we need to configure the Eureka Server URL in the application.properties file:
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.application.name = myEurekaClientApplication
Service Discovery
Here we are going to see two ways in which we can invoke a service through Eureka:
Spring Discovery Client
Netflix Feign Client
Spring Discovery Client
Once the Eureka Server and Client are set up, the next step is to use the Eureka Client for service discovery. To use service discovery, we need to use the DiscoveryClient API. To get an instance of the DiscoveryClient API, we can use the following code:
@Autowired
private DiscoveryClient discoveryClient;
To retrieve the list of service instances, we can use the following code:
List<ServiceInstance> instances = discoveryClient.getInstances("service-name");
Now you have the list of ServiceInstance for the microservice. You can apply any custom load balancing algorithm and then use RestTemplate to call the service.
Netflix Feign Client
The Feign library takes a different approach to calling a REST service by having the developer first define a Java interface and then annotating that interface with Spring Cloud annotations to map what Eureka-based service Ribbon will invoke. The Spring Cloud framework will dynamically generate a proxy class that will be used to invoke the targeted REST service. There’s no code being written for calling the service other than an interface definition. This helps to remove the boilerplate code.
Add @EnableFeignClients to the main class of the project:
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Define a Feign Interface for calling particular microservice:
@FeignClient("service-name")
public interface ServiceFeignClient {
@RequestMapping(
method= RequestMethod.GET,
value="contextPath",
consumes="application/json")
String getData(); // should match the method signature of service you are calling
}
For more such articles on spring MVC, spring cloud (Config server, Spring Cloud Netflix Zuul, Spring Cloud Gateway, AWS Cloud), and Spring Boot Tutorial (Restful Web Service, Spring Security) follow me on instagram.
Comentarios