DEV Community

Cover image for How to Configure Eureka Server
Eduardo
Eduardo

Posted on

How to Configure Eureka Server

The goal of Eureka Server is to locate and register services you want to interact with. Additionally, it helps with load balancing and fault tolerance.

To configure Eureka Server, there must be at least two types of applications: one application acting as the server and another as the client.

Each service must communicate with the Eureka Server to tell it that it is available for use. Eureka Server will store its information and status. This communication between the microservice and Eureka is called heartbeats and occurs every 30 seconds. If 3 heartbeats fail, Eureka Server removes it from the list.

1. Configure the Eureka Server Project

To create the project, we can use the Spring Boot Initializr tool or the wizard in your preferred IDE. For this example, Spring Boot version 2.7.0 will be used.

During project creation, we must ensure we select "Eureka Server" (usually listed as Eureka Server in Initializr) so that our project has the necessary libraries to perform the example.

Once the project is created, we must configure our properties file (application.yml) as follows:

eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
spring:
  application:
    name: servicio-eureka-server
server:
  port: 8761
Enter fullscreen mode Exit fullscreen mode

The configuration inside the eureka node prevents the Eureka Server application from registering itself. The spring node gives the name to the application we are working on, and the server node indicates the port on which the application will run. By convention, Eureka Server is assigned this port number (8761).

Finally, we must add the @EnableEurekaServer annotation to our class containing the main method.

@EnableEurekaServer
@SpringBootApplication
public class SpringBootEurekaServerApplication {

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

To test the application, we run it and go to http://localhost:8761, where we can observe the Eureka Server dashboard.

Eureka Server

2. Configuration of a Eureka Client

To create the application that will connect to Eureka Server, we follow a similar process to the previous one using Spring Boot Initializr or our preferred IDE. However, now we must ensure we select "Eureka Discovery Client" so that it can be correctly registered in Eureka Server.

Now we configure the application.yml of the Eureka Client as follows:

spring:
  application:
    name: eureka-client
server:
  port: ${PORT:0}
eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
Enter fullscreen mode Exit fullscreen mode

As we saw earlier, the spring.application.name node gives the application its name. server.port indicates the communication port where the application will start; in this case, we are not setting a specific port but leaving it dynamic (${PORT:0}).

Now, the instance-id node is necessary to be able to run on a different random port with a different random instance ID. In this case, instance-id is formed by the application name and a random value.

Now we must declare in the main method that this is a Eureka Client:

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

To verify that Eureka Server registers the services correctly, we first start Eureka Server, and then we can start the Eureka Client application twice to verify that the ports do not clash and that the port number and instance ID are random.

Now we go to http://localhost:8761 to verify this. We can notice that the ports did not clash, they each have a different random ID, and they are correctly registered in Eureka Server.

3. How to Consume a Eureka Client Service

Now we are going to build a service that makes a request to our service called Eureka Client. To do this, we are going to modify our Eureka Client service slightly by adding a Rest Controller, and we will create a new service that functions as a Rest client.

Rest Controller in Eureka Client
Inside the Eureka Client Project, we are first going to create a POJO Object that will be returned to be visualized as a result.

public class Item implements Serializable {
    private static final long serialVersionUID = 129348938L;
    private int i;
    private String name;

    public Item() {
    }

    public Item(int i, String name) {
        this.i = i;
        this.name = name;
    }

    // Getters, Setters, Equals and HashCode
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // ... equals and hashCode methods
}
Enter fullscreen mode Exit fullscreen mode

Then we create some dummy business logic where we obtain a list of Item objects.

public interface ItemService {
    List<Item> findAll();
}
Enter fullscreen mode Exit fullscreen mode
@Service
public class ItemServiceImpl implements ItemService {
    @Override
    public List<Item> findAll() {
        List<Item> items = new ArrayList<>();
        Item item;
        for(int i = 0 ; i < 10; i++){
            item = new Item(i, "Item " + i);
            items.add(item);
        }
        return items;
    }
}
Enter fullscreen mode Exit fullscreen mode

And we link it with the controller.

@RestController
public class ItemController {

    private static Logger logger = LoggerFactory.getLogger(ItemController.class);

    @Autowired
    private ItemService itemService;

    @GetMapping(path = "/listing", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Item>> listing(){
        List<Item> all = itemService.findAll();
        return new ResponseEntity<>(all, HttpStatus.OK);
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Spring Boot Feign Client

Now we are going to create a new project that will serve as a client for our Microservice using Feign.

First, we create an object representing Item in this project as well (similar to the code above).

Now we create the Feign client. In the name property, we put the name of the service exactly as we registered it in Eureka Server.

You might notice that no URL or port is configured within this connection. This is thanks to Eureka and the fact that we configured a random port in the Eureka Client. Now we can spin up N number of services with random ports and everything will work transparently.

@FeignClient(name = "eureka-client")
public interface ProductRestClient {

    @GetMapping(path = "/listing")
    public List<ItemDto> list();
}
Enter fullscreen mode Exit fullscreen mode
public interface ItemService {
    public List<ItemDto> findAll();
}
Enter fullscreen mode Exit fullscreen mode
@Service
public class ItemServiceFeign implements ItemService {

    @Autowired
    private ProductRestClient feignClient; // Feign Client

    @Override
    public List<ItemDto> findAll() {
        return feignClient.list();
    }
}
Enter fullscreen mode Exit fullscreen mode

We create the Controller for the Feign client and configure the project properties.

@RestController
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping(path = "/list")
    public List<ItemDto> list(){
        return itemService.findAll();
    }
}
Enter fullscreen mode Exit fullscreen mode
spring:
  application:
    name: feign-client
server:
  port: 8002
Enter fullscreen mode Exit fullscreen mode

Our example is now ready to be tested. Using any browser, we can go to the URL http://localhost:8002/list and we will get the JSON result.

Conclusions

In this post, we were able to see how Eureka helps us scale our services, and makes communication between them easier. We created an example to observe how it behaves and how simple it is to configure Eureka Server. Now we can use it for our projects or to create our portfolio.

Relevant Links

Top comments (0)