Eureka Configuration for springcloud Microservices

hivefans
4 min readAug 1, 2020

Eureka registry/service discovery framework

Eureka is a service discovery framework developed by Netflix, itself a REST-based service that is used to locate middle-tier services running in AWS domains for load balancing and middle-tier service failover purposes. springCloud integrates it into its subproject spring-cloud-netflix to implement the SpringCloud’s Service Discovery feature.

Eureka contains two components: Eureka Server and Eureka Client.

Eureka Server provides service registration service, after the start of each node, it will be registered in Eureka Server, so that the service registry in EurekaServer will store all available service node information, service node information can be seen intuitively in the interface.

The Eureka Client is a java client that is used to simplify interaction with Eureka Server and is a built-in load balancer that uses a round-robin load balancing algorithm.

If Eureka Server does not receive a heartbeat from a node in multiple heartbeat cycles, Eureka Server will remove the node from the service registry (default is 90 seconds).

In summary, Eureka ensures the synchronization of the system through heartbeat checking and client-side caching. High availability, flexibility and scalability.

How to use Build Eureka Server ?

Adding dependencies (Maven is used here as an example)

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>

Creating the Eureka Server Master Runtime Class

package com.aws.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}

The Eureka Server comes with a web home page, accessed by default at http://localhost:8761/.

Eureka services have no background storage, but all service instances in the registry must send heartbeats to keep their registrations up to date (and thus can be done in memory). The client also has a memory cache of Eureka registrations (so they do not have to go into the registry for every request to the service).

By default, each Eureka server also has Eureka clients and requires (at least one) service URL to locate them. If you do not provide the service, the service will keep running, and output error logs that may interfere with you (if you are not on port 8761 and have configured a different serviceUrl it will keep generating such error logs, if configured by default it will only report this error once, and then wait for itself to start up to connect itself successfully).

Standalone configuration

application.yml (individual Eureka service configuration), as follows.

server:
port: 8761 # port
spring:
application:
name: eureka-server # application name
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url: # The address of the EurekaServer is now its own address, and if it is a cluster, you need to add the addresses of the other Servers.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

Note the serviceUrl, which points to the same host as the local instance.

Cluster configuration
application.yml (two Eureka service configurations), as follows

---
server:
port: 6001
spring:
profiles: eureka6001
eureka:
instance:
hostname: eureka6001.com
client:
register-with-eureka: false
fetch-registry: false
service-url: # The address of the EurekaServer is now its own address, and if it is a cluster, you need to add the addresses of the other Servers.
defaultZone: http://eureka6002:6002/eureka


---
server:
port: 6002
spring:
profiles: eureka6002
eureka:
instance:
hostname: eureka6002.com
client:
register-with-eureka: false
fetch-registry: false
service-url: # The address of the EurekaServer is now its own address, and if it is a cluster, you need to add the addresses of the other Servers.
defaultZone: http://eureka6001:6001/eureka

In the previous example, we have a YAML file that allows you to run the same Eureka service on two hosts (eureka6001 and eureka6002) by running the server in a different Spring configuration file. You can use this configuration to test peer-awareness on a single host by operating /etc/hosts to resolve hostnames (there is not much value in doing this in a production environment). In fact, eureka.instance.hostname if you run it on a machine whose hostname you know (by default, the hostname of that machine is used). eureka springcloud application example www.1b23.com

You can add multiple Eureka services to a cluster and they can register synchronously with each other as long as they are all connected to each other. If they are physically separated (within a data center or between multiple data centers), they can register synchronously with each other as long as they are all directly connected to each other.

Eureka Client Connection Eureka Server Cluster Configuration

application.yml (both Eureka service connection addresses need to be added, separated by commas), as follows

eureka:
client:
serviceUrl:
defaultZone: http://eureka6001.com/eureka/,http://eureka6002.com

--

--