DEV Community

Mukul Bindal
Mukul Bindal

Posted on

Configuring and Using Eureka Discovery

Configuring and using Eureka Discovery Server in Spring Cloud is a fundamental step in building microservices-based applications. Below is a guide on how to set up and utilize Eureka in your spring boot application.

Step 1: Set Up a Spring Boot Project
Start by creating a Spring Boot project using your preferred IDE or Spring Initializer (https://start.spring.io/). Make sure to include the "Eureka Server" and "Eureka Discovery" dependencies.

Step 2: Configure Eureka Server
In your Spring Boot application, you need to configure it as a Eureka Server. Create a main class and annotate it with @EnableEurekaServer:

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

In your application.properties or application.yml, specify the server port and any other configurations:

server.port=8761
spring.application.name=eureka-server
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Eureka Client(s)
Now, you'll create one or more Eureka clients, which are your microservices that register with the Eureka Server. In your microservice project(s), add the Eureka Client dependency.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

In your microservice's application.properties or application.yml, specify the Eureka Server's location:

eureka.client.service-url.default-zone=http://localhost:8761/eureka
Enter fullscreen mode Exit fullscreen mode

Step 4: Register Microservices
In each of your microservices, annotate the main class with @EnableDiscoveryClient:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class YourMicroserviceApplication {

    public static void main(String[] args) {
        SpringApplication.run(YourMicroserviceApplication.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Test Eureka

  • Start your Eureka Server application.
  • Start your microservices (Eureka Clients).
  • Access the Eureka Server's dashboard in your browser: http://localhost:8761.

You should see your registered microservices listed in the dashboard.

Step 6: Load Balancing
Eureka also provides client-side load balancing. You can use the @LoadBalanced annotation with your RestTemplate or WebClient to make requests to other services registered with Eureka. For example:

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
Enter fullscreen mode Exit fullscreen mode

Example Use Case:
Suppose you have a "ProductService" and a "UserService" as microservices. You can use the @LoadBalanced RestTemplate to make requests from the "UserService" to the "ProductService" without hardcoding the URL.

That's a basic guide on how to configure and use Eureka Discovery Server in Spring Cloud. You can extend this architecture to build complex microservices ecosystems with ease.

Top comments (0)