四:帶你實現SpringBoot+SpringCloud Eureka註冊中心的機制與配置

點擊上方 "程序員小樂"關注, 星標或置頂一起成長

每天凌晨00點00分, 第一時間與你相約


每日英文

The fact is that the world is out of everyone's expectation. But some learn to forget, but others insist.

事實上,這個世界不會符合所有人的夢想。只是有人學會遺忘,有人卻堅持。


每日掏心話

每一發奮努力的背後,必是有加倍的賞賜。莫要去找藉口失敗,只找理由成功。也許現在你正面臨一些困難,但是你別忘了,生活總是在向前,明天總是會更好。

來自:XuePeng77 | 責編:樂樂

鏈接:my.oschina.net/u/2450666/blog/1457061

四:帶你實現SpringBoot+SpringCloud Eureka註冊中心的機制與配置

程序員小樂(ID:study_tech)第 832 次推文 圖片來自百度


往日回顧:前後端分離模式下的權限設計方案


正文


手把手教你SpringBoot+SpringCloud系列共8篇:
1、(一)手把手教你 SpringBoot + SpringCloud 開發環境搭建2、(二)手把手教你 SpringBoot + SpringCloud —— 使用Eureka實現服務註冊與發現!3、(三)手把手教你 SpringBoot + SpringCloud——高可用的Eureka註冊中心

Eureka機制與配置

前三節中,使用一個簡單的服務註冊發現構建了Eureka服務治理體系中的核心:


  • 服務註冊中心:提供服務註冊與發現的功能;

  • 服務提供者:提供服務應用,將自己提供的服務註冊到Eureka,以供其他應用發現並使用;

  • 服務消費者:從服務註冊中心獲取服務列表,通過列表去所需處調用具體服務;


服務治理機制

服務機制機制有以下重要元素:


  • 註冊中心互相註冊組成了高可用集群;

  • 分佈式服務分別註冊到註冊中心集群;

  • 消費者也分別指向註冊中心集群;


服務提供者

服務註冊:

服務提供者在啟動的時候會通過REST請求的方式將自己註冊到EurekaServer上,同時帶有自身的元數據信息,EurekaServer接收到這個REST請求後,將元數據存儲。

服務同步:

分佈式服務分別註冊到不同的註冊中心,由於高可用的註冊中心相互註冊,它會將服務發送的註冊請求轉發給集群中相連的其他註冊中心,從而實現註冊中心之間的服務同步。

服務續約:

在服務順利註冊完成後,服務提供者會維護一個心跳用來通知註冊中心,服務還可用,根據以下兩個屬性可以定義具體的心跳通知細節:

#定義服務續約任務的調用間隔時間
eureka.instance.lease-renewal-interval-in-seconds=30
#定義服務失效的時間
eureka.instance.lease-expiration-duration-in-seconds=90


服務消費者

獲取服務:

當啟動一個服務消費者時,它會向註冊中心發起一個REST請求獲取服務清單。為了性能考慮,註冊中心會維護一份只讀的清單來返回給消費者,清單每30秒更新一次。

服務調用:

消費者在獲取到清單後,通過服務名可以獲得具體服務信息,並根據自己的需求決定調用哪個服務,在Ribbon中會默認採用輪訓的方式進行調用,從而實現負載均衡。

服務下線:

在系統運行的過程中,會面臨關閉或者重啟服務,當進行正常關閉操作時,它會觸發一個服務下線的REST請求給註冊中心,註冊中心接到請求後,將該服務的狀態設置為下線(DOWN),並將該事件傳播出去。


註冊中心

失效剔除:

當某些服務出現異常,不能發送服務下線的請求到註冊中心。對於這種情況,註冊中心會每隔一段時間(60S)將當前清單中超時(90S)沒有續約的服務剔除出去。

自我保護:

當Eureka Server節點在短時間內丟失過多客戶端時(可能發生了網絡分區故障),那麼這個節點就會進入自我保護模式。一旦進入該模式,Eureka Server就會保護服務註冊表中的信息,不再刪除服務註冊表中的數據(也就是不會註銷任何微服務)。當網絡故障恢復後,該Eureka Server節點會自動退出自我保護模式。

