Spring Cloud微学习(四)Eureka配置——配置详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ErErFei/article/details/79076914

细心的读者会发现application.yml中有关eureka的配置从何而来呢,怎么去查呢,本节将介绍所有关于eureka的配置项,并详细介绍其中几种的含义

eureka配置源码分析

maven仓库中找到spring-cloud-netflix-eureka-clientmaven仓库中找到spring-cloud-netflix-eureka-server下载最新的spring-cloud-netflix-eureka-XXXX-1.4.1.RELEASE.jar。解压缩打开META-INF/spring-configuration-metadata.json文件即可看到所有的可配置内容properties,几乎所有配置中都有(全)(尼玛)(英文)

client

通过groups中的内容可以看到对应的类是什么

"groups": [
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean",
      "name": "eureka.client",
      "type": "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean",
      "name": "eureka.client.transport",
      "sourceMethod": "getTransport()",
      "type": "com.netflix.discovery.shared.transport.EurekaTransportConfig"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean",
      "name": "eureka.instance",
      "type": "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean"
    }
]

通过properties的配置可以看到有哪些配置项可用

"properties": [
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean",
      "defaultValue": 90,
      "name": "eureka.instance.lease-expiration-duration-in-seconds",
      "description": "Indicates the time in seconds that the eureka server waits since it received the\n last heartbeat before it can remove this instance from its view and there by\n disallowing traffic to this instance.\n\n Setting this value too long could mean that the traffic could be routed to the\n instance even though the instance is not alive. Setting this value too small could\n mean, the instance may be taken out of traffic because of temporary network\n glitches.This value to be set to atleast higher than the value specified in\n leaseRenewalIntervalInSeconds.",
      "type": "java.lang.Integer"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean",
      "defaultValue": 30,
      "name": "eureka.instance.lease-renewal-interval-in-seconds",
      "description": "Indicates how often (in seconds) the eureka client needs to send heartbeats to\n eureka server to indicate that it is still alive. If the heartbeats are not\n received for the period specified in leaseExpirationDurationInSeconds, eureka\n server will remove the instance from its view, there by disallowing traffic to this\n instance.\n\n Note that the instance could still not take traffic if it implements\n HealthCheckCallback and then decides to make itself unavailable.",
      "type": "java.lang.Integer"
    },
    ... ...
]

server

通过groups中的内容可以看到对应的类是什么

"groups": [
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.server.EurekaDashboardProperties",
      "name": "eureka.dashboard",
      "type": "org.springframework.cloud.netflix.eureka.server.EurekaDashboardProperties"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.server.InstanceRegistryProperties",
      "name": "eureka.instance.registry",
      "type": "org.springframework.cloud.netflix.eureka.server.InstanceRegistryProperties"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean",
      "name": "eureka.server",
      "type": "org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean"
    }
]

通过properties的配置可以看到有哪些配置项可用

"properties": [
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.server.EurekaDashboardProperties",
      "defaultValue": true,
      "name": "eureka.dashboard.enabled",
      "description": "Flag to enable the Eureka dashboard. Default true.",
      "type": "java.lang.Boolean"
    },
    {
      "sourceType": "org.springframework.cloud.netflix.eureka.server.EurekaDashboardProperties",
      "defaultValue": "\/",
      "name": "eureka.dashboard.path",
      "description": "The path to the Eureka dashboard (relative to the servlet path). Defaults to \"\/\".",
      "type": "java.lang.String"
    },
    ... ...
]

不难看出,eureka.server.*的配置是org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean在处理,eureka.instance.*的配置是org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean在处理,eureka.client.*的配置是org.springframework.cloud.netflix.eureka.EurekaClientConfigBean在处理。可以通过Maven打开对应的类查看具体的对应配置内容


常用配置项

  • eureka.client.registerWithEureka=true/false(默认为true)

设置本实例是否注册到服务注册中心,因为有些时候实例只想获取服务而不想提供服务

/**
* Indicates whether or not this instance should register its information with eureka
* server for discovery by others.
*
* In some cases, you do not want your instances to be discovered whereas you just
* want do discover other instances.
*/
  • eureka.client.fetchRegistry=true/false(默认为true)

设置本客户端是否从服务注册中心获取服务

/**
 * Indicates whether this client should fetch eureka registry information from eurekaserver.
*/
  • eureka.instance.lease-renewal-interval-in-seconds=30(单位是 s)

设置注册的服务多久向服务注册中心发送心跳包

/**
 * Indicates how often (in seconds) the eureka client needs to send heartbeats to
 * eureka server to indicate that it is still alive. If the heartbeats are not
 * received for the period specified in leaseExpirationDurationInSeconds, eureka
 * server will remove the instance from its view, there by disallowing traffic to this
 * instance.
 *
 * Note that the instance could still not take traffic if it implements
 * HealthCheckCallback and then decides to make itself unavailable.
 */
  • eureka.instance.lease-expiration-duration-in-seconds=90(单位是 s)

设置多久没有收到注册服务的心跳包后剔除该服务

/**
 * Indicates the time in seconds that the eureka server waits since it received the
 * last heartbeat before it can remove this instance from its view and there by
 * disallowing traffic to this instance.
 *
 * Setting this value too long could mean that the traffic could be routed to the
 * instance even though the instance is not alive. Setting this value too small could
 * mean, the instance may be taken out of traffic because of temporary network
 * glitches.This value to be set to atleast higher than the value specified in
 * leaseRenewalIntervalInSeconds.
 */
  • eureka.server.enable-self-preservation=ture/false(默认为true)

设置服务是否开启保护机制,即即使eureka.instance.lease-expiration-duration-in-seconds超时也不会剔除该服务,一直等待服务重新开启,设置true时会一直持有该服务不释放

  • eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

设置指定注册服务中心地址,如果查看源码就可以发现,serviceUrl的配置存储在Map类型中,其中key是Zone这里是defaultZone,value为具体的URL地址这里是http://localhost:1111/eureka/,所以也可以配置其它的Zone

  • eureka.client.availability-zones.*

查看源码可以看到availabilityZones是一个HashMap类型,其中key是region,value是用,隔开的zones,所以自定义zones需要

eureka.client.region=love
eureka.client.availabilityZones.love=mlq,roye,fly

EurekaClientConfigBean.java中获取zones的源码

@Override
public String[] getAvailabilityZones(String region) {
    String value = this.availabilityZones.get(region);
    if (value == null) {
        value = DEFAULT_ZONE;
    }
    return value.split(",");
}
  • eureka.instance.preferIpAddress=true/false(默认false)

是否优先使用IP地址作为主机名,如果不想配置主机名IP的映射,可以设置此配置为true,然后eureka.instance.hostname就可以直接配置IP地址了

持续更新中… …

猜你喜欢

转载自blog.csdn.net/ErErFei/article/details/79076914