Eureka 源码解析一-------------------Eureka Client

Eureka简介与使用

声明:由于Eureka的使用很简单,本博客不过多说明,重点放在Eureka的源码解析

Eureka简介:

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务发现功能。Eureka通过心跳检测、健康检查、客户端缓存等机制,确保了系统的高可用性、灵活性和可伸缩性。

Eureka使用:

关于Eureka的使用很简单,我们使用Spring-Cloud构建项目,在充当Eureka的服务启动类上加注解@EnableEurekaClient标明这个服务作为一个Eureka服务。在要被注册的服务启动类上加上@EnableDiscoveryClient,这里的Eureka服务可以配置集群。
这里有一个需要注意的点是,Eureka的server带有一个client,它会把自己注册到自己的server上,通常我们在application.yml文件中增加如下配置:

eureka:
  client:
    register-with-eureka: false #当前服务是否将自己注册到Eureka服务中,如果本身就是Eureka,不注册。

Eureka相关概念:

服务注册:Eureka Client通过发送REST请求向Eureca Server注册自己的服务,提供自身元数据,比如ip地址,服务名称,端口号,运行状况(健康指标)等。Eureka Server接收到注册请求后,会把这些元数据信息存储在一个双层的Map中。
服务续约:服务注册后,Eureka Client会维护一个心跳来持续通知Eureka Server(心跳机制),说明服务一直处于可用状态,防止剔除。默认情况下是30s Eureka Client发送一次心跳来进行服务续约。
服务同步:Eureka Server之间会互相进行注册,构建Eureka Server集群,不同Eureka Server间会进行服务同步,用来保证服务信息的一致性。
获取服务:Eureka Client在启动的时候,会发送一个REST请求给Eureka Server,获取上面注册的服务列表,并且缓存在Eureka Client本地,默认缓存30秒。为了性能考虑,Eureka Server也会维护一份只读的服务清单列表,每隔30秒更新一次。
服务调用:服务消费者在获取到服务列表后,就可以根据列表中的服务信息,查找到其他服务的地址,从而进行远程调用。Eureka有Region和Zone的概念,一个Region可以包含多个Zone,在进行服务调用时,优先访问处于同一个Zone中的服务提供者。
服务下线:当Eureka Client需要关闭或重启时,就不希望在这个时间段内再有请求进来,所以,就需要提前先发送REST请求给Eureka Server,告诉Eureka Server自己要下线了,Eureka Server在收到请求后,就会把该服务状态置为下线(DOWN),并把该下线事件传播出去。
服务剔除:有时候,服务实例可能会因为网络故障等原因导致不能提供服务,而此时该实例也没有发送请求给Eureka Server来进行服务下线,所以,还需要有服务剔除的机制。Eureka Server在启动的时候会创建一个定时任务,每隔一段时间(默认60秒),从当前服务清单中把超时没有续约(默认90秒)的服务剔除。
自我保护:既然Eureka Server会定时剔除超时没有续约的服务,那就有可能出现一种场景,网络一段时间内发生了异常,所有的服务都没能够进行续约,Eureka Server就把所有的服务都剔除了,这样显然不太合理。所以,就有了自我保护机制,当短时间内,统计续约失败的比例,如果达到一定阈值,则会触发自我保护的机制,在该机制下,Eureka Server不会剔除任何的微服务,等到正常后,再退出自我保护机制。

Eureka源码分析

Eureka代码结构

下载eurek-core模块包含了功能的核心实现:

  1. com.netflix.eureka.cluster - 与peer节点复制(replication)相关的功能
  2. com.netflix.eureka.lease - 即”续约”, 用来控制注册信息的生命周期(添加、清除、续约)
  3. com.netflix.eureka.registry - 存储、查询服务注册信息
  4. com.netflix.eureka.resources - RESTful风格中的”R”, 即资源。相当于SpringMVC中的Controller
  5. com.netflix.eureka.transport - 发送HTTP请求的客户端,如发送心跳
  6. com.netflix.eureka.aws - 与amazon AWS服务相关的类

eureka-client模块:
Eureka客户端,微服务通过该客户端与Eureka进行通讯,屏蔽了通讯细节

eureka-server模块:
包含了 servlet 应用的基本配置,如 web.xml。构建成功后在该模块下会生成可部署的war包。

Eureka Client源码

[声明]:我在此使用的是Eureka 2.0.1.RELEASE 其他版本的可能存在些许不同,但大同小异。

服务注册

从服务注册开始分析,Eureka Clien启动的时候就去Eureka Server注册服务,通过在启动类上添加注解@EnableDiscoveryClient来声明这是一个Eureka Client。我们来看下这个注解:

