Setting Up Eureka Service (Service Discovery & Registration)
Introduction
One of the primary goals of maintaining a distributed systems architecture is to achieve fault-tolerance and load balancing. Hence, it’s no surprise that we usually have multiple redundant services essentially doing the same thing. We can use DNS to keep track of the address and port of these services; however, DNS is fairly static and in the microservices world, and especially in the cloud, services come and go fairly frequently. That is why we need another separate service, commonly referred as Service Discovery and Registration, to keep track of a registry of available services and their corresponding locations. Since Spring Cloud Netflix has integrated Netflix’s Eureka easily to Spring Boot, we’ll use Eureka for as our Service Discovery and Registration. Two primary components make up Spring Cloud Eureka:
spring-cloud-eureka-server
: a super easy way to set up your Service Discovery and Registration server (Eureka Server)spring-cloud-starter-eureka
: easy plug-in dependency to set up your client to register as a service and fetch list of services from the registry
In summary, Service Registry is a logical mapping between a service id/ name to a hostname and port combination.
Setting up Eureka Service’s Configuration
In the existing config’s git:
vim eureka-service.properties
### Add the below to eureka-service.properties
server.port=${PORT:8761}
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
## End of eureka-service.properties
git add eureka-service.properties
git commit -m "Add eureka-service.properties"
NOTE: the reason we set eureka.client.register-with-eureka
and eureka.client.fetch-registry
to false
is that because eureka-server has eureka-client built-in in as well. Hence, we need to prevent the client for registering and querying itself (causing unnecessary infinite loop).
Setting up Eureka Service
-
Go to https://start.spring.io/
-
Type
eureka-service
as the “Artifact” name, addEureka Server
&Config Client
dependencies, and “Generate Project”:
NOTE: the reason we need Config Client
is such that we can connect to our previously set up config-service
to retrieve centrally hosted configuration
-
Open the project in your favorite Java IDE (mine is IntelliJ)
-
Add
@EnableEurekaServer
insrc/main/java/EurekaServiceApplication
:package com.example.eurekaservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class, args); } }
-
in
src/main/resources
, create a new file calledbootstrap.properties
, add the below:# tell config service what is the name of the service and corresponding config to retrieve spring.application.name=eureka-service # tell the location the location of config server spring.cloud.config.uri=http://localhost:8888
Note: Why
bootstrap.properties
? Refer to: https://stackoverflow.com/questions/32997352/what-is-the-diference-between-putting-a-property-on-application-yml-or-bootstrap -
Run
EurekaServiceApplication
and voila! Your Eureka server is started! -
You can check the following URL to see the Eureka’s Dashboard: http://localhost:8761/
THINGS TO NOTE:
- Again, it’s just so convenient to bootstrap a fully functioning Service Discovery & Registration server by just a single annotation!
- You will see that our
eureka-service
is running on port 8761. How does it now that? From theserver.port
we configured ineureka-service.properties
. And how did our service know how to talk to ourconfig-service
? We configured it to haveConfig Client
(akaspring-cloud-starter-config
in thepom.xml
) and tell the URL of the config service inbootstrap.properties
. - Right now, we haven’t registered any service to Eureka yet. In a later tutorial, you’ll see how you can register your service to Eureka and see how Eureka keeps track of it when it’s terminated.
Reference
http://www.baeldung.com/spring-cloud-netflix-eureka