Setting Up Eureka Service Registry
Creating Eureka Server
For multiple Spring Boot projects, use a Maven multi-module structure. Create a main Maven project with Spring Boot and Spring Cloud dependencies in the parent POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microservice</groupId>
<artifactId>service-registry</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Create a module named registry-server using Spring Initializr with Eureka Server dependency:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microservice</groupId>
<artifactId>registry-server</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.microservice</groupId>
<artifactId>service-registry</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
Configure the server in application.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
Add @EnableEurekaServer annotation to the main application class:
@EnableEurekaServer
@SpringBootApplication
public class RegistryServerApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryServerApplication.class, args);
}
}
Start the application and access http://localhost:8761 to view the Eureka dashboard.
Creating Eureka Client
Create a client module named service-client with Eureka Client dependency:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.microservice</groupId>
<artifactId>service-client</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.microservice</groupId>
<artifactId>service-registry</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
Client configuration in application.yml:
server:
port: 8762
spring:
application:
name: service-client
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka/
Spring Cloud Finchley automatically enables service discovery when Eureka dependencies are present:
@SpringBootApplication
public class ServiceClientApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceClientApplication.class, args);
}
}
Start the client application. Registration confirmation appears in logs and the Eureka dashboard shows the registered instance.
Building High Availability Eureka Server Cluster
Production environments require multiple registry servers for high availability. Configure the Eureka server with multiple profiles.
Main configuration file application.yml:
spring:
profiles:
active: default
application:
name: eureka-cluster
Profile-specific configurations:
application-node1.yml:
server:
port: 8761
eureka:
instance:
hostname: node1
client:
service-url:
default-zone: http://node2:8762/eureka/
application-node2.yml:
server:
port: 8762
eureka:
instance:
hostname: node2
client:
service-url:
default-zone: http://node1:8761/eureka/
Both nodes reference eachother in their default-zone configuration for mutual registration and synchronization.
Update local hosts file to resolve node names:
127.0.0.1 node1
127.0.0.1 node2
Package the application and start both cluster nodes:
java -jar registry-server-1.0.0-SNAPSHOT.jar --spring.profiles.active=node1
java -jar registry-server-1.0.0-SNAPSHOT.jar --spring.profiles.active=node2
Access both nodes at http://node1:8761 and http://node2:8762. Each dashboard displays the other node as a replica.
Update client configuration to connect to the cluster:
server:
port: 8763
spring:
application:
name: service-client
eureka:
client:
service-url:
default-zone: http://node1:8761/eureka
Even when configured to register with only one node, services automatically synchronize across all cluster members through Eureka's peer replication mechanism.