/**
 * Annotation to enable a DiscoveryClient implementation.
 * @author Spencer Gibb
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {

	/**
	 * If true, the ServiceRegistry will automatically register the local server.
	 */
	boolean autoRegister() default true;
}

看上方的注释说这个注解是为了开启一个DiscoveryClient的实例,同时会默认autoRegister为true;
那么我们看一下是如是实现的:

实现DiscoveryClient

来看一下DiscoveryClient这个接口:

/**
 * DiscoveryClient represents read operations commonly available to Discovery service such as
 * Netflix Eureka or consul.io
 * @author Spencer Gibb
 */
public interface DiscoveryClient {

	/**
	 * A human readable description of the implementation, used in HealthIndicator
	 * @return the description
	 */
	String description();//获取备注

	/**
	 * Get all ServiceInstances associated with a particular serviceId
	 * @param serviceId the serviceId to query
	 * @return a List of ServiceInstance
	 */
	List<ServiceInstance> getInstances(String serviceId);//根据服务id获取服务列表

	/**
	 * @return all known service ids
	 */
	List<String> getServices();//获取所有的服务的id

}

注释告诉我们这个接口定义了服务通常的读取操作的抽象方法,分析这个接口下的三个方法,作用分别是:获取备注 ,根据服务id获取服务列表,获取所有的服务的id。
这个而接口貌似到顶了,我们看一下他的实现类,你可以看到一个你很切切的名字EurekaDiscoveryCLient(Eureka的服务客户端)如下:

public class EurekaDiscoveryClient implements DiscoveryClient {

	public static final String DESCRIPTION = "Spring Cloud Eureka Discovery Client";

	private final EurekaInstanceConfig config;

	private final EurekaClient eurekaClient;

	public EurekaDiscoveryClient(EurekaInstanceConfig config, EurekaClient eurekaClient) {
		this.config = config;
		this.eurekaClient = eurekaClient;
	}

	@Override
	public String description() {
		return DESCRIPTION;
	}

	@Override
	public List<ServiceInstance> getInstances(String serviceId) {
		List<InstanceInfo> infos = this.eurekaClient.getInstancesByVipAddress(serviceId,
				false);
		List<ServiceInstance> instances = new ArrayList<>();
		for (InstanceInfo info : infos) {
			instances.add(new EurekaServiceInstance(info));
		}
		return instances;
	}

...省略代码....

	@Override
	public List<String> getServices() {
		Applications applications = this.eurekaClient.getApplications();
		if (applications == null) {
			return Collections.emptyList();
		}
		List<Application> registered = applications.getRegisteredApplications();
		List<String> names = new ArrayList<>();
		for (Application app : registered) {
			if (app.getInstances().isEmpty()) {
				continue;
			}
			names.add(app.getName().toLowerCase());

		}
		return names;
	}

}

依赖了DiscoveryClient,而对于getServices和getIntances方法的实现代码中都是调用了EurekaClient的方法,既是"this.eurekaClient…"

继续跟踪EurekaClient,EurekaClient的源码:

@ImplementedBy(DiscoveryClient.class)
public interface EurekaClient extends LookupService{
...省略代码
}

Eureka实现了LookupService接口,继续跟踪LookupService:

**
 * Lookup service for finding active instances.
 *
 * @author Karthik Ranganathan, Greg Kim.
 * @param <T> for backward compatibility

 */
public interface LookupService<T> {

    /**
     * Returns the corresponding {@link Application} object which is basically a
     * container of all registered <code>appName</code> {@link InstanceInfo}s.
     *
     * @param appName
     * @return a {@link Application} or null if we couldn't locate any app of
     *         the requested appName
     */
    Application getApplication(String appName);

    /**
     * Returns the {@link Applications} object which is basically a container of
     * all currently registered {@link Application}s.
     *
     * @return {@link Applications}
     */
    Applications getApplications();

    /**
     * Returns the {@link List} of {@link InstanceInfo}s matching the the passed
     * in id. A single {@link InstanceInfo} can possibly be registered w/ more
     * than one {@link Application}s
     *
     * @param id
     * @return {@link List} of {@link InstanceInfo}s or
     *         {@link java.util.Collections#emptyList()}
     */
    List<InstanceInfo> getInstancesById(String id);

   ...省略代码...
}

从注释“Lookup service for finding active instances”可以知道这个LookupService的作用是用于查找活动实例的服务,并提供了一些方法。然而在EurekaDiscoveryClient中使用的到底是EurekaClient哪个实现类的实例呢?我们继续跟踪一下EurekaCLient的实例 com.netflix.discovery.DiscoveryClient,从包名可以知道这个类是netflix提供的:

