本文主要研究一下eureka的preferSameZoneEureka参数

配置

eureka.client.prefer-same-zone-eureka

    {"sourceType": "org.springframework.cloud.netflix.eureka.EurekaClientConfigBean","defaultValue": true,"name": "eureka.client.prefer-same-zone-eureka","description": "Indicates whether or not this instance should try to use the eureka server in the\n same zone for latency and\/or other reason.\n\n Ideally eureka clients are configured to talk to servers in the same zone\n\n The changes are effective at runtime at the next registry fetch cycle as specified\n by registryFetchIntervalSeconds","type": "java.lang.Boolean"}

EurekaClientConfigBean

spring-cloud-netflix-eureka-client-2.0.0.RC1-sources.jar!/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java

@ConfigurationProperties(EurekaClientConfigBean.PREFIX)
public class EurekaClientConfigBean implements EurekaClientConfig {/*** Indicates whether or not this instance should try to use the eureka server in the* same zone for latency and/or other reason.** Ideally eureka clients are configured to talk to servers in the same zone** The changes are effective at runtime at the next registry fetch cycle as specified* by registryFetchIntervalSeconds*/private boolean preferSameZoneEureka = true;public boolean shouldPreferSameZoneEureka() {return this.preferSameZoneEureka;}//......
}

使用

EndpointUtils.getDiscoveryServiceUrls

eureka-client-1.8.8-sources.jar!/com/netflix/discovery/endpoint/EndpointUtils.java

    /*** Get the list of all eureka service urls for the eureka client to talk to.** @param clientConfig the clientConfig to use* @param zone the zone in which the client resides* @param randomizer a randomizer to randomized returned urls, if loading from dns** @return The list of all eureka service urls for the eureka client to talk to.*/public static List<String> getDiscoveryServiceUrls(EurekaClientConfig clientConfig, String zone, ServiceUrlRandomizer randomizer) {boolean shouldUseDns = clientConfig.shouldUseDnsForFetchingServiceUrls();if (shouldUseDns) {return getServiceUrlsFromDNS(clientConfig, zone, clientConfig.shouldPreferSameZoneEureka(), randomizer);}return getServiceUrlsFromConfig(clientConfig, zone, clientConfig.shouldPreferSameZoneEureka());}

getServiceUrlsFromConfig

   /*** Get the list of all eureka service urls from properties file for the eureka client to talk to.** @param clientConfig the clientConfig to use* @param instanceZone The zone in which the client resides* @param preferSameZone true if we have to prefer the same zone as the client, false otherwise* @return The list of all eureka service urls for the eureka client to talk to*/public static List<String> getServiceUrlsFromConfig(EurekaClientConfig clientConfig, String instanceZone, boolean preferSameZone) {List<String> orderedUrls = new ArrayList<String>();String region = getRegion(clientConfig);String[] availZones = clientConfig.getAvailabilityZones(clientConfig.getRegion());if (availZones == null || availZones.length == 0) {availZones = new String[1];availZones[0] = DEFAULT_ZONE;}logger.debug("The availability zone for the given region {} are {}", region, availZones);int myZoneOffset = getZoneOffset(instanceZone, preferSameZone, availZones);List<String> serviceUrls = clientConfig.getEurekaServerServiceUrls(availZones[myZoneOffset]);if (serviceUrls != null) {orderedUrls.addAll(serviceUrls);}int currentOffset = myZoneOffset == (availZones.length - 1) ? 0 : (myZoneOffset + 1);while (currentOffset != myZoneOffset) {serviceUrls = clientConfig.getEurekaServerServiceUrls(availZones[currentOffset]);if (serviceUrls != null) {orderedUrls.addAll(serviceUrls);}if (currentOffset == (availZones.length - 1)) {currentOffset = 0;} else {currentOffset++;}}if (orderedUrls.size() < 1) {throw new IllegalArgumentException("DiscoveryClient: invalid serviceUrl specified!");}return orderedUrls;}

这里把preferSameZone参数传入getZoneOffset方法获取zone的offset,然后通过availZones数组取出zone的名称

getZoneOffset

    /*** Gets the zone to pick up for this instance.*/private static int getZoneOffset(String myZone, boolean preferSameZone, String[] availZones) {for (int i = 0; i < availZones.length; i++) {if (myZone != null && (availZones[i].equalsIgnoreCase(myZone.trim()) == preferSameZone)) {return i;}}logger.warn("DISCOVERY: Could not pick a zone based on preferred zone settings. My zone - {}," +" preferSameZone - {}. Defaulting to {}", myZone, preferSameZone, availZones[0]);return 0;}

这里对于所有的availZones,挨个判断是否跟myZone相等,如果是则返回,如果都没找到则日志warn一下然后返回第一个availZone。

小结

