sequence

This article mainly studies Netflix EurekaHttpClient

EurekaHttpClient

Eureka – the 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();
}
Copy the code

You can see that the client interface defines a number of low level REST apis for Eureka Server, including Register, Cancel, sendHeartBeat, statusUpdate, getApplications, and so on.

EurekaHttpClientDecorator

Eureka – the 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); / /... }Copy the code

EurekaHttpClientDecorator by defining an abstract method execute (RequestExecutor RequestExecutor), EurekaHttpClient to packing

A subclass of EurekaHttpClientDecorator

Inherited from EurekaHttpClientDecorator class has the following a few, are on the com.net flix. Discovery. Shared. Transport. Decorators package

  • MetricsCollectingEurekaHttpClient
  • RedirectingEurekaHttpClient
  • RetryableEurekaHttpClient
  • SessionedEurekaHttpClient

EurekaHttpClientFactory(top level)

Eureka – the 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();

}
Copy the code

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

EurekaHttpClientFactory implementation class

  • RetryableEurekaHttpClient eureka – the client – 1.8.8 – sources. The 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(); }}; }Copy the code

The client is created through RetryableEurekaHttpClient packaging

  • MetricsCollectingEurekaHttpClient eureka – the client – 1.8.8 – sources. The 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(); }}; }Copy the code

The client is created through MetricsCollectingEurekaHttpClient packaging

  • EurekaHttpClients eureka – the client – 1.8.8 – sources. The 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(); }}; }Copy the code

Is created through RedirectingEurekaHttpClient, RetryableEurekaHttpClient, SessionedEurekaHttpClient client after packaging The original EurekaHttpClient is created through the TransportClientFactory

TransportClientFactory(low level)

Eureka – the 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();

}
Copy the code

As noted in the comments, it is not recommended for advanced consumers and is a low-level API

TransportClientFactory implementation class

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

The above three are specialized class implementations, and the following are anonymous class implementations

  • RedirectingEurekaHttpClient
  • EurekaServerHttpClients
  • Jersey1TransportClientFactories
  • MetricsCollectingEurekaHttpClient

summary

Netflix’s Eureka interface to httpClient is EurekaHttpClient, The factory methods are classified into the top level EurekaHttpClientFactory and the low level TransportClientFactory.

  • At top level, it’s mostly a series of wrappers through the decorator pattern, Like EurekaHttpClients is created through RedirectingEurekaHttpClient, RetryableEurekaHttpClient, SessionedEurekaHttpClient packing client.
  • According to Low Levle, the main implementation is the underlying HTTP remote call, which Netflix offers in a Jersey-based version, while Spring Cloud offers in a RestTemplate based version.

doc

  • EurekaHttpClient