博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Service Discovery: Eureka
阅读量:6272 次
发布时间:2019-06-22

本文共 4727 字,大约阅读时间需要 15 分钟。

hot3.png

介绍下Eureka的使用以及相关配置参数

高可用

服务调用

1.使用原始的Eureka DiscoveryClient 来找到服务实例的主机并且用Spring的RestTemplate来发起调用。 2.使用Ribbon,一个使用Eureka来找到服务实例的客户端负载均衡解决方案 3. 使用Feigin,它使用了一个说明性的方法来调用服务。它内部使用了Ribbo

源码

服务端

服务端启动

EurekaServerInitializerConfiguration.start()

try {					//TODO: is this class even needed now?					eurekaServerBootstrap.contextInitialized(EurekaServerInitializerConfiguration.this.servletContext);					log.info("Started Eureka Server");					publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig()));					EurekaServerInitializerConfiguration.this.running = true;					publish(new EurekaServerStartedEvent(getEurekaServerConfig()));				}				catch (Exception ex) {					// Help!					log.error("Could not initialize Eureka servlet context", ex);				}

复制 neighboring eureka node

// Copy registry from neighboring eureka node		int registryCount = this.registry.syncUp();

计算Renews threshold (numberOfRenewsPerMinThreshold)

公式: 2 * N * 0.85 ==> 60/心跳时间(总是认为是30s)*instance num *factor Netflix code assumes that heartbeats are always received at intervals of 30 seconds for this calculation.

1.初始化 numberOfRenewsPerMinThreshold PeerAwareInstanceRegistryImpl


@Override      //count 来自 this.registry.syncUp();    public void openForTraffic(ApplicationInfoManager applicationInfoManager, int count) {        // Renewals happen every 30 seconds and for a minute it should be a factor of 2.        this.expectedNumberOfRenewsPerMin = count * 2;       // serverConfig.getRenewalPercentThreshold() 来自配置 默认0.85        this.numberOfRenewsPerMinThreshold =                (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold());        logger.info("Got " + count + " instances from neighboring DS node");        logger.info("Renew threshold is: " + numberOfRenewsPerMinThreshold);        this.startupTime = System.currentTimeMillis();        if (count > 0) {            this.peerInstancesTransferEmptyOnStartup = false;        }        DataCenterInfo.Name selfName = applicationInfoManager.getInfo().getDataCenterInfo().getName();        boolean isAws = Name.Amazon == selfName;        if (isAws && serverConfig.shouldPrimeAwsReplicaConnections()) {            logger.info("Priming AWS connections for all replicas..");            primeAwsReplicas(applicationInfoManager);        }        logger.info("Changing status to UP");        applicationInfoManager.setInstanceStatus(InstanceStatus.UP);        super.postInit();    }

2.客户端注册时候刷新numberOfRenewsPerMinThreshold

AbstractInstanceRegistry  // The lease does not exist and hence it is a new registration                synchronized (lock) {                    if (this.expectedNumberOfRenewsPerMin > 0) {                        // Since the client wants to cancel it, reduce the threshold                        // (1                        // for 30 seconds, 2 for a minute)                        this.expectedNumberOfRenewsPerMin = this.expectedNumberOfRenewsPerMin + 2;                        this.numberOfRenewsPerMinThreshold =                                (int) (this.expectedNumberOfRenewsPerMin * serverConfig.getRenewalPercentThreshold());                    }                }                logger.debug("No previous lease information found; it is new registration");

3.renewal-threshold-update-interval-ms 时间间隔后从新计算阀值.

接收客户端的rest 请求 DiscoveryJerseyProvider

@Override    public Object readFrom(Class serializableClass, Type type,                           Annotation[] annotations, MediaType mediaType,                           MultivaluedMap headers, InputStream inputStream) throws IOException {        DecoderWrapper decoder;        if (MediaType.MEDIA_TYPE_WILDCARD.equals(mediaType.getSubtype())) {            decoder = xmlDecoder;        } else if ("json".equalsIgnoreCase(mediaType.getSubtype())) {            decoder = jsonDecoder;        } else {            decoder = xmlDecoder; // default        }        try {            return decoder.decode(inputStream, serializableClass);        } catch (Throwable e) {            if (e instanceof Error) { // See issue: https://github.com/Netflix/eureka/issues/72 on why we catch Error here.                closeInputOnError(inputStream);                throw new WebApplicationException(createErrorReply(500, e, mediaType));            }            LOGGER.debug("Cannot parse request body", e);            throw new WebApplicationException(createErrorReply(400, "cannot parse request body", mediaType));        }    }

EvictionTask


以eviction-interval-timer-in-ms (eureka.server.evictionIntervalTimerInMs)指定的频率执行 移除过期的instacne.在移除之前检查是否进入自我保护模式.(Renews < Renews threshold && eureka.server.enableSelfPreservation=true)

Renews (last min)


每分钟计算一次.

客户端

转载于:https://my.oschina.net/fuckmylife0/blog/1548668

你可能感兴趣的文章
(转)使用C#开发ActiveX控件
查看>>
spring mvc 基于注解 配置默认 handlermapping
查看>>
半小时学会上传本地项目到github
查看>>
Android学Jni/Ndk 开发记录(一)
查看>>
Linux Tcl和Expect的安装
查看>>
WPF中的依赖项属性(转)
查看>>
linux防火墙相关 iptables
查看>>
最简单的单例模式
查看>>
JPopupMenu的使用以及JPopupMenu中子组件的事件处理
查看>>
从反汇编的角度看引用和指针的区别
查看>>
拓马长枪定乾坤
查看>>
UIProgressView的详细使用
查看>>
Silverlight实用窍门系列:70.Silverlight的视觉状态组VisualStateGroup
查看>>
照片筛选与上传功能
查看>>
Hello ZED
查看>>
常见web攻击方式
查看>>
hdu 4472
查看>>
oracle存储过程中is和as区别
查看>>
windows 2003 群集
查看>>
几个gcc的扩展功能
查看>>