聊聊netflix的EurekaHttpClient

本文主要研究下netflix的EurekaHttpClient

EurekaHttpClient

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/EurekaHttpClient.java

public interface EurekaHttpClient {

    EurekaHttpResponse<Void> register(InstanceInfo info);

    EurekaHttpResponse<Void> cancel(String appName, String id);

    EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id, InstanceInfo info, InstanceStatus overriddenStatus);

    EurekaHttpResponse<Void> statusUpdate(String appName, String id, InstanceStatus newStatus, InstanceInfo info);

    EurekaHttpResponse<Void> deleteStatusOverride(String appName, String id, InstanceInfo info);

    EurekaHttpResponse<Applications> getApplications(String... regions);

    EurekaHttpResponse<Applications> getDelta(String... regions);

    EurekaHttpResponse<Applications> getVip(String vipAddress, String... regions);

    EurekaHttpResponse<Applications> getSecureVip(String secureVipAddress, String... regions);

    EurekaHttpResponse<Application> getApplication(String appName);

    EurekaHttpResponse<InstanceInfo> getInstance(String appName, String id);

    EurekaHttpResponse<InstanceInfo> getInstance(String id);

    void shutdown();
}

可以看到这个client接口定义了eureka server的一些low level rest api,其中包括register、cancel、sendHeartBeat、statusUpdate、getApplications等。

EurekaHttpClientDecorator

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/decorator/EurekaHttpClientDecorator.java

public abstract class EurekaHttpClientDecorator implements EurekaHttpClient {

    public enum RequestType {
        Register,
        Cancel,
        SendHeartBeat,
        StatusUpdate,
        DeleteStatusOverride,
        GetApplications,
        GetDelta,
        GetVip,
        GetSecureVip,
        GetApplication,
        GetInstance,
        GetApplicationInstance
    }

    public interface RequestExecutor<R> {
        EurekaHttpResponse<R> execute(EurekaHttpClient delegate);

        RequestType getRequestType();
    }

    protected abstract <R> EurekaHttpResponse<R> execute(RequestExecutor<R> requestExecutor);

    //......
}

EurekaHttpClientDecorator通过定义一个抽象方法execute(RequestExecutorrequestExecutor),来包装EurekaHttpClient

EurekaHttpClientDecorator的子类

继承自EurekaHttpClientDecorator的类有如下几个,都在com.netflix.discovery.shared.transport.decorator包里头

  • MetricsCollectingEurekaHttpClient
  • RedirectingEurekaHttpClient
  • RetryableEurekaHttpClient
  • SessionedEurekaHttpClient

EurekaHttpClientFactory(top level)

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/EurekaHttpClientFactory.java

/**
 * A top level factory to create http clients for application/eurekaClient use
 *
 * @author Tomasz Bak
 */
public interface EurekaHttpClientFactory {

    EurekaHttpClient newClient();

    void shutdown();

}

这个是EurekaHttpClient的工厂高级别接口,定义了newClient()以及shutdown()方法

EurekaHttpClientFactory的实现类

  • RetryableEurekaHttpClient eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/decorator/RetryableEurekaHttpClient.java public static EurekaHttpClientFactory createFactory(final String name, final EurekaTransportConfig transportConfig, final ClusterResolver<EurekaEndpoint> clusterResolver, final TransportClientFactory delegateFactory, final ServerStatusEvaluator serverStatusEvaluator) { return new EurekaHttpClientFactory() { @Override public EurekaHttpClient newClient() { return new RetryableEurekaHttpClient(name, transportConfig, clusterResolver, delegateFactory, serverStatusEvaluator, DEFAULT_NUMBER_OF_RETRIES); } @Override public void shutdown() { delegateFactory.shutdown(); } }; } 创建的是经过RetryableEurekaHttpClient包装的client
  • MetricsCollectingEurekaHttpClient eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/decorator/MetricsCollectingEurekaHttpClient.java public static EurekaHttpClientFactory createFactory(final EurekaHttpClientFactory delegateFactory) { final Map<RequestType, EurekaHttpClientRequestMetrics> metricsByRequestType = initializeMetrics(); final ExceptionsMetric exceptionMetrics = new ExceptionsMetric(EurekaClientNames.METRIC_TRANSPORT_PREFIX + "exceptions"); return new EurekaHttpClientFactory() { @Override public EurekaHttpClient newClient() { return new MetricsCollectingEurekaHttpClient( delegateFactory.newClient(), metricsByRequestType, exceptionMetrics, false ); } @Override public void shutdown() { shutdownMetrics(metricsByRequestType); exceptionMetrics.shutdown(); } }; } 创建的是经过MetricsCollectingEurekaHttpClient包装的client
  • EurekaHttpClients eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/EurekaHttpClients.java static EurekaHttpClientFactory canonicalClientFactory(final String name, final EurekaTransportConfig transportConfig, final ClusterResolver<EurekaEndpoint> clusterResolver, final TransportClientFactory transportClientFactory) { return new EurekaHttpClientFactory() { @Override public EurekaHttpClient newClient() { return new SessionedEurekaHttpClient( name, RetryableEurekaHttpClient.createFactory( name, transportConfig, clusterResolver, RedirectingEurekaHttpClient.createFactory(transportClientFactory), ServerStatusEvaluators.legacyEvaluator()), transportConfig.getSessionedClientReconnectIntervalSeconds() * 1000 ); } @Override public void shutdown() { wrapClosable(clusterResolver).shutdown(); } }; } 创建的是经过RedirectingEurekaHttpClient、RetryableEurekaHttpClient、SessionedEurekaHttpClient包装之后的client 而原始的EurekaHttpClient则通过TransportClientFactory创建

