Eureka Service management and configuration in detail

A, Eureka Service Governance

Eureka Service Governance three elements: service providers, service consumers and registries; their relations as shown below:
Service Management

service providers
Service Registration

REST will pass requests to start the service provider registration service registry, and a number of its own additional metadata information, Eureka Server will metadata information is stored in a map bilayer structure (the first layer is a key service name - application.name, the second layer is the instance name -instance-id)

Service contract

Service providers will maintain a heartbeat to tell registry, examples still alive, so as not to be removed registry

Service offline

Examples of services normally shut down or restart, will be off the assembly line state registry, the registry will eliminate the examples or REST request by the state to DOWN tell, if non-normal exit the service, the registry will wait until the service fails then it removed out.

Consumer Services
Service acquisition

Service consumers through REST request to obtain a read-only list of services to the registry, the default update a list of 30 seconds

Service call

Service consumers get through the list of services acquired in accordance with the service name and metadata specific instance name to be called, metadata contains the address of the call, to achieve load balancing by calling Ribbon.

Registry
Excluding fail

Out of service at non-normal circumstances, no request through REST inform the registry, but the registry at startup has created a timer: timing service at the Super weed out the list of services.

Synchronization Service

Since the registry is a cluster, so the service once registered to a registry of them, then the registry will broadcast mode register for the service to other registration centers, to achieve synchronization service

Self-protection

By default, in a certain period of time the registry has not received a heartbeat renewal services, the service will be removed from the list of services off, but may be due to the reasons which led to the network service center can not be registered and communications, and health service , so this culling operation is very dangerous, so the registry lose too many instances in a short time, then enter the self-protection mode, during which the service will not delete the list of services until communication resumes (received heartbeat threshold reaches the set value)

Two, Eureka Client end structures

Project structure
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>

        <!--定义公共接口-->
        <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 #实例名称
eureka:
  client:
    #注册到注册中心,默认true
    register-with-eureka: true
    #是否偏好使用处于相同zone的服务提供者
    prefer-same-zone-eureka: true
    #可以理解为地区
    region: guangzhou
    #可用机房
    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 #status下面的超链接名称
    #显示IP
    prefer-ip-address: true
    #定义服务任务续约的调用间隔时间
    lease-renewal-interval-in-seconds: 5
    #定义服务失效时间
    lease-expiration-duration-in-seconds: 10
    #实例信息的元数据
    metadata-map:
      #实例属于哪个机房
      zone: zone-1

Master Boot categories: 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
//开启熔断
@EnableCircuitBreaker
public class Serviceclient01Application {

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

}

三、Eureka配置详解

服务注册类配置,EurekaClientConfigBean,前缀:eureka.client
service-url:指定注册中心,map结构,默认defaultZone=http://localhost:8761/eureka,也可以配置安全校验
enabled:启用客户端,默认true
registryFetchIntervalSeconds:服务清单的缓存刷新时间间隔
instanceInfoReplicationIntervalSeconds:更新实例变化到注册中心的时间间隔
initialInstanceInfoReplicationIntervalSeconds:初始化实例信息到注册中心的时间间隔
eurekaServiceUrlPollIntervalSeconds:轮询服务端地址更改的时间间隔,集成config配置中心时,动态刷新eureka注册地址时需要关注的参数
eurekaServerReadTimeoutSeconds:读取eureka server信息的超时时间
eurekaServerConnectTimeoutSeconds:连接eureka server的超时时间
eurekaServerTotalConnections:eureka客户端到所有server的连接总数
eurekaServerTotalConnectionsPerHost:client到每个server主机的连接总数
eurekaConnectionIdleTimeoutSeconds:server连接的空闲关闭时间
heartbeatExecutorThreadPoolSize:心跳连接池的初始化线程数
heartbeatExecutorExponentialBackOffBound:心跳超时重试延迟时间的最大乘数值
cacheRefreshExecutorThreadPoolSize:缓存刷新线程池的初始化线程数
cacheRefreshExecutorExponentialBackOffBound:缓存刷新重试延迟时间的最大乘数值
registerWithEureka:是否注册到server,默认true
preferSameZoneEureka:是否偏好使用处于相同Zone的服务提供者,使用region和zone时才关注
filterOnlyUpInstances:获取实例时是否过滤,仅保留up状态的实例
fetchRegistry:检索服务
服务实例类配置,EurekaInstanceConfigBean,前缀:eureka.instance
使用random.int[start,end]随机指定启动端口,同时使用 s p r i n g . a p p l i c a t i O n . n a m e : {spring.application.name}: {server.port}指定实例名
instanceId:实例名称,区分统一服务中不同实例
statusPageUrlPath:info的页面
healthCheckUrlPath:health页面
preferIpAddress:是否优先使用IP作为主机名的显示
leaseRenewalIntervalInSeconds:心跳间隔时间
leaseExpirationDurationInSeconds:服务剔除时间
nonSecurePort:非安全端口,就是http通道
securePort:安全端口,https通道
nonSecurePortEnable:是否开启
securePortEnable:是否开启

后续

后面会再出一篇讲讲源码

发布了6 篇原创文章 · 获赞 1 · 访问量 7234

Guess you like

Origin blog.csdn.net/qq_34864092/article/details/103970446