**
 * The class that is instrumental for interactions with <tt>Eureka Server</tt>.
 *
 * <p>
 * <tt>Eureka Client</tt> is responsible for a) <em>Registering</em> the
 * instance with <tt>Eureka Server</tt> b) <em>Renewal</em>of the lease with
 * <tt>Eureka Server</tt> c) <em>Cancellation</em> of the lease from
 * <tt>Eureka Server</tt> during shutdown
 * <p>
 * d) <em>Querying</em> the list of services/instances registered with
 * <tt>Eureka Server</tt>
 * <p>
 *
 * <p>
 * <tt>Eureka Client</tt> needs a configured list of <tt>Eureka Server</tt>
 * {@link java.net.URL}s to talk to.These {@link java.net.URL}s are typically amazon elastic eips
 * which do not change. All of the functions defined above fail-over to other
 * {@link java.net.URL}s specified in the list in the case of failure.
 * </p>
 *
 * @author Karthik Ranganathan, Greg Kim
 * @author Spencer Gibb
 *
 */
@Singleton
public class DiscoveryClient implements EurekaClient {
...省略代码...
}

翻译一下这个类的文档注释,大概的意思就是:
DiscoveryClient和Eureka Server类型进行交互
向Eureka Server注册服务
向Eureka Server服务续约
服务关闭,取消服务续约
获取服务列表
Eureka Client需要配置Eireka Serve的url列表

看到这里我们大概知道其实真正的服务发现的Netflix包中com.netflix.discovery.DiscoveryClient类,我们整理一下这几个类/接口的关系图:
![enter description here][1]

接下来我们详细看一下DiscoveryClient是如何实现服务注册和发现等功能的,找到DiscoveryClient中的代码:

   /**
     * Initializes all scheduled tasks.
     */
    private void initScheduledTasks() {
    ...省略代码...
            instanceInfoReplicator = new InstanceInfoReplicator(
                    this,
                    instanceInfo,
                    clientConfig.getInstanceInfoReplicationIntervalSeconds(),
                    2); // burstSize

     ...省略代码...
          instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
    ...省略代码...
    }

从方法注释上可以知道该方法里面初始化了很多的定时任务。

instanceInfo:是根据服务配置创建出来的服务实例相关信息对象,是在EurekaClientAutoConfiguration类中的 eurekaApplicationInfoManager方法中被创建的


public ApplicationInfoManager eurekaApplicationInfoManager(
                EurekaInstanceConfig config) {
            InstanceInfo instanceInfo = new InstanceInfoFactory().create(config);
            return new ApplicationInfoManager(config, instanceInfo);
}

而 EurekaInstanceConfig 就是application.properties配置的绑定

instanceInfoReplicator 是一个线程对象,代码如下:

/**
 * A task for updating and replicating the local instanceinfo to the remote server. Properties of this task are:
 * - configured with a single update thread to guarantee sequential update to the remote server
 * - update tasks can be scheduled on-demand via onDemandUpdate()
 * - task processing is rate limited by burstSize
 * - a new update task is always scheduled automatically after an earlier update task. However if an on-demand task
 *   is started, the scheduled automatic update task is discarded (and a new one will be scheduled after the new
 *   on-demand update).
 *
 *   @author dliu
 */
class InstanceInfoReplicator implements Runnable {
...省略代码...
}

翻译注释知道 InstanceInfoReplicator的作用是用于更新本地instanceinfo并将其复制到远程服务器的任务 ,其实就是把本地服务实例的相关配置信息(地址,端口,服务名等)发送到注册中心完成注册

我们来跟踪一下他的 run方法 :

 public void run() {
        try {
            discoveryClient.refreshInstanceInfo();

            Long dirtyTimestamp = instanceInfo.isDirtyWithTime();
            if (dirtyTimestamp != null) {
                discoveryClient.register();
                instanceInfo.unsetIsDirty(dirtyTimestamp);
            }
        } catch (Throwable t) {
            logger.warn("There was a problem with the instance info replicator", t);
        } finally {
            Future next = scheduler.schedule(this, replicationIntervalSeconds, TimeUnit.SECONDS);
            scheduledPeriodicRef.set(next);
        }
    }