自我保護模式是一種應對網絡異常的安全保護措施。它的架構哲學是寧可同時保留所有微服務(健康的微服務和不健康的微服務都會保留),也不盲目註銷任何健康的微服務。使用自我保護模式,可以讓Eureka集群更加的健壯、穩定。

#關閉自我保護
eureka.server.enable-self-preservation = false


配置簡述


註冊中心

註冊中心在大多數情況下無需去特意配置,相關的配置在org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean下有定義,在配置文件中均以eureka.server作為前綴,類的具體類容如下:

package org.springframework.cloud.netflix.eureka.server;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.netflix.eureka.EurekaConstants;
import org.springframework.core.env.PropertyResolver;

import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.aws.AwsBindingStrategy;

import lombok.Data;



/**
* @author Dave Syer
*/
@Data
@ConfigurationProperties(EurekaServerConfigBean.PREFIX)
public class EurekaServerConfigBean implements EurekaServerConfig, EurekaConstants {

public static final String PREFIX = "eureka.server";

private static final int MINUTES = 60 * 1000;

@Autowired(required = false)
PropertyResolver propertyResolver;

private String aWSAccessId;

private String aWSSecretKey;

private int eIPBindRebindRetries = 3;

private int eIPBindingRetryIntervalMs = 5 * MINUTES;

private int eIPBindingRetryIntervalMsWhenUnbound = 1 * MINUTES;

private boolean enableSelfPreservation = true;

private double renewalPercentThreshold = 0.85;

private int renewalThresholdUpdateIntervalMs = 15 * MINUTES;

private int peerEurekaNodesUpdateIntervalMs = 10 * MINUTES;

private int numberOfReplicationRetries = 5;

private int peerEurekaStatusRefreshTimeIntervalMs = 30 * 1000;

private int waitTimeInMsWhenSyncEmpty = 5 * MINUTES;

private int peerNodeConnectTimeoutMs = 200;

private int peerNodeReadTimeoutMs = 200;

private int peerNodeTotalConnections = 1000;

private int peerNodeTotalConnectionsPerHost = 500;

private int peerNodeConnectionIdleTimeoutSeconds = 30;

private long retentionTimeInMSInDeltaQueue = 3 * MINUTES;


private long deltaRetentionTimerIntervalInMs = 30 * 1000;

private long evictionIntervalTimerInMs = 60 * 1000;

private int aSGQueryTimeoutMs = 300;

private long aSGUpdateIntervalMs = 5 * MINUTES;

private long aSGCacheExpiryTimeoutMs = 10 * MINUTES; // defaults to longer than the
// asg update interval

private long responseCacheAutoExpirationInSeconds = 180;

private long responseCacheUpdateIntervalMs = 30 * 1000;

private boolean useReadOnlyResponseCache = true;

private boolean disableDelta;

private long maxIdleThreadInMinutesAgeForStatusReplication = 10;

private int minThreadsForStatusReplication = 1;

private int maxThreadsForStatusReplication = 1;

private int maxElementsInStatusReplicationPool = 10000;

private boolean syncWhenTimestampDiffers = true;

private int registrySyncRetries = 0;

private long registrySyncRetryWaitMs = 30 * 1000;

private int maxElementsInPeerReplicationPool = 10000;

private long maxIdleThreadAgeInMinutesForPeerReplication = 15;

private int minThreadsForPeerReplication = 5;

private int maxThreadsForPeerReplication = 20;

private int maxTimeForReplication = 30000;

private boolean primeAwsReplicaConnections = true;

private boolean disableDeltaForRemoteRegions;

private int remoteRegionConnectTimeoutMs = 1000;

private int remoteRegionReadTimeoutMs = 1000;

private int remoteRegionTotalConnections = 1000;

private int remoteRegionTotalConnectionsPerHost = 500;

private int remoteRegionConnectionIdleTimeoutSeconds = 30;

private boolean gZipContentFromRemoteRegion = true;

private Map remoteRegionUrlsWithName = new HashMap<>();

private String[] remoteRegionUrls;

private Map> remoteRegionAppWhitelist;

private int remoteRegionRegistryFetchInterval = 30;

private int remoteRegionFetchThreadPoolSize = 20;

private String remoteRegionTrustStore = "";

private String remoteRegionTrustStorePassword = "changeit";

private boolean disableTransparentFallbackToOtherRegion;

private boolean batchReplication;

private boolean rateLimiterEnabled = false;

private boolean rateLimiterThrottleStandardClients = false;

private Set rateLimiterPrivilegedClients = Collections.emptySet();

private int rateLimiterBurstSize = 10;

private int rateLimiterRegistryFetchAverageRate = 500;

private int rateLimiterFullFetchAverageRate = 100;

private boolean logIdentityHeaders = true;

private String listAutoScalingGroupsRoleName = "ListAutoScalingGroups";

private boolean enableReplicatedRequestCompression = false;

private String jsonCodecName;

private String xmlCodecName;


private int route53BindRebindRetries = 3;

private int route53BindingRetryIntervalMs = 5 * MINUTES;

private long route53DomainTTL = 30;

private AwsBindingStrategy bindingStrategy = AwsBindingStrategy.EIP;

private int minAvailableInstancesForPeerReplication = -1;

@Override
public boolean shouldEnableSelfPreservation() {
return this.enableSelfPreservation;
}

@Override
public boolean shouldDisableDelta() {
return this.disableDelta;
}

@Override
public boolean shouldSyncWhenTimestampDiffers() {
return this.syncWhenTimestampDiffers;
}

@Override
public boolean shouldPrimeAwsReplicaConnections() {
return this.primeAwsReplicaConnections;
}

@Override
public boolean shouldDisableDeltaForRemoteRegions() {
return this.disableDeltaForRemoteRegions;
}

@Override
public boolean shouldGZipContentFromRemoteRegion() {
return this.gZipContentFromRemoteRegion;
}

@Override
public Set getRemoteRegionAppWhitelist(String regionName) {
return this.remoteRegionAppWhitelist
.get(regionName == null ? "global" : regionName.trim().toLowerCase());
}

@Override
public boolean disableTransparentFallbackToOtherRegion() {
return this.disableTransparentFallbackToOtherRegion;

}

@Override
public boolean shouldBatchReplication() {
return this.batchReplication;
}

@Override
public boolean shouldLogIdentityHeaders() {
return this.logIdentityHeaders;
}

@Override
public String getJsonCodecName() {
return this.jsonCodecName;
}

@Override
public String getXmlCodecName() {
return this.xmlCodecName;
}

@Override
public boolean shouldUseReadOnlyResponseCache() {
return this.useReadOnlyResponseCache;
}

@Override
public boolean shouldEnableReplicatedRequestCompression() {
return this.enableReplicatedRequestCompression;
}

@Override
public String getExperimental(String name) {
if (this.propertyResolver != null) {
return this.propertyResolver.getProperty(PREFIX + ".experimental." + name,
String.class, null);
}
return null;
}

@Override
public int getHealthStatusMinNumberOfAvailablePeers() {
return this.minAvailableInstancesForPeerReplication;
}
}


服務提供者

關於服務提供者的配置信息,可以在org.springframework.cloud.netflix.eureka.EurekaClientConfigBean類中看到詳細信息,這些配置都是以eureka.client為前綴:

package org.springframework.cloud.netflix.eureka;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;

import com.netflix.appinfo.EurekaAccept;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.transport.EurekaTransportConfig;

import lombok.Data;

/**
* @author Dave Syer
*/
@Data
@ConfigurationProperties(EurekaClientConfigBean.PREFIX)
public class EurekaClientConfigBean implements EurekaClientConfig, EurekaConstants {

public static final String PREFIX = "eureka.client";

@Autowired(required = false)
PropertyResolver propertyResolver;

public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX
+ "/";

public static final String DEFAULT_ZONE = "defaultZone";

private static final int MINUTES = 60;

/**
* Flag to indicate that the Eureka client is enabled.
*/


private boolean enabled = true;

private EurekaTransportConfig transport = new CloudEurekaTransportConfig();

/**
* Indicates how often(in seconds) to fetch the registry information from the eureka
* server.
*/
private int registryFetchIntervalSeconds = 30;

/**
* Indicates how often(in seconds) to replicate instance changes to be replicated to
* the eureka server.
*/
private int instanceInfoReplicationIntervalSeconds = 30;

/**
* Indicates how long initially (in seconds) to replicate instance info to the eureka
* server
*/
private int initialInstanceInfoReplicationIntervalSeconds = 40;

/**
* Indicates how often(in seconds) to poll for changes to eureka server information.
* Eureka servers could be added or removed and this setting controls how soon the
* eureka clients should know about it.
*/
private int eurekaServiceUrlPollIntervalSeconds = 5 * MINUTES;

/**
* Gets the proxy port to eureka server if any.
*/
private String proxyPort;

/**
* Gets the proxy host to eureka server if any.
*/
private String proxyHost;

/**
* Gets the proxy user name if any.
*/
private String proxyUserName;

/**
* Gets the proxy password if any.
*/
private String proxyPassword;

/**

* Indicates how long to wait (in seconds) before a read from eureka server needs to
* timeout.
*/
private int eurekaServerReadTimeoutSeconds = 8;

/**
* Indicates how long to wait (in seconds) before a connection to eureka server needs
* to timeout. Note that the connections in the client are pooled by
* org.apache.http.client.HttpClient and this setting affects the actual connection
* creation and also the wait time to get the connection from the pool.
*/
private int eurekaServerConnectTimeoutSeconds = 5;

/**
* Gets the name of the implementation which implements BackupRegistry to fetch the
* registry information as a fall back option for only the first time when the eureka
* client starts.
*
* This may be needed for applications which needs additional resiliency for registry
* information without which it cannot operate.
*/
private String backupRegistryImpl;

/**
* Gets the total number of connections that is allowed from eureka client to all
* eureka servers.
*/
private int eurekaServerTotalConnections = 200;

/**
* Gets the total number of connections that is allowed from eureka client to a eureka
* server host.
*/
private int eurekaServerTotalConnectionsPerHost = 50;

/**
* Gets the URL context to be used to construct the service url to contact eureka
* server when the list of eureka servers come from the DNS. This information is not
* required if the contract returns the service urls from eurekaServerServiceUrls.
*
* The DNS mechanism is used when useDnsForFetchingServiceUrls is set to true and the
* eureka client expects the DNS to configured a certain way so that it can fetch
* changing eureka servers dynamically. The changes are effective at runtime.
*/
private String eurekaServerURLContext;

/**
* Gets the port to be used to construct the service url to contact eureka server when
* the list of eureka servers come from the DNS.This information is not required if
* the contract returns the service urls eurekaServerServiceUrls(String).

*
* The DNS mechanism is used when useDnsForFetchingServiceUrls is set to true and the
* eureka client expects the DNS to configured a certain way so that it can fetch
* changing eureka servers dynamically.
*
* The changes are effective at runtime.
*/
private String eurekaServerPort;

/**
* Gets the DNS name to be queried to get the list of eureka servers.This information
* is not required if the contract returns the service urls by implementing
* serviceUrls.
*
* The DNS mechanism is used when useDnsForFetchingServiceUrls is set to true and the
* eureka client expects the DNS to configured a certain way so that it can fetch
* changing eureka servers dynamically.
*
* The changes are effective at runtime.
*/
private String eurekaServerDNSName;

/**
* Gets the region (used in AWS datacenters) where this instance resides.
*/
private String region = "us-east-1";

/**
* Indicates how much time (in seconds) that the HTTP connections to eureka server can
* stay idle before it can be closed.
*
* In the AWS environment, it is recommended that the values is 30 seconds or less,
* since the firewall cleans up the connection information after a few mins leaving
* the connection hanging in limbo
*/
private int eurekaConnectionIdleTimeoutSeconds = 30;

/**
* Indicates whether the client is only interested in the registry information for a
* single VIP.
*/
private String registryRefreshSingleVipAddress;

/**
* The thread pool size for the heartbeatExecutor to initialise with
*/
private int heartbeatExecutorThreadPoolSize = 2;

/**
* Heartbeat executor exponential back off related property. It is a maximum

* multiplier value for retry delay, in case where a sequence of timeouts occurred.
*/
private int heartbeatExecutorExponentialBackOffBound = 10;

/**
* The thread pool size for the cacheRefreshExecutor to initialise with
*/
private int cacheRefreshExecutorThreadPoolSize = 2;

/**
* Cache refresh executor exponential back off related property. It is a maximum
* multiplier value for retry delay, in case where a sequence of timeouts occurred.
*/
private int cacheRefreshExecutorExponentialBackOffBound = 10;

/**
* Map of availability zone to list of fully qualified URLs to communicate with eureka
* server. Each value can be a single URL or a comma separated list of alternative
* locations.
*
* Typically the eureka server URLs carry protocol,host,port,context and version
* information if any. Example:
* http://ec2-256-156-243-129.compute-1.amazonaws.com:7001/eureka/
*
* The changes are effective at runtime at the next service url refresh cycle as
* specified by eurekaServiceUrlPollIntervalSeconds.
*/
private Map serviceUrl = new HashMap<>();

{
this.serviceUrl.put(DEFAULT_ZONE, DEFAULT_URL);
}

/**
* Indicates whether the content fetched from eureka server has to be compressed
* whenever it is supported by the server. The registry information from the eureka
* server is compressed for optimum network traffic.
*/
private boolean gZipContent = true;

/**
* Indicates whether the eureka client should use the DNS mechanism to fetch a list of
* eureka servers to talk to. When the DNS name is updated to have additional servers,
* that information is used immediately after the eureka client polls for that
* information as specified in eurekaServiceUrlPollIntervalSeconds.
*
* Alternatively, the service urls can be returned serviceUrls, but the users should
* implement their own mechanism to return the updated list in case of changes.
*

* The changes are effective at runtime.
*/
private boolean useDnsForFetchingServiceUrls = false;

/**
* 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.
*/
private boolean registerWithEureka = true;

/**
* Indicates whether or not this instance should try to use the eureka server in the
* same zone for latency and/or other reason.
*
* Ideally eureka clients are configured to talk to servers in the same zone
*
* The changes are effective at runtime at the next registry fetch cycle as specified
* by registryFetchIntervalSeconds
*/
private boolean preferSameZoneEureka = true;

/**
* Indicates whether to log differences between the eureka server and the eureka
* client in terms of registry information.
*
* Eureka client tries to retrieve only delta changes from eureka server to minimize
* network traffic. After receiving the deltas, eureka client reconciles the
* information from the server to verify it has not missed out some information.
* Reconciliation failures could happen when the client has had network issues
* communicating to server.If the reconciliation fails, eureka client gets the full
* registry information.
*
* While getting the full registry information, the eureka client can log the
* differences between the client and the server and this setting controls that.
*
* The changes are effective at runtime at the next registry fetch cycle as specified
* by registryFetchIntervalSecondsr
*/
private boolean logDeltaDiff;

/**
* Indicates whether the eureka client should disable fetching of delta and should
* rather resort to getting the full registry information.
*
* Note that the delta fetches can reduce the traffic tremendously, because the rate
* of change with the eureka server is normally much lower than the rate of fetches.
*

* The changes are effective at runtime at the next registry fetch cycle as specified
* by registryFetchIntervalSeconds
*/
private boolean disableDelta;

/**
* Comma separated list of regions for which the eureka registry information will be
* fetched. It is mandatory to define the availability zones for each of these regions
* as returned by availabilityZones. Failing to do so, will result in failure of
* discovery client startup.
*
*/
private String fetchRemoteRegionsRegistry;

/**
* Gets the list of availability zones (used in AWS data centers) for the region in
* which this instance resides.
*
* The changes are effective at runtime at the next registry fetch cycle as specified
* by registryFetchIntervalSeconds.
*/
private Map availabilityZones = new HashMap<>();

/**
* Indicates whether to get the applications after filtering the applications for
* instances with only InstanceStatus UP states.
*/
private boolean filterOnlyUpInstances = true;

/**
* Indicates whether this client should fetch eureka registry information from eureka
* server.
*/
private boolean fetchRegistry = true;

/**
* Get a replacement string for Dollar sign <code>${{}}$lt;/code> during
* serializing/deserializing information in eureka server.
*/
private String dollarReplacement = "_-";

/**
* Get a replacement string for underscore sign <code>_/<code> during
* serializing/deserializing information in eureka server.
*/
private String escapeCharReplacement = "__";

/**
* Indicates whether server can redirect a client request to a backup server/cluster.

* If set to false, the server will handle the request directly, If set to true, it
* may send HTTP redirect to the client, with a new server location.
*/
private boolean allowRedirects = false;

/**
* If set to true, local status updates via ApplicationInfoManager will trigger
* on-demand (but rate limited) register/updates to remote eureka servers
*/
private boolean onDemandUpdateStatusChange = true;

/**
* This is a transient config and once the latest codecs are stable, can be removed
* (as there will only be one)
*/
private String encoderName;

/**
* This is a transient config and once the latest codecs are stable, can be removed
* (as there will only be one)
*/
private String decoderName;

/**
* EurekaAccept name for client data accept
*/
private String clientDataAccept = EurekaAccept.full.name();

@Override
public boolean shouldGZipContent() {
return this.gZipContent;
}

@Override
public boolean shouldUseDnsForFetchingServiceUrls() {
return this.useDnsForFetchingServiceUrls;
}

@Override
public boolean shouldRegisterWithEureka() {
return this.registerWithEureka;
}

@Override
public boolean shouldPreferSameZoneEureka() {
return this.preferSameZoneEureka;
}

@Override
public boolean shouldLogDeltaDiff() {

return this.logDeltaDiff;
}

@Override
public boolean shouldDisableDelta() {
return this.disableDelta;
}

@Override
public String fetchRegistryForRemoteRegions() {
return this.fetchRemoteRegionsRegistry;
}

@Override
public String[] getAvailabilityZones(String region) {
String value = this.availabilityZones.get(region);
if (value == null) {
value = DEFAULT_ZONE;
}
return value.split(",");
}

@Override
public List getEurekaServerServiceUrls(String myZone) {
String serviceUrls = this.serviceUrl.get(myZone);
if (serviceUrls == null || serviceUrls.isEmpty()) {
serviceUrls = this.serviceUrl.get(DEFAULT_ZONE);
}
if (!StringUtils.isEmpty(serviceUrls)) {
final String[] serviceUrlsSplit = StringUtils.commaDelimitedListToStringArray(serviceUrls);
List eurekaServiceUrls = new ArrayList<>(serviceUrlsSplit.length);
for (String eurekaServiceUrl : serviceUrlsSplit) {
if (!endsWithSlash(eurekaServiceUrl)) {
eurekaServiceUrl += "/";
}
eurekaServiceUrls.add(eurekaServiceUrl);
}
return eurekaServiceUrls;
}

return new ArrayList<>();
}

private boolean endsWithSlash(String url) {
return url.endsWith("/");
}

@Override

public boolean shouldFilterOnlyUpInstances() {
return this.filterOnlyUpInstances;
}

@Override
public boolean shouldFetchRegistry() {
return this.fetchRegistry;
}

@Override
public boolean allowRedirects() {
return this.allowRedirects;
}

@Override
public boolean shouldOnDemandUpdateStatusChange() {
return this.onDemandUpdateStatusChange;
}

@Override
public String getExperimental(String name) {
if (this.propertyResolver != null) {
return this.propertyResolver.getProperty(PREFIX + ".experimental." + name,
String.class, null);
}
return null;
}

@Override
public EurekaTransportConfig getTransportConfig() {
return getTransport();
}
}/<code>


