Network Configuration
Modify your local hosts file to map custom domain names to the loopback address for simulating a multi-node environment:
127.0.0.1 node1
127.0.0.1 node2
127.0.0.1 node3
Service Registry Setup
Define the Maven dependencies for the Eureka Server. Ensure the spring-cloud-starter-netflix-eureka-server dependency is included.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>registry-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>registry-center</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Registry Bootstrap Class
Annotate the main application class with @EnableEurekaServer to activate the registry functionality.
package com.example.registry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class RegistryCenterApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryCenterApplication.class, args);
}
}
Cluster Configuration
Create a shared base configurasion and specific profiles for each node. The nodes register with each other to form a peer-aware cluster.
src/main/resources/application.properties (Base config):
spring.application.name=registry-center
eureka.client.serviceUrl.defaultZone=http://node1:9001/eureka/,http://node2:9002/eureka/,http://node3:9003/eureka/
Profile: node1
server.port=9001
spring.profiles=node1
eureka.instance.hostname=node1
Profile: node2
server.port=9002
spring.profiles=node2
eureka.instance.hostname=node2
Profile: node3
server.port=9003
spring.profiles=node3
eureka.instance.hostname=node3
Client Application Setup
The client application includes the spring-cloud-starter-netflix-eureka-client dependency to register itself with the cluster.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-consumer</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Client Bootstrap and Controller
The client uses @EnableEurekaClient and exposes a simple REST endpoint.
package com.example.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.Map;
@EnableEurekaClient
@SpringBootApplication
@RestController
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@GetMapping("/health")
public Map<String, Object> checkHealth() {
Map<String, Object> response = new LinkedHashMap<>();
response.put("code", 200);
response.put("service", "active");
response.put("identifier", "order-service");
return response;
}
}
Client Properties
Configure the client to register using an IP address and set specific heartbeat intervals. The client points to all three registry nodes for resilience.
server.port=8080
spring.application.name=service-consumer
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${random.value}
eureka.instance.lease-renewal-interval-in-seconds=5
eureka.instance.lease-expiration-duration-in-seconds=10
eureka.client.registry-fetch-interval-seconds=5
eureka.client.serviceUrl.defaultZone=http://node1:9001/eureka,http://node2:9002/eureka,http://node3:9003/eureka