Careeroza
Setting Up Eureka Server
Create the Discovery Server
The registry is just another Spring Boot app. Create discovery-server on Spring Initializr with the dependency Eureka Server:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>package com.shopease.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}Configuration
application.yml
A single Eureka server must not try to register with itself or fetch a registry from a peer:
application.yml
server:
port: 8761
spring:
application:
name: discovery-server
eureka:
client:
register-with-eureka: false # I am the registry, not a client
fetch-registry: false
server:
enable-self-preservation: true # default; see the heartbeat topicExample output
INFO c.n.eureka.DefaultEurekaServerContext : Initializing ... INFO c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP INFO o.s.b.w.embedded.tomcat.TomcatWebServer: Tomcat started on port 8761 (http) INFO .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761 INFO c.s.d.DiscoveryServerApplication : Started DiscoveryServerApplication in 4.21 seconds
Open http://localhost:8761 in a browser. You see the Eureka dashboard with the table "Instances currently registered with Eureka" (empty for now).
High Availability
One registry is a single point of failure. In production run 2–3 Eureka servers that replicate to each other (peer awareness):
# eureka-1
eureka:
client:
service-url:
defaultZone: http://eureka-2:8761/eureka/,http://eureka-3:8761/eureka/Note: Clients cache the registry locally (refreshed every 30 seconds). If all Eureka servers go down, running services can still call each other using the cached list. New instances just cannot register until Eureka is back.