服務消費者

關於服務消費者的配置信息,可以在org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean中看到,這些配置都以eureka.instance為前綴:

package org.springframework.cloud.netflix.eureka;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.bind.RelaxedPropertyResolver;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtils.HostInfo;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;
import com.netflix.appinfo.MyDataCenterInfo;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

/**
* @author Dave Syer
* @author Spencer Gibb
* @author Ryan Baxter
*/
@Data
@ConfigurationProperties("eureka.instance")
public class EurekaInstanceConfigBean implements CloudEurekaInstanceConfig, EnvironmentAware {

private static final String UNKNOWN = "unknown";

@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private HostInfo hostInfo;

@Getter(AccessLevel.PRIVATE)
@Setter(AccessLevel.PRIVATE)
private InetUtils inetUtils;

/**
* Get the name of the application to be registered with eureka.
*/
private String appname = UNKNOWN;

/**
* Get the name of the application group to be registered with eureka.
*/
private String appGroupName;

/**
* Indicates whether the instance should be enabled for taking traffic as soon as it
* is registered with eureka. Sometimes the application might need to do some
* pre-processing before it is ready to take traffic.
*/

private boolean instanceEnabledOnit;

/**
* Get the non-secure port on which the instance should receive traffic.
*/
private int nonSecurePort = 80;

/**
* Get the Secure port on which the instance should receive traffic.
*/
private int securePort = 443;

/**
* Indicates whether the non-secure port should be enabled for traffic or not.
*/
private boolean nonSecurePortEnabled = true;

/**
* Indicates whether the secure port should be enabled for traffic or not.
*/
private boolean securePortEnabled;

/**
* 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.
*/
private int leaseRenewalIntervalInSeconds = 30;

/**
* 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.
*/
private int leaseExpirationDurationInSeconds = 90;

/**
* Gets the virtual host name defined for this instance.
*

* This is typically the way other instance would find this instance by using the
* virtual host name.Think of this as similar to the fully qualified domain name, that
* the users of your services will need to find this instance.
*/
private String virtualHostName = UNKNOWN;

/**
* Get the unique Id (within the scope of the appName) of this instance to be
* registered with eureka.
*/
private String instanceId;

/**
* Gets the secure virtual host name defined for this instance.
*
* This is typically the way other instance would find this instance by using the
* secure virtual host name.Think of this as similar to the fully qualified domain
* name, that the users of your services will need to find this instance.
*/
private String secureVirtualHostName = UNKNOWN;

/**
* Gets the AWS autoscaling group name associated with this instance. This information
* is specifically used in an AWS environment to automatically put an instance out of
* service after the instance is launched and it has been disabled for traffic..
*/
private String aSGName;

/**
* Gets the metadata name/value pairs associated with this instance. This information
* is sent to eureka server and can be used by other instances.
*/
private Map metadataMap = new HashMap<>();

/**
* Returns the data center this instance is deployed. This information is used to get
* some AWS specific instance information if the instance is deployed in AWS.
*/
private DataCenterInfo dataCenterInfo = new MyDataCenterInfo(
DataCenterInfo.Name.MyOwn);

/**
* Get the IPAdress of the instance. This information is for academic purposes only as
* the communication from other instances primarily happen using the information
* supplied in {@link #getHostName(boolean)}.
*/
private String ipAddress;

/**

* Gets the relative status page URL path for this instance. The status page URL is
* then constructed out of the hostName and the type of communication - secure or
* unsecure as specified in securePort and nonSecurePort.
*
* It is normally used for informational purposes for other services to find about the
* status of this instance. Users can provide a simple HTML indicating what is the
* current status of the instance.
*/
private String statusPageUrlPath = "/info";

/**
* Gets the absolute status page URL path for this instance. The users can provide the
* statusPageUrlPath if the status page resides in the same instance talking to
* eureka, else in the cases where the instance is a proxy for some other server,
* users can provide the full URL. If the full URL is provided it takes precedence.
*
* It is normally used for informational purposes for other services to find about the
* status of this instance. Users can provide a simple HTML indicating what is the
* current status of the instance.
*/
private String statusPageUrl;

/**
* Gets the relative home page URL Path for this instance. The home page URL is then
* constructed out of the hostName and the type of communication - secure or unsecure.
*
* It is normally used for informational purposes for other services to use it as a
* landing page.
*/
private String homePageUrlPath = "/";

/**
* Gets the absolute home page URL for this instance. The users can provide the
* homePageUrlPath if the home page resides in the same instance talking to eureka,
* else in the cases where the instance is a proxy for some other server, users can
* provide the full URL. If the full URL is provided it takes precedence.
*
* It is normally used for informational purposes for other services to use it as a
* landing page. The full URL should follow the format http://${eureka.hostname}:7001/
* where the value ${eureka.hostname} is replaced at runtime.
*/
private String homePageUrl;

/**
* Gets the relative health check URL path for this instance. The health check page
* URL is then constructed out of the hostname and the type of communication - secure
* or unsecure as specified in securePort and nonSecurePort.
*
* It is normally used for making educated decisions based on the health of the
* instance - for example, it can be used to determine whether to proceed deployments

* to an entire farm or stop the deployments without causing further damage.
*/
private String healthCheckUrlPath = "/health";

/**
* Gets the absolute health check page URL for this instance. The users can provide
* the healthCheckUrlPath if the health check page resides in the same instance
* talking to eureka, else in the cases where the instance is a proxy for some other
* server, users can provide the full URL. If the full URL is provided it takes
* precedence.
*
*


* It is normally used for making educated decisions based on the health of the
* instance - for example, it can be used to determine whether to proceed deployments
* to an entire farm or stop the deployments without causing further damage. The full
* URL should follow the format http://${eureka.hostname}:7001/ where the value
* ${eureka.hostname} is replaced at runtime.
*/
private String healthCheckUrl;

/**
* Gets the absolute secure health check page URL for this instance. The users can
* provide the secureHealthCheckUrl if the health check page resides in the same
* instance talking to eureka, else in the cases where the instance is a proxy for
* some other server, users can provide the full URL. If the full URL is provided it
* takes precedence.
*
*


* It is normally used for making educated decisions based on the health of the
* instance - for example, it can be used to determine whether to proceed deployments
* to an entire farm or stop the deployments without causing further damage. The full
* URL should follow the format http://${eureka.hostname}:7001/ where the value
* ${eureka.hostname} is replaced at runtime.
*/
private String secureHealthCheckUrl;

/**
* Get the namespace used to find properties. Ignored in Spring Cloud.
*/
private String namespace = "eureka";

/**
* The hostname if it can be determined at configuration time (otherwise it will be
* guessed from OS primitives).
*/
private String hostname;


/**
* Flag to say that, when guessing a hostname, the IP address of the server should be
* used in prference to the hostname reported by the OS.
*/
private boolean preferIpAddress = false;

/**
* Initial status to register with rmeote Eureka server.
*/
private InstanceStatus initialStatus = InstanceStatus.UP;

private String[] defaultAddressResolutionOrder = new String[0];
private Environment environment;

public String getHostname() {
return getHostName(false);
}

@SuppressWarnings("unused")
private EurekaInstanceConfigBean() {
}

public EurekaInstanceConfigBean(InetUtils inetUtils) {
this.inetUtils = inetUtils;
this.hostInfo = this.inetUtils.findFirstNonLoopbackHostInfo();
this.ipAddress = this.hostInfo.getIpAddress();
this.hostname = this.hostInfo.getHostname();
}

@Override
public String getInstanceId() {
if (this.instanceId == null && this.metadataMap != null) {
return this.metadataMap.get("instanceId");
}
return this.instanceId;
}

@Override
public boolean getSecurePortEnabled() {
return this.securePortEnabled;
}

public void setHostname(String hostname) {
this.hostname = hostname;
this.hostInfo.override = true;
}

public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
this.hostInfo.override = true;

}

