In the article, we will discuss how to implement a microservice registry with Eureka. The microservice registry is a database of the instances of the microservices. It stores its locations. The instances of the service are registered with the registry service on startup and deregistered automatically at the time of shutdown. Netflix provides a registry service server that is Eureka. Spring Boot provides integration with the Netflix API, so we can easily implement a microservice registry using Netflix’s Eureka server.
In the previous article, we have discussed how to discover a microservices and also explained microservices discovery patterns such as the client-side discovery and server-side discovery patterns.
The client service, or external routers, make a query to find the available instances of a service. The registry server provides all of the available instances of the requested service. Take a look at the following diagram, which shows Service Registry and discovery with Eureka:
As you can see, all services register with the Eureka server to make themselves available. In the following section, we’ll look at how to register services with Eureka.
We can use the Eureka registry server with Spring Cloud because it integrates with Netflix. First, add the following Maven dependency into your application’s pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
Let’s create a service, the Account Service, and register it with the Eureka server. The application class of the Account Service in Spring Boot will look as follows:
package com.dineshonjava.bookshop.accountservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class AccountServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountServiceApplication.class, args);
}
}
As you can see in the preceding code, the @EnableEurekaClient annotation activates the Netflix EurekaClient implementation. This Account Service registers itself with the Eureka server, which makes it available. Take a look at the following YML configuration file for this Account Service application:
spring:
application:
name: account-service
server:
port: 1111
eureka:
client:
service-url:
default-zone: ${EUREKA_URI:http://localhost:8761/eureka}
instance:
prefer-ip-address: true
As you can see, the service name is account-service, and it will run on port 1111. This service registers itself with the Eureka server running on http://localhost:8761/eureka.
In the following section, we will implement the Eureka server with Spring Boot.
It is very simple to implement the Eureka server application using Spring Boot; just use the following code:
package com.dineshonjava.bookshop.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
As you can see in the preceding code, the Eureka server is a small Spring Boot application. The @EnableEurekaServer annotation provides Netflix’s Eureka registry server. Take a look at the following Eureka server application configuration file for more information:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
This server application uses port 8761.
Now, let’s take a look at the following screenshot:
As you can see, the ACCOUNT-SERVICE has now been successfully registered with the Eureka server.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…