Eureka service governance and configuration details

1, Eureka service governance mechanism

Eureka service governance has three elements: service provider, service consumer and registration center. The relationship between the three elements is as follows:

Service provider
Service registration

When the service provider starts, it will register the service with the registry through the REST request, and attach some metadata information of its own. Eureka Server will store these metadata information in a two-tier map structure (the first tier key is the service name - application.name, and the second tier is the instance name - instance ID)

Renewal of service

The service provider will maintain a heartbeat to tell the registry that the instance is still alive, so that the registry will not reject it

Service Downline

When an instance of a service is shut DOWN or restarted normally, it will tell the registration center the offline status through the REST request. The registration center will remove the instance or change the status to DOWN. If the service exits abnormally, the registration center will wait until the service fails to remove it.

Service consumers
Service acquisition

The service consumer obtains a read-only service list from the registration center through the REST request, and updates the list once in 30 seconds by default

Service invocation

The service consumer obtains the specific instance name and metadata to be called according to the service name in the obtained service list. The metadata contains the calling address. The call of load balancing is realized through the Ribbon.

Registry Center
Failure elimination

If the service exits abnormally, the registry will not be informed of the REST request. When the registry starts, it has created a timer: regularly remove the overtime services from the service list.

Service synchronization

Since the registry is a cluster, once the service is registered in one of the registries, the registry will register the service with other registries in the broadcast mode to achieve service synchronization

Self protection

By default, if the registration center does not receive the renewal heartbeat of the service within a certain period of time, the service will be removed from the service list. However, the service and the registration center may not be able to communicate due to network reasons, and the service is healthy, so this removal operation is very dangerous. Therefore, if the registration center loses too many instances in a short period of time, it will enter self-protection Protect mode. During this period, the service in the service list will not be deleted until the communication is restored (the received heartbeat threshold reaches the set value)

2, Eureka Client building

Project structure

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 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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>serviceclient01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serviceclient01</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.RELEASE</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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <!--<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.33</version>
        </dependency>

        <!--Define common interface-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </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>

application.yml

server:
  servlet:
    context-path: /serviceclient
  port: 9000
spring:
  application:
    name: serviceclient #Instance name
eureka:
  client:
    #Register to the registry, default true
    register-with-eureka: true
    #Do you prefer to use service providers in the same zone
    prefer-same-zone-eureka: true
    #Can be understood as region
    region: guangzhou
    #Available room
    availability-zones:
      guangzhou: zone-1,zone-2
    service-url:
      #defaultZone: http://localhost:8080/eureka01/eureka/,http://localhost:8081/eureka02/eureka/
      zone-1: http://localhost:8080/eureka01/eureka/
      zone-2: http://localhost:8081/eureka02/eureka/
  instance:
    instance-id: serviceclient01 #Hyperlink name under status
    #Display IP
    prefer-ip-address: true
    #Define the call interval of service task renewal
    lease-renewal-interval-in-seconds: 5
    #Define service expiration time
    lease-expiration-duration-in-seconds: 10
    #Metadata of instance information
    metadata-map:
      #Which computer room does the instance belong to
      zone: zone-1

Main startup class: Serviceclient01Application.java

package com.example.serviceclient01;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
//Open fuse
@EnableCircuitBreaker
public class Serviceclient01Application {

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

}

3, Eureka configuration details

Service registration class configuration, EurekaClientConfigBean, prefix: eureka.client
Service URL: specify the registry, map structure, default zone = http: / / localhost: 8761 / Eureka, or configure security verification
enabled: enable client, default true
Registry fetch interval seconds: cache refresh interval for service manifest
instanceInfoReplicationIntervalSeconds: time interval from update instance to registry
initialInstanceInfoReplicationIntervalSeconds: time interval from initializing instance information to the registry
eurekaServiceUrlPollIntervalSeconds: polling the time interval of server address change. When integrating the config configuration center, parameters that need to be paid attention to when dynamically refreshing the eureka registered address
eurekaServerReadTimeoutSeconds: timeout for reading eureka server information
eurekaServerConnectTimeoutSeconds: timeout for connecting to eureka server
Eurekaservertoalconnections: total number of connections from eureka clients to all servers
Eurekaservertoalconnectionsperhost: total number of client connections to each server host
eurekaConnectionIdleTimeoutSeconds: idle shutdown time of server connection
Heartbeat executor thread poolsize: number of initialization threads of heartbeat connection pool
Heartbeat executorexponentialbackoffbound: the maximum multiple value of heartbeat timeout retry delay time
cacheRefreshExecutorThreadPoolSize: number of initialization threads in the cache refresh thread pool
cacheRefreshExecutorExponentialBackOffBound: the maximum multiplier value of cache refresh retry delay time
registerWithEureka: register to server or not, true by default
preferSameZoneEureka: do you prefer to use service providers in the same zone? Only pay attention when using region and zone
filterOnlyUpInstances: whether to filter when getting an instance. Only the instances in up state are retained
fetchRegistry: retrieval service
Service instance class configuration, EurekaInstanceConfigBean, prefix: eureka.instance
Use random.int[start,end] to randomly specify the start port, and use spring.application.name:{spring.application.name}:spring.application.name:{server.port} to specify the instance name
instanceId: the name of an instance, which distinguishes different instances in a unified service
Page of statusPageUrlPath: info
healthCheckUrlPath: health page
preferIpAddress: whether IP is preferred as the display of host name
leaseRenewalIntervalInSeconds: heartbeat interval
leaseExpirationDurationInSeconds: Service rejection time
Non secure port: non secure port, that is, http channel
securePort: secure port, https channel
Non secureportenable: enable or not
securePortEnable: enable or not

Follow-up

I'll give you another article about source code

Published 6 original articles, won praise 1, visited 7231
Private letter follow

Tags: Spring Maven REST Apache

Posted on Tue, 14 Jan 2020 03:00:17 -0500 by Pryach