TransportClientFactory(low level)

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/TransportClientFactory.java

/**
 * A low level client factory interface. Not advised to be used by top level consumers.
 *
 * @author David Liu
 */
public interface TransportClientFactory {

    EurekaHttpClient newClient(EurekaEndpoint serviceUrl);

    void shutdown();

}

如注释上所说,不建议高级别的消费者使用,是一个低级别的api

TransportClientFactory实现类

  • JerseyEurekaHttpClientFactory eureka-client-1.8.8-sources.jar!/com/netflix/discovery/shared/transport/jersey/JerseyEurekaHttpClientFactory.java
  • JerseyRemoteRegionClientFactory eureka-core-1.8.8-sources.jar!/com/netflix/eureka/transport/JerseyRemoteRegionClientFactory.java
  • RestTemplateTransportClientFactory spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/http/RestTemplateTransportClientFactory.java

上面这三个是有专门的类实现,下面这几个是匿名类实现

  • RedirectingEurekaHttpClient
  • EurekaServerHttpClients
  • Jersey1TransportClientFactories
  • MetricsCollectingEurekaHttpClient

小结

netflix的eureka关于httpClient的接口为EurekaHttpClient,其工厂方法主要分类top level的EurekaHttpClientFactory以及low level的TransportClientFactory。

  • top level的话,主要是通过decorator模式进行一系列包装,像EurekaHttpClients创建的则是经过RedirectingEurekaHttpClient、RetryableEurekaHttpClient、SessionedEurekaHttpClient包装的client。
  • low levle的话,主要是底层的http远程调用的实现,netflix提供的是基于Jersey的版本,而spring cloud则提供了基于RestTemplate的版本。

doc

  • EurekaHttpClient

原文发布于微信公众号 - 码匠的流水账(geek_luandun)

原文发表时间:2018-05-03

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

19430
来自专栏编舟记

命令式到函数式编程

应用场景:当我们用到 if-elseif-else 的时候,可以考虑使用 Optional 语义。 举例说明:

7820
来自专栏calmound

UVA 10604 Chemical Reaction(六维dp数组)

题意:有六种不同的试剂,放于试管中,不同的试剂融合其产生的热量不同,且生成的新试剂也不相同,问最后最低温度是多少。 分析:由于只有六种试剂,所以开辟一个六维dp...

37970
来自专栏我是攻城师

Fastjson解析嵌套Map例子

43950
来自专栏SeanCheney的专栏

《Pandas Cookbook》第09章 合并Pandas对象

25710
来自专栏HTML5学堂

斐波那契数列与arguments.callee

HTML5学堂:提到斐波那契数列,很多人还不是太清楚,但是如果提到兔子繁殖这个经典题目,相信学过计算机语言的人们会立刻感觉“亲切”起来,今天我们就来说说斐波那契...

38070
来自专栏Android知识点总结

Java总结IO之总集篇

字符流和字节流向来各行其事,很少有交集。 但Reader和Writer有两个奇子,名叫InputStreamReader(男)和OutputStreamWri...

14650
来自专栏ml

位运算的方法,大结

Title:       位操作基础篇之位操作全面总结 Author:     MoreWindows E-mail:      morewindows@126...

64180
来自专栏HansBug's Lab

3098: Hash Killer II

3098: Hash Killer II Time Limit: 5 Sec  Memory Limit: 128 MBSec  Special Judge S...

30060
来自专栏進无尽的文章

编码篇-开发中关于数字的那些事儿

在日常的开发中我们随时都会跟数字打着交道,对数字的处理也是很平常的事,本文仅对常用的数字操作一个小结,当一个笔记方便日后查看,也希望读者能从中收获些感觉有用的知...

13610

扫码关注云+社区

领取腾讯云代金券