找到代码 discoveryClient.register();这里就是实现服务注册,继续跟踪进去:

 boolean register() throws Throwable {
        logger.info(PREFIX + "{}: registering service...", appPathIdentifier);
        EurekaHttpResponse<Void> httpResponse;
        try {
            httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
        } catch (Exception e) {
            logger.warn(PREFIX + "{} - registration failed {}", appPathIdentifier, e.getMessage(), e);
            throw e;
        }
        if (logger.isInfoEnabled()) {
            logger.info(PREFIX + "{} - registration status: {}", appPathIdentifier, httpResponse.getStatusCode());
        }
        return httpResponse.getStatusCode() == 204;
    }

这里通过调用:eurekaTransport.registrationClient.register(instanceInfo);实现注册,而instanceInfo其实就是当前服务实例的元数据(配置信息),继续跟踪::

/**
 * Low level Eureka HTTP client API.
 *
 * @author Tomasz Bak
 */
public interface EurekaHttpClient {

    EurekaHttpResponse<Void> register(InstanceInfo info);
...省略代码...
}

翻译 Low level Eureka HTTP client API. 得知这里是一个HTTP客户端的API,那么我们可以大胆猜测,register方法的实现其实就是通过 rest 请求的方式。继续往下追踪该方法(这里需要查看EurekaHttpClient的实现类):

