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

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

  hot3.png

本文主要研究下netflix的EurekaHttpClient

EurekaHttpClient

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

public interface EurekaHttpClient {    EurekaHttpResponse
register(InstanceInfo info); EurekaHttpResponse
cancel(String appName, String id); EurekaHttpResponse
sendHeartBeat(String appName, String id, InstanceInfo info, InstanceStatus overriddenStatus); EurekaHttpResponse
statusUpdate(String appName, String id, InstanceStatus newStatus, InstanceInfo info); EurekaHttpResponse
deleteStatusOverride(String appName, String id, InstanceInfo info); EurekaHttpResponse
getApplications(String... regions); EurekaHttpResponse
getDelta(String... regions); EurekaHttpResponse
getVip(String vipAddress, String... regions); EurekaHttpResponse
getSecureVip(String secureVipAddress, String... regions); EurekaHttpResponse
getApplication(String appName); EurekaHttpResponse
getInstance(String appName, String id); EurekaHttpResponse
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
{ EurekaHttpResponse
execute(EurekaHttpClient delegate); RequestType getRequestType(); } protected abstract
EurekaHttpResponse
execute(RequestExecutor
requestExecutor); //......}

EurekaHttpClientDecorator通过定义一个抽象方法execute(RequestExecutor<R> requestExecutor),来包装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
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
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
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

转载于:https://my.oschina.net/go4it/blog/1805950

你可能感兴趣的文章
Linux中select poll和epoll的区别
查看>>
Cocos2d-x之MenuItem
查看>>
远程共享文件夹
查看>>
[转] C/C++中printf和C++中cout的输出格式
查看>>
swift 如何实现点击view后显示灰色背景
查看>>
【Android】3.9 覆盖物功能
查看>>
MySQL也有潜规则 – Select 语句不加 Order By 如何排序?
查看>>
搭建SolrCloud的详细步骤
查看>>
svn的安装与使用
查看>>
基于Linux下Iptables限制BT下载的研究
查看>>
Android对话框-中篇-之建立自己的对话框
查看>>
华为交换机VRP用户界面配置及Telnet登录实验
查看>>
作为一个程序员我最大的遗憾
查看>>
《SolidWorks 2012中文版从入门到精通》一6.5 综合实例——斜齿圆柱齿轮
查看>>
storm集群的监控
查看>>
Vue学习笔记(2)—— Vue的生命周期
查看>>
RHCE 6.0学习笔记-2 RHEL 6 使用光盘配置本地YUM源
查看>>
Mongodb定期备份
查看>>
Confluence 6 数据库设置
查看>>
刨根问底-struts-怎么加载配置的相应的信息
查看>>