8

Eureka源码分析 - DiligentCoder

 1 year ago
source link: https://www.cnblogs.com/LoveShare/p/17090509.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Eureka源码分析 - DiligentCoder - 博客园

微服务注册后,在注册中心的注册表结构是一个map: ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry,假如一个order服务部署了三台机器,那么Map的第一个key为服务名称,第二个map的key是实例编号(instance-id),

InstanceInfo该对象封装了服务的主要信息,例如ip 端口 服务名称 服务的编号等:

1365950-20200331112511036-240487321.png

一、服务的注册

1.客户端源码(DiscoveryClient类里面):

1365950-20200331113434354-1147452496.png

2.服务端的源码(AbstractInstanceRegistry类):

1365950-20200331115524692-226497332.png

1365950-20200331115533406-425050848.png

二、服务的续约(异常下线时,可能无法回调,此时通过续约+剔除机制实现服务剔除)

1.客户端源码(DiscoveryClient类里面):通过发送心跳进行续约,告诉注册中心我还活着

  

1365950-20200331120658814-37487755.png

2.服务端的源码(AbstractInstanceRegistry类):

1365950-20200331121232382-730109674.png

1365950-20200331121309752-910596507.png

三、服务的下线(客户端关闭时,主动发送消息给注册中心,注册中心从注册表中将该服务实例删除)

1.客户端源码(DiscoveryClient类里面):

  

1365950-20200331141306594-721864378.png

  

1365950-20200331141327122-1296629951.png

1365950-20200331141420527-1703961636.png

2.服务端的源码(AbstractInstanceRegistry类):

1365950-20200331141814777-661584094.png

四、服务的剔除:当注册中心服务器一直收不到客户端的心跳续约超过一定时间限制时,注册中心会将该服务从注册表中剔除,该功能只存在注册中心

注册中心源码(AbstractInstanceRegistry类):

1365950-20200331142646851-415608785.png

五、服务的发现:客户端要向注册中心拉取注册列表

1.客户端源码(DiscoveryClient类里面):

1365950-20200331143726408-1519934239.png

1.1全量拉取:

1365950-20200331144824366-228738478.png

1365950-20200331144219440-1622459260.png

1365950-20200331144231305-800520499.png

1.2 增量拉取

1365950-20200331145029553-753978688.png

1365950-20200331144422989-1875965906.png

1365950-20200331144438164-1486841696.png

2.服务端的源码(AbstractInstanceRegistry类):

2.1 注册中心全量拉取:

1365950-20200331150938667-30024328.png

1365950-20200331150959282-1350771420.png

1365950-20200331151106949-1576012526.png

2.2 增量拉取:

1365950-20200331151248775-919379240.png

六 、定时器

1.客户端会定时向注册中心发送心跳进行续约以及定时去注册中心拉取最新的注册列表信息

客户端源码(DiscoveryClient类的构造器里):省略部分无关代代码:

  1. @Inject
  2. DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args,
  3. Provider<BackupRegistry> backupRegistryProvider, EndpointRandomizer endpointRandomizer) {
  4. //此处省略了部分代码
  5. //创建一个线程调度器
  6. scheduler = Executors.newScheduledThreadPool(2,
  7. new ThreadFactoryBuilder()
  8. .setNameFormat("DiscoveryClient-%d")
  9. .setDaemon(true)
  10. .build());
  11. //处理心跳的线程池
  12. heartbeatExecutor = new ThreadPoolExecutor(
  13. 1, clientConfig.getHeartbeatExecutorThreadPoolSize(), 0, TimeUnit.SECONDS,
  14. new SynchronousQueue<Runnable>(),
  15. new ThreadFactoryBuilder()
  16. .setNameFormat("DiscoveryClient-HeartbeatExecutor-%d")
  17. .setDaemon(true)
  18. .build()
  19. ); // use direct handoff
  20. //拉取注册表的线程池
  21. cacheRefreshExecutor = new ThreadPoolExecutor(
  22. 1, clientConfig.getCacheRefreshExecutorThreadPoolSize(), 0, TimeUnit.SECONDS,
  23. new SynchronousQueue<Runnable>(),
  24. new ThreadFactoryBuilder()
  25. .setNameFormat("DiscoveryClient-CacheRefreshExecutor-%d")
  26. .setDaemon(true)
  27. .build()
  28. ); // use direct handoff
  29. //此处省略部分无关代码
  30. initScheduledTasks(); //调用该方法,该方法会执行对应的线程池
  1. private void initScheduledTasks() {
  2. if (clientConfig.shouldFetchRegistry()) {
  3. // registry cache refresh timer
  4. int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
  5. int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
  6. cacheRefreshTask = new TimedSupervisorTask(
  7. "cacheRefresh",
  8. scheduler,
  9. cacheRefreshExecutor,
  10. registryFetchIntervalSeconds,
  11. TimeUnit.SECONDS,
  12. expBackOffBound,
  13. new DiscoveryClient.CacheRefreshThread() //该方法里面会调用拉取注册表的方法
  14. scheduler.schedule(
  15. cacheRefreshTask,
  16. registryFetchIntervalSeconds, TimeUnit.SECONDS); //开启定时任务
  17. if (clientConfig.shouldRegisterWithEureka()) {
  18. int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
  19. int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();
  20. logger.info("Starting heartbeat executor: " + "renew interval is: {}", renewalIntervalInSecs);
  21. // Heartbeat timer
  22. heartbeatTask = new TimedSupervisorTask(
  23. "heartbeat",
  24. scheduler,
  25. heartbeatExecutor,
  26. renewalIntervalInSecs,
  27. TimeUnit.SECONDS,
  28. expBackOffBound,
  29. new HeartbeatThread();//该线程会去调用发送心跳方法
  30. scheduler.schedule(
  31. heartbeatTask,
  32. renewalIntervalInSecs, TimeUnit.SECONDS); //开启心跳定时任务
  33. //此处省略部分无关代码

//拉取注册列表的线程:

1365950-20200331165422851-1899329896.png

//发送心跳的线程:

1365950-20200331165505912-602881596.png

2. 注册中心服务剔除定时器(注册中心源码(AbstractInstanceRegistry类))

1365950-20200331172841570-165594221.png

1365950-20200331172914689-1005348443.png

总结:eureka注册中心是去化的,注册表是存在内存中的,并且客户端拉取一份注册表后,会存在本地缓存中,因此即使注册中心挂了,一样不影响客户端相互调用

附加: 客户端如何获取服务实例demo

1365950-20200401115056809-1983706216.png

1365950-20200401115237089-387249189.png

综上所述,其实我们也可以自己写个简单的注册中心,思路如下:

1.创建一个springboot项目,写个controller类,提供注册,续约,下线,获取服务列表四个接口

2. 定义一个实例对象Instance,该对象封装ip,端口 还有更新时间

3.客户端调用注册接口,将Instance作为参数传过来,注册中心取到对应实例,存到Map<String,Map<String,Instance>> 中

4.客户端弄个定时器,每个一段时间,调用注册中心的续约方法,将更新实例的修改时间

5.客户端弄个定时器,每隔一段时间向注册中心拉取服务,其实就是拉取Map<String,Map<String,Instance>>

6.注册中心弄个定时器,每隔一段时间遍历Map<String,Map<String,Instance>>,找出每个实例中的更新时间,加上过期时间,然后跟当前时间比较,看看有没过期,如果过期就剔除,也就是在map中删除


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK