Service Discovery with
Spring Cloud Eureka
Implementing Passive Service Discovery
What is it, and why would you use it?
Copyright Ken Krueger 2015
Objectives
● At the end of this module, you will be able to
– Explain what Passive Service Discovery is
– Build and Run and Spring Cloud Eureka Server
– Build, Run, and Configure a Eureka Client
Copyright Ken Krueger 2015
Module Outline
● Service Discovery
● Eureka Server
● Discovery Client
● Service Discovery Considerations
Copyright Ken Krueger 2015
Service Discovery - Analogy
● When you sign into a chat client, what
happens?
– Client 'registers' itself with the server – server knows you are
online.
– The server provides you with a list of all the other known clients
● In essence, you client has “discovered” the
other clients
– ...and has itself been “discovered” by others
Copyright Ken Krueger 2015
Service Discovery
● Microservice architectures result in large numbers of inter-service calls
– Very challenging to configure
● How can one application easily find all of the other runtime dependencies?
– Manual configuration – Impractical, brittle
● Service Discovery provides a single 'lookup' service.
– Clients register themselves, discover other registrants.
– Solutions: Eureka, Consul, Etcd, Zookeeper, SmartStack, etc.
Copyright Ken Krueger 2015
Eureka –
Service Discovery Server and Client
● Part of Spring Cloud Netflix
– Battle tested by Netflix
● Eureka provides a 'lookup' server.
– Generally made highly available by running multiple copies
– Copies replicate state of registered services.
● “Client” Services register with Eureka
– Provide metadata on host, port, health indicator URL, etc.
● Client Services send heartbeats to Eureka
– Eureka removes services without heartbeats.
Copyright Ken Krueger 2015
Module Outline
● Service Discovery
● Eureka Server
● Discovery Client
● Service Discovery Considerations
Copyright Ken Krueger 2015
Making a Eureka Server – Building, part 1
● Include minimal dependencies in <parent>
your POM (or Gradle) <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
– Spring Boot Starter Parent <version>1.5.2.RELEASE</version>
</parent>
– Spring Cloud Starter Parent ...
<dependencyManagement>
– Spring Cloud Starter Eureka <dependencies>
Server <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.RELEASE</version>
...
</dependencyManagement>
Does this look familiar? <dependencies>
– Same parent as config server <dependency>
– “cloud-starter-eureka-server” instead <groupId>org.springframework.cloud</groupId>
of “cloud-config-server” <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
Copyright Ken Krueger 2015
Making a Eureka Server – Building, part 2
● Switch Eureka on with @EnableEurekaServer:
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
● That's It!
Copyright Ken Krueger 2015
Multiple Servers
● Typically, multiple Eureka servers should be run simultaneously
– Otherwise, you'll get many warnings in the log
– Eureka servers communicate with each other to share state
– Provides High Availability
● Each server should know URL to the others
– Can be provided by Config Server
– One server (JAR), multiple profiles
Shared State
Eureka Server Eureka Server Eureka Server
Region: US West Region: US East Region: EU (Frankfurt)
Copyright Ken Krueger 2015
Multiple Servers
Configuration
● Common Configuration Options for Eureka Server:
– See https://github.com/Netflix/eureka/wiki/Configuring-Eureka for full list.
Control http port (any boot application)
server:
port: 8011
eureka:
instance:
statusPageUrlPath: ${management.contextPath}/info
healthCheckUrlPath: ${management.contextPath}/health
hostname: localhost
Enables silent running of a single instance (non-production)
client:
registerWithEureka: false
fetchRegistry: false
Comma separated list
serviceUrl:
defaultZone: http://server:port/eureka/,http://server:port/eureka/
Copyright Ken Krueger 2015
Module Outline
● Service Discovery
● Eureka Server
● Discovery Client
● Service Discovery Considerations
Copyright Ken Krueger 2015
Registering with Eureka – Part 1
● Use the Spring Boot Starter parent as a Parent POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
...And use Dependency management section for Spring Cloud:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency> Look familiar?
</dependencies> exactly the same options
</dependencyManagement> as a spring cloud config client.
Copyright Ken Krueger 2015
Registering with Eureka – Part 1
● Add Dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
● ...enable:
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
Default fallback.
} Any Eureka instance will do
(we usually want several comma-separated
● ...and specify the location of the Eureka server: URLs)
# application.properties
eureka.client.serviceUrl.defaultZone: http://server:8761/eureka/
Copyright Ken Krueger 2015
@EnableDiscoveryClient
● Automatically registers client with Eureka server
– Registers the application name, host, and port
● Using values from the Spring Environment.
● But can be overridden.
● Give your application a spring.application.name
● Makes this app an “instance” and a “client” Service ID (Eureka VIP)
Corresponds to
– It can locate other services spring.application.name
@Autowired DiscoveryClient client;
public URI storeServiceUrl() {
List<ServiceInstance> list = client.getInstances("STORES");
if (list != null && list.size() > 0 ) {
return list.get(0).getUri();
}
return null;
}
Copyright Ken Krueger 2015
Module Outline
● Service Discovery
● Eureka Server
● Discovery Client
● Service Discovery Considerations
Copyright Ken Krueger 2015
What is a Zone?
● Eureka server designed for multi-instance use
– Single instance will actually warn you when it runs without any peers!
● Eureka Server does not persist service registrations
– Relies on client registrations; always up to date, always in memory
● Stateful application.
● Typical production usage – many Eureka server instances running
in different availability zones / regions
– Connected to each other as “peers”
Copyright Ken Krueger 2015
Which Comes First?
Eureka or Config Server?
● Config First Bootstrap (default) – Use Config Server to configure location of
Eureka server
– Implies spring.cloud.config.uri configured in each app
● Eureka First Bootstrap – Use Eureka to expose location to config server
– Config server is just another client
– Implies spring.cloud.config.discovery.enabled=true and
eureka.client.serviceUrl.defaultZone configured in each app.
– Client makes two network trips to obtain configuration.
Copyright Ken Krueger 2015
VIP – Indicating Client Type
Copyright Ken Krueger 2015
Health Check URL
Actuator
Copyright Ken Krueger 2015
References
● http://techblog.netflix.com/2012/09/eureka.html
● https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-
with-spring-cloud-and-netflix-s-eureka
Copyright Ken Krueger 2015
Summary
● Passive Service Discovery -
– Having services register themselves and find others
automatically
● Spring Cloud Eureka Server
– Convenient wrapper around Netflix Eureka libraries
– Holds registrations, shares information on other registrants
– Synchronizes itself with other servers
● Spring Cloud Eureka Client
– Connects to server to register, and obtain information on
other clients.
Copyright Ken Krueger 2015
Exercise
Create and Run Several Applications coordinated by Spring
Cloud Eureka
Instructions: Student Files, Lab 4
Copyright Ken Krueger 2015