Spring Cloud: Building Eureka Service Registry

Keywords: Programming Spring xml Java snapshot

I: Introduction to Eureka

Eureka, like Constul and Zookeeper, is a component of user service discovery and service registration, initially mainly used for Amazon's cloud computing service platform AWS. Eureka is divided into Eureka Server (Service Registry) and Eureka Client (Client).

Eureka advantages

  • 1: Completely open source, and after three years of iteration, performance and functionality are very stable.
  • 2: It is the registration center recommended by Spring Cloud. It can connect with other Spring Cloud components perfectly.
  • 3: Eureka can cooperate with Ribbon, Hystrix, Zuul and other components to quickly complete the service registry, and these components are called Netfilx OOS components, which are integrated by Spring Cloud. They are the core service components and basic components of Spring Cloud.

Second: Setting up the First Service Registry

Talking is boolshit, just look at the code!

  • Parent project pom.xml

<groupId>com.calvin.cloud</groupId>
<artifactId>cloud_day01</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>eureka-server</module>
    <module>eureka-client</module>
</modules>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Use SpringInitializr to create two submodules, Eureka server and Eureka client.

  • eureka-server/pom.xml
<parent>
    <artifactId>cloud_day01</artifactId>
    <groupId>com.calvin.cloud</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>eureka-server</artifactId>

<dependencies>
    <!-- springcloud If there is a problem with the version, the introduction of the package will be problematic. -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

  • eureka-server/EurekaServerApplication.java
/**
 * EurekaServer Startup class
 * @author Calvin
 * @date 2019/07/29
 */

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


  • eureka-server/application.yml
server:
  port: 8080
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone:
        http://${eureka.instance.hostname}:${server.port}/eureka

Start Eureka ServerApplication. main (), browser access http://localhost:8080/eureka At this point, it shows that there is no registered service available.
No instances available

Write another EurekaClient

  • eureka-client/pom.xml
<parent>
        <artifactId>cloud_day01</artifactId>
        <groupId>com.calvin.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

    <dependencies>
        <!-- springcloud If there is a problem with the version, the introduction of the package will be problematic. -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  • eureka-client/application.yml
server:
  port: 8081
spring:
  application:
    name: my_eureka_client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
  • eureka-client/ClientApplication.java
/**
 * Client Test startup class
 * @author Calvin
 * @date 2019/09/29
 */
@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class);
    }

}

Refresh http://localhost:8080/eureka Discover that the service has been registered successfully

Third: Eureka Core Exploration

Concepts in Microservices

  • 1:Register - Service Registration Service registration means that EurekaClient Xiang EurekaServer submits its its own service information, including IP address, port, service ID and so on. If there is no ServiceId in the EurekaClient, the default service name is #{spring.application.name} in the configuration file.

  • 2: Renew - Service renewal
    By default, EurekaClient sends a heartbeat every 30 seconds for service renewal. Inform Eureka Server through service renewal that the Eureka Client is still available and there is no failure. If Eureka Server does not receive a heartbeat within 90 seconds, Eureka Server will assume that the service has been deactivated, and the Eureka Client strength will be removed from the registration list.

  • 3: FetchRegistries -- Get information about service registration lists
    EurekaClient retrieves service registry information from Eureka Server and caches it locally. EurekaClient makes remote calls by using information in the service registration list to find information about other services.
    The registration list information is updated from the server in 30 seconds. Each time the returned registration list information may not match the information cached by EurekaClient. EurekaClient will process the information itself. If for some reason the registration list information cannot match in time, the EurekaClient will update the information.
    Eureka Server caches all the service registration list information and compresses the entire service list and every other application. The compressed content is exactly the same as the uncompressed content. EurekaClient and EurekaServer can communicate with each other in JSON and XML data formats. By default, EurekaClient uses JSON format to obtain service registration list information.

  • 4: Cancel - Offline Service
    EurekaClient can send an offline request to EurekaServer when the program closes. After sending the request, the instance information of the client will be deleted from the service information list of EurekaServer. This offline request will not be completed automatically. The following code needs to be called when the program closes DiscoverManager.getInstance().shutdownComponent();

  • 5: Evication - Service Elimination
    By default, when EurekaClient does not send heartbeat like EurekaServer for 90 consecutive seconds, EurekaServer considers the service unavailable and deletes the service instance from the service list information.

High Availability Service Registry


The above architecture describes how Eureka is deployed on Netflix and how Eureka is deployed and run normally. Each region has an Eureka cluster, and each region has at least one Eureka server to handle regional failures.

The service registers with Eureka Server and sends a heartbeat to update its lease every 30 seconds. If the client can't keep the heartbeat, Eureka Server will delete the service instance from the server registration list in 90 seconds. Registration and renewal information will be copied to all Eureka Server nodes in the cluster. EurekaClient from any region can get all the registration list information every 30 seconds and make remote calls. The above information comes from the official website: https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance

Four: summary

  1. A brief introduction to Eureka and its advantages and disadvantages
  2. Write the first Eureka registry
  3. Understanding the concepts in Eureka and the architecture of high-availability registries

V: Summary of problems

  1. The content is relatively simple and there is no in-depth analysis of the concept.
  2. There is no clustering in the code, nor testing service providers and consumers.
  3. Source parsing should be added later

Posted by Agtronic on Tue, 08 Oct 2019 08:18:53 -0700