This guide walks you through the process of standing up and consuming the Netflix Eureka service registry.
You’ll setup a Netflix Eureka service registry and then build a client that both registers itself with the registry and uses it to resolve its own host. A service registry is useful because it enables client-side load-balancing and decouples service providers from consumers without the need for DNS.
eureka-service/build.gradle
link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-service/build.gradle[role=include]
eureka-client/build.gradle
link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-client/build.gradle[role=include]
eureka-service/pom.xml
link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-service/pom.xml[role=include]
eureka-client/pom.xml
link:https://raw.githubusercontent.com/spring-guides/gs-service-registration-and-discovery/master/initial/eureka-client/pom.xml[role=include]
You’ll first need a Eureka Service registry. You can use Spring Cloud’s @EnableEurekaServer
to stand up a registry that other applications can talk to. This is a regular Spring Boot application with one annotation added to enable the service registry.
eureka-service/src/main/java/hello/EurekaServiceApplication.java
link:complete/eureka-service/src/main/java/hello/EurekaServiceApplication.java[role=include]
When the registry starts up it will complain, with a stacktrace, that there are no replica nodes for the registry to connect to. In a production environment, you will want more than one instance of the registry. For our simple purposes, however, it sufficies to disable the relevant logging.
By default, the registry will also attempt to register itself, so you’ll need to disable that, as well.
It’s a good convention to put this registry on a separate port when using it locally.
Add some properties to your eureka-service/src/main/resources/application.properties
to handle all of these requirements.
eureka-service/src/main/resources/application.properties
link:complete/eureka-service/src/main/resources/application.properties[role=include]
Now that we’ve stood up a service registry, let’s stand up a client that both registers itself with the registry and uses the Spring Cloud DiscoveryClient
abstraction to interrogate the registry for it’s own host and port. The @EnableDiscoveryClient
activates the Netflix Eureka DiscoveryClient
implementation. There are other implementations for other service registries like Hashicorp’s Consul or Apache Zookeeper.
eureka-client/src/main/java/hello/EurekaClientApplication.java
link:complete/eureka-client/src/main/java/hello/EurekaClientApplication.java[role=include]
Whatever implementation you choose, you’ll soon see the eureka-client
registered under whatever name you specify in the spring.application.name
property. This property is used a lot in Spring Cloud, often in the earliest phases of a service’s configuration. This property is used in service bootstrap and so by convention lives in eureka-client/src/main/resources/bootstrap.properties
where it’s found before src/main/resources/application.properties
.
eureka-client/src/main/resources/bootstrap.properties
link:complete/eureka-client/src/main/resources/bootstrap.properties[role=include]
The eureka-client
defines a Spring MVC REST endpoint, ServiceInstanceRestController
, that returns an enumeration of all the ServiceInstance
instances registered in the registry at http://localhost:8080/service-instances/a-bootiful-client
. Consult the Building a RESTful Web Service guide to learn more about building REST services with Spring MVC and Spring Boot.
Test the end-to-end result by starting the eureka-service
first and then, once loaded, starting the eureka-client
. The eureka-client
will take about a minute to register itself in the registry and to refresh its own list of registered instances from the registry. All of these thresholds are configurable. Visit the eureka-client
in the browser, http://localhost:8080/service-instances/a-bootiful-client
. There, you should see the ServiceInstance
for the eureka-client
reflected in the response.
Congratulations! You’ve just used Spring to stand up a Netflix Eureka service registry and to use that registry in a client application.