@Override
public String getHostName(boolean refresh) {
if (refresh && !this.hostInfo.override) {
this.ipAddress = this.hostInfo.getIpAddress();
this.hostname = this.hostInfo.getHostname();
}
return this.preferIpAddress ? this.ipAddress : this.hostname;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
// set some defaults from the environment, but allow the defaults to use relaxed binding
RelaxedPropertyResolver springPropertyResolver = new RelaxedPropertyResolver(this.environment, "spring.application.");
String springAppName = springPropertyResolver.getProperty("name");
if(StringUtils.hasText(springAppName)) {
setAppname(springAppName);
setVirtualHostName(springAppName);
setSecureVirtualHostName(springAppName);
}
}
}

獲取項目源碼:在公眾號後臺回覆“STS”即可獲得。


歡迎在留言區留下你的觀點,一起討論提高。如果今天的文章讓你有新的啟發,學習能力的提升上有新的認識,歡迎轉發分享給更多人。


猜你還想看


阿里、騰訊、百度、華為、京東最新面試題彙集

手把手教你 Netty 實現自定義協議!

一次項目代碼重構:使用Spring容器幹掉條件判斷

JDK 中定時器是如何實現的

關注訂閱號「程序員小樂」,收看更多精彩內容
嘿,你在看嗎?


分享到:


相關文章: