为什么80%的码农都做不了架构师?>>>   

本文主要研究一下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

转载于:https://my.oschina.net/go4it/blog/1815043

聊聊eureka的preferSameZoneEureka参数相关推荐

  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. “比特币耶稣”Roger Ver:比特币现金分叉没有技术论据支持
  2. MySql 存储过程实例(附完整注释)
  3. Angular Material 攻略 04 Icon
  4. java常见排序算法有哪些_Java中常用的6种排序算法详细分解
  5. 准大学生的实用省钱小技巧
  6. Xtreme9.0 - Block Art 线段树
  7. 联合索引会创建几个索引_联合索引在B+树上的存储结构及数据查找方式
  8. 了解Linux操作系统的引导过程
  9. 光伏蓄电池单相并网模型。光伏+MPPT+boost升压电路+桥式逆变 带参考文件,模型说明文件
  10. 抖音一个老人和一个机器人歌曲_抖音M哥很火的歌曲有哪些
  11. 智慧园区导航可视化分析平台技术方案
  12. Noob 靶场渗透记录
  13. java宠物商店_Java如何实现宠物商店管理 Java实现宠物商店管理代码示例
  14. unity应用开发实战案例_Unity AR增强现实开发实战
  15. ERROR | Corrupt JPEG data: 111 extraneous bytes before marker 0xd9...
  16. 纸鸢|物联网云平台倒闭的原因和案例
  17. 浅谈分布式架构搭建-理论知识
  18. 移动硬盘的“磁盘结构损坏且无法读取”问题的解决方法
  19. 如何看linux是ubuntu还是centos
  20. 1056 Mice and Rice (25分)

热门文章

  1. 【Java并发.3】对象的共享
  2. nginx实现共享以及实现原理
  3. eclipse中jsp页面%@page import=javax.servlet.http.*%报错
  4. Python for Infomatics 第12章 网络编程四(译)
  5. hibernate注解方式实现一对多映射
  6. 计算给定坐标系和各个定点坐标的凸多边形的面积
  7. C语言函数指针简单应用
  8. js中邦定事件与解绑支持匿名函数
  9. net_sf_interfacecpp项目诞生宣言
  10. Xcode12 “PushKit apps that use VoIP push must link either CallKit or IncomingCallNotifications