public abstract class EurekaHttpClientDecorator implements EurekaHttpClient {
    @Override
    public EurekaHttpResponse<Void> register(final InstanceInfo info) {
        return execute(new RequestExecutor<Void>() {
            @Override
            public EurekaHttpResponse<Void> execute(EurekaHttpClient delegate) {
                return delegate.register(info);
            }
 
            @Override
            public RequestType getRequestType() {
                return RequestType.Register;
            }
        });

看到这里我们就可以明白了,在register方法中获取到了 serviceUrl 即配置文件中的注册服务地址,,把InstanceInfo作为参数,底层通过EurekaHttpClient(Rest方式)来发请求请求,实现服务注册。

服务获取

继续看com.netflix.discovery.DiscoveryClient的initScheduledTasks()方法,里面还有两个定时任务:

    private void initScheduledTasks() {
        if (clientConfig.shouldFetchRegistry()) {
            // registry cache refresh timer
            int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
            int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
            scheduler.schedule(
                    new TimedSupervisorTask(
                            "cacheRefresh",
                            scheduler,
                            cacheRefreshExecutor,
                            registryFetchIntervalSeconds,
                            TimeUnit.SECONDS,
                            expBackOffBound,
                            new CacheRefreshThread()
                    ),
                    registryFetchIntervalSeconds, TimeUnit.SECONDS);
        }

        if (clientConfig.shouldRegisterWithEureka()) {
            int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
            int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();
            logger.info("Starting heartbeat executor: " + "renew interval is: {}", renewalIntervalInSecs);

            // Heartbeat timer
            scheduler.schedule(
                    new TimedSupervisorTask(
                            "heartbeat",
                            scheduler,
                            heartbeatExecutor,
                            renewalIntervalInSecs,
                            TimeUnit.SECONDS,
                            expBackOffBound,
                            new HeartbeatThread()
                    ),
                    renewalIntervalInSecs, TimeUnit.SECONDS);
...省略代码...
          }

我们先看第一个任务:服务获取 ,
clientConfig.getRegistryFetchIntervalSeconds()是从配置中获取服务清单获取时间间隔,他是执行线程是new HeartbeatThread(),我们跟踪进去:

class CacheRefreshThread implements Runnable {
        public void run() {
            refreshRegistry();
        }
    }
 
    @VisibleForTesting
    void refreshRegistry() {
        try {
    ...省略代码...
 boolean success = fetchRegistry(remoteRegionsModified);
...省略代码...

fetchRegistry:就是获取注册表(注册服务列表)的方法:

private boolean fetchRegistry(boolean forceFullRegistryFetch) {
        Stopwatch tracer = FETCH_REGISTRY_TIMER.start();

        try {
            // If the delta is disabled or if it is the first time, get all
            // applications
            Applications applications = getApplications();

            if (clientConfig.shouldDisableDelta()
                    || (!Strings.isNullOrEmpty(clientConfig.getRegistryRefreshSingleVipAddress()))
                    || forceFullRegistryFetch
                    || (applications == null)
                    || (applications.getRegisteredApplications().size() == 0)
                    || (applications.getVersion() == -1)) //Client application does not have latest library supporting delta
            {
                logger.info("Disable delta property : {}", clientConfig.shouldDisableDelta());
                logger.info("Single vip registry refresh property : {}", clientConfig.getRegistryRefreshSingleVipAddress());
                logger.info("Force full registry fetch : {}", forceFullRegistryFetch);
                logger.info("Application is null : {}", (applications == null));
                logger.info("Registered Applications size is zero : {}",
                        (applications.getRegisteredApplications().size() == 0));
                logger.info("Application version is -1: {}", (applications.getVersion() == -1));
                getAndStoreFullRegistry();
            } else {
                getAndUpdateDelta(applications);
            }
            applications.setAppsHashCode(applications.getReconcileHashCode());
            logTotalInstances();
        } catch (Throwable e) {
            logger.error(PREFIX + "{} - was unable to refresh its cache! status = {}", appPathIdentifier, e.getMessage(), e);
            return false;
        } finally {
            if (tracer != null) {
                tracer.stop();
            }
        }

        // Notify about cache refresh before updating the instance remote status
        onCacheRefreshed();

        // Update remote status based on refreshed data held in the cache
        updateInstanceRemoteStatus();

        // registry was fetched successfully, so return true
        return true;
    }

getAndStoreFullRegistry():获得并存储完整的注册表,跟踪进去:

/**
     * Gets the full registry information from the eureka server and stores it locally.
     * When applying the full registry, the following flow is observed:
     *
     * if (update generation have not advanced (due to another thread))
     *   atomically set the registry to the new registry
     * fi
     *
     * @return the full registry information.
     * @throws Throwable
     *             on error.
     */
    private void getAndStoreFullRegistry() throws Throwable {
        long currentUpdateGeneration = fetchRegistryGeneration.get();

        logger.info("Getting all instance registry info from the eureka server");

        Applications apps = null;
        EurekaHttpResponse<Applications> httpResponse = clientConfig.getRegistryRefreshSingleVipAddress() == null
                ? eurekaTransport.queryClient.getApplications(remoteRegionsRef.get())
                : eurekaTransport.queryClient.getVip(clientConfig.getRegistryRefreshSingleVipAddress(), remoteRegionsRef.get());
        if (httpResponse.getStatusCode() == Status.OK.getStatusCode()) {
            apps = httpResponse.getEntity();
        }
        logger.info("The response status is {}", httpResponse.getStatusCode());

        if (apps == null) {
            logger.error("The application is null for some reason. Not storing this information");
        } else if (fetchRegistryGeneration.compareAndSet(currentUpdateGeneration, currentUpdateGeneration + 1)) {
            localRegionApps.set(this.filterAndShuffle(apps));
            logger.debug("Got full registry with apps hashcode {}", apps.getAppsHashCode());
        } else {
            logger.warn("Not updating applications as another thread is updating it already");
        }
    }

我们可以看到 eurekaTransport.queryClient.这样的代码其实就是通过Rest方式去获取服务清单 最后通过 localRegionApps.set把服务存储到本地区域

服务续约

我们继续看

com.netflix.discovery.DiscoveryClient 中的 initScheduledTasks() 方法中的另一个定时任务:

   scheduler.schedule(
                    new TimedSupervisorTask(
                            "heartbeat",
                            scheduler,
                            heartbeatExecutor,
                            renewalIntervalInSecs,
                            TimeUnit.SECONDS,
                            expBackOffBound,
                            new HeartbeatThread()
                    ),
                    renewalIntervalInSecs, TimeUnit.SECONDS);

heartbeat :使用心跳机制实现服务续约,即每间隔多少秒去请求一下注册中心证明服务还在线,防止被剔除。
renewalIntervalInSecs :就是心跳时间间隔 对应的配置:
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90

我们继续看一下他的执行线程:HeartbeatThread


   private class HeartbeatThread implements Runnable {
 
        public void run() {
            if (renew()) {
                lastSuccessfulHeartbeatTimestamp = System.currentTimeMillis();
            }
        }
    }
    boolean renew() {
        EurekaHttpResponse<InstanceInfo> httpResponse;
        try {
            httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null);
            logger.debug(PREFIX + "{} - Heartbeat status: {}", appPathIdentifier, httpResponse.getStatusCode());
            if (httpResponse.getStatusCode() == 404) {
                REREGISTER_COUNTER.increment();
                logger.info(PREFIX + "{} - Re-registering apps/{}", appPathIdentifier, instanceInfo.getAppName());
                long timestamp = instanceInfo.setIsDirtyWithTime();
                boolean success = register();
                if (success) {
                    instanceInfo.unsetIsDirty(timestamp);
                }
                return success;
            }
            return httpResponse.getStatusCode() == 200;
        } catch (Throwable e) {
            logger.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, e);
            return false;
        }
    }

不难看出他是通过eurekaTransport.registrationClient.sendHeartBeat:去发送心跳 ,依然是Rest方式

【下一篇为大家介绍Eureka,Nacos,zookeeper等几大注册中心的区别,还请进来的小伙伴点个关注吧】

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值