eureka的preferSameZoneEureka默认为true,类似Session Affinity,优先选择client实例所在的zone的服务。

doc

  • Understanding eureka client server communication

聊聊eureka的preferSameZoneEureka参数 1相关推荐

  1. 简单聊聊FPGA的一些参数

    笔者:E林1010 在上一篇中,我们已经知道了,FPGA的几个主流厂家和其中Intel家族中FPGA的系列的分类. 上一篇文章链接: https://mp.weixin.qq.com/s/1YufdR ...

  2. 简单聊聊FPGA的一些参数——后篇

    上一篇:https://mp.weixin.qq.com/s/prBScH4D4ixxtbdW_7n9Yg 2. I/O 和体系结构特性 安全器件管理器 英特尔 Stratix 10 器件系列在所有密 ...

  3. 聊聊eureka的delta配置

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究一下eureka的delta配置 client端配置 {"sourceType": "o ...

  4. [case19]聊聊eureka的TaskDispatcher

    序 本文主要研究一下eureka的TaskDispatcher PeerEurekaNode public class PeerEurekaNode {public PeerEurekaNode(Pe ...

  5. 聊聊lettuce的shareNativeConnection参数

    序 本文主要研究一下lettuce的shareNativeConnection参数 LettuceConnectionFactory spring-data-redis-2.0.10.RELEASE- ...

  6. 聊聊Eureka Server的REST API

    序 本文主要研究下Eureka Server的REST API ApplicationsResource eureka-core-1.8.8-sources.jar!/com/netflix/eure ...

  7. vr设备的服务器性能指标,让我们一起聊聊VR眼镜技术参数

    时下最炙手可热的数码产品无疑就是VR眼镜了,而作为影响VR体验的重要一部分,VR眼镜技术参数也成为了很多人讨论的焦点.目前市面上的VR设备良莠不齐,具体什么样的VR眼镜技术参数规格才是优秀的产品,而这 ...

  8. java forward 修改请求参数_聊聊springboot session timeout参数设置

    序 本文主要介绍下spring boot中对session timeout参数值的设置过程. ServerProperties spring-boot-autoconfigure-1.5.8.RELE ...

  9. Spring Cloud Netflix Eureka 配置参数说明

    为什么80%的码农都做不了架构师?>>>    Eureka Client 配置项(eureka.client.*) org.springframework.cloud.netfli ...

  10. 《Spring Cloud Netflix官方文档》1.服务发现:Eureka客户端

    1.     服务发现:Eureka客户端 服务发现是微服务架构的关键原则之一.使用手动配置或一些约定方式来处理多服务多实例的方式是非常困难,并且十分脆弱的.Eureka同时是Netflix服务发现的 ...

最新文章

  1. vs怎么编译php文件,vscode怎么初始编译
  2. 为什么程序return -1或exit(-1)而获取到的值是255
  3. linux链接时区分动态库,Linux修改时区的方法
  4. SAP数据表(一)商品表
  5. ACL 2021 | ConSERT:基于对比学习的句子语义表示迁移框架
  6. [Leedcode][JAVA][第55题][跳跃游戏][贪心][动态规划]
  7. python 登录接口_使用python编写一个登录接口
  8. bzoj4602 [Sdoi2016]齿轮 边权并查集
  9. 中国移动将于11月4日首发上会 拟募资560亿元
  10. 原来,我们的时间这样被科技巨头们瓜分(转)
  11. 左连接与右连接的区别
  12. ICLR2021 | 唯一录取的NER论文:NER数据存在漏标怎么办?
  13. 联想服务器查看运行状态,服务器硬件批量监控工具
  14. Linux公网渗透抓鸡实验
  15. 设置新版谷歌浏览器自动启用flash
  16. 显卡内存足够但是torch报错RuntimeError: CUDA out of memory
  17. Scratch幻影小猫 电子学会图形化编程scratch等级考试三级真题答案2019-9
  18. 【imx6ul】imx6ul上电boot rom启动过程
  19. kodu_Kodu教您的孩子直观地编程自己的视频游戏
  20. 用云服务器搭建虚拟主机,用云服务器搭建虚拟主机

热门文章

  1. React 组件开发 传参(详解)。
  2. Android中使用Fragment实现标题栏(不可滑动)
  3. mysql查询交叉连接_MySQL表连接(内连接、交叉连接、外连接、联合查询)-阿里云开发者社区...
  4. hadoop使用mapreduce统计词频_Hadoop自带WordCount进行词频统计(mapreduce)
  5. net core文件接收(jpg、png、zip、pdf等自己设置)
  6. linux服务器启动ftp连接
  7. PHP使用PHPExcel删除Excel单元格指定列的方法
  8. YAF redis
  9. JAVA NP插件,特定的宽度600,插件就不显示
  10. 全网首发:明明已安装也存在,报错找不到lualib.h