Talk about netflix’s EurekaHttpClient

  springcloud

Order

This article mainly studies netflix’s 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();
}

You can see that this client interface defines some low levelrest apis of eureka server, including register, cancel, sendHeartBeat, statusUpdate, getApplications, etc.

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 wraps EurekaHttpClient by defining an abstract method execute (requestexecutor < r > requestexecutor)

Subclass of EurekaHttpClientDecorator

There are several classes inherited from EurekaHttpClientDecorator, all in the com.netflix.discovery.shared.transport.decorator package.

  • 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();

}

This is the factory high-level interface of EurekaHttpClient, which defines newClient () and shutdown () methods.

Implementation Class of 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();
            }
        };
    }

The client was created wrapped by RetryableEurekaHttpClient.

  • 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();
            }
        };
    }

The client was created wrapped by Metropolis CollectingeureKahttpclient

  • 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();
            }
        };
    }

The client is created after being wrapped by RedirectingEurekaHttpClient, RetryableEurekaHttpClient, SessionedEurekaHttpClient.
The original EurekaHttpClient was created through 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();

}

As stated in the note, it is not recommended for high-level consumers. It is a low-level api.

TransportClientFactory Implementation Class

  • 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

These three above have special class implementations, and the following are anonymous class implementations

  • RedirectingEurekaHttpClient
  • EurekaServerHttpClients
  • Jersey1TransportClientFactories
  • MetricsCollectingEurekaHttpClient

Summary

Netflix’s eureka interface on httpClient is EurekaHttpClient, and its factory methods are mainly classified into top level EurekaHttpClientFactory and low level TransportClientFactory.

  • Top level, mainly through the decorator mode for a series of packaging, such as EurekaHttpClients is created by RedirectingEurekaHttpClient, RetryableEurekaHttpClient, SessionedEurekaHttpClient packaged client.
  • Lowlale’s words are mainly the implementation of the underlying http remote call. netflix provides a version based on Jersey, while spring cloud provides a version based on RestTemplate.

doc