Eureka is an open source Service registration and Discovery product from Netflix that provides complete implementations of Service Registry and Service Discovery. It is also one of the most important and core components of the springcloud architecture.

The significance of the registry

The registry

Manage various service functions including service registration, discovery, fusing, load, downgrade, etc., such as dubbo Admin background functions.

With the registry, the call relationship changes, so let’s draw a few sketches to see what happens.

Service A invokes service B

Since various services are registered in the service center, there are many advanced features to do. For example, several services provide the same service to balance the load. Monitor the success rate of server calls to make fuses and remove failure points from the service list; Monitor service invocation times to set different weights for different servers, and so on.

The past and present of Netflix

Before we get to Eureka, let’s talk about Netflix:

The following introduction is from Baidu Baike:

Netflix(Nasdaq NFLX), founded in 1997, is an online video rental provider, mainly providing Netflix with a large number of DVDS and free delivery, headquartered in Los Gatto, California, the United States.

Netflix has topped the customer satisfaction list five times in a row. You can watch movies and TV shows on your PC, TV, iPad, iPhone, and connect your TV to your Wii, Xbox360, PS3 and other devices. Starting in October 2006, Netflix released about 100 million anonymous ratings of 1-5 movies in a data set containing only titles. Rating star and rating date without any text rating content. The competition asked participants to predict which Netflix customers would like, improving their prediction efficiency by more than 10 percent.

All in all, Netflix is one of the largest streaming media companies in the world. The list starts at the beginning of various American TV shows and movies, which include House of Cards, Narcos and Stranger Things. Springcloud’s microservices are based on Netflix’s open-source offerings.

The open source framework component of Netflix has been verified in the large-scale distributed micro-service environment of Netflix for many years, and is gradually accepted by the community as the standard component of the construction of micro-service framework. The Open source product of Spring Cloud is mainly based on the further encapsulation of Netflix open source components to facilitate Spring developers to build microservices infrastructure. For some companies planning to build microservice framework system, it is undoubtedly a shortcut to microservice architecture to make full use of or refer to Netflix’s open source microservice component (or Spring Cloud) and carry out necessary enterprise customization on this basis.

Eureka

According to the official introduction:

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

Eureka is a REST-based service used primarily in the AWS cloud to locate services for load balancing and failover of mid-tier servers.

Spring Cloud encapsulates the Eureka module developed by Netflix for service registration and discovery. Eureka adopts the C-S design architecture. Eureka Server serves as the Server for the service registry function. It is the service registry. Other microservices in the system use Eureka’s clients to connect to Eureka Server and maintain heartbeat connections. In this way, the maintenance personnel of the system can monitor the normal operation of each micro-service in the system through Eureka Server. Some other modules in Spring Cloud, such as Zuul, can use Eureka Server to discover other microservices in the system and perform related logic.

Eureka consists of two components: Eureka server and Eureka client. The Eureka server serves as the service registry server. The Eureka client is a Java client designed to simplify interactions with servers, act as a polling load balancer, and provide failover support for services. Netflix uses a separate client in its production environment that provides weighted load balancing based on traffic, resource utilization, and error status.

Here’s an official picture:

  • item Eureka Server

    Provide service registration and discovery

  • item Service Provider

    Service Provider

    Register your services with Eureka so that service consumers can find them

  • item Service Consumer

    Service consumer

    Get a list of registered services from Eureka to be able to consume services

Case study

Open idea, first file->new-> Project, select Spring Initializr, then you can see the right side let us choose an initial service URL, the default is the above official link. start.spring.io/

Pom file

For the Maven project, let’s start with pom.xml:

<?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.2.6. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.my.eureka</groupId>
    <artifactId>eureka-server-7001</artifactId>
    <version>1.0 the SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>eureka-server</name>
    <description>Eureka Server Indicates the server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
Copy the code
  • Parent: parent dependent project, you can see that the parent of the dependent springBoot is 2.1.6.RELEASE.
  • Properties: The current configuration file has some configurations, you can see the Java version is 1.8, springCloud version is Greenwich.SR1.
  • Eureka test framework dependencies: Eureka
  • DependencyManagement: the parent project does not inherit a dependency if the parent project does not declare a dependency. The dependency is inherited from the parent project only if the dependency is written in the child project and no version is specified, and both version and scope are read from the parent POM. In addition, if the version number is specified in the subproject, the jar version specified in the subproject is used.
  • The spring-boot-Maven-plugin can package a Spring Boot application as an executable JAR or WAR file, and then run the Spring Boot application in the usual way.

configuration

The default configuration file is application.properties under Resource. In SpringBoot projects, two configuration files are currently supported, as well as yamL, and all the configurations I’m using here are yamL.

server:
  port: 7001

spring:
  application:
    name: eureka-server-7001

eureka:
  instance:
    hostname: eureka7001.com          # Eureka server instance host name
  server:
    enable-self-preservation: false   # Disable self-protection in the local debug environment
  client:
    register-with-eureka: false      Declare yourself a server and do not register yourself with the registry
    fetch-registry: false            # This client obtains registration information from the eureka server registry. The default is true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
Copy the code
  • Enable-self-preservation: Prevents clients from being displayed online incorrectly due to the Eureka mechanism. This function is available only in the development environment. This function needs to be cached in the production environment to prevent frequent online and offline services due to network fluctuations.
  • Register-with-eureka: Not like a registry that registers itself
  • Register-with-eureka: indicates the application registration address of the Eureka server

Start the EurekaApplication. Java

@SpringBootApplication
@EnableEurekaServer        // Declare a Eureka server service
public class EurekaServerApplication {

    public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code

Add annotation @enableeurekaserver. In the main function, directly right-click debug to start, as shown in the picture below:

High availability cluster

Double machine deployment

Follow the steps above to create a new Eureka-Server-7002 project. The YML configuration file is basically the same. Change the part of 7001 to 7002

server: port: 7002 #spring spring: application: name: eureka-server-7002 eureka: instance: hostname: Eureka7002.com # Eureka server: enable-self-preservation: false # eureka7002.com # Eureka server: enable-self-preservation: false Register-with-eureka: false # fetch-registry: Default value: true service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7001.com:7002/eureka/Copy the code

Then restart eureka-Server-7001 and Eureka-Server-7002 projects

In the same way as the two-machine configuration scheme, the multi-machine configuration is to add the service addresses of other machines after defaultZone.

Reference: www.geekdigging.com/2019/08/31/…