最近在研究spring cloud eureka集群配置的时候碰到问题:多台eureka server如果需要互相注册,需要在配置文件中将其他服务器地址配置写死.同样客户端启用服务发现功能(eureka client)也需要配置服务端地址列表(其实eureka server与其他eureka server通信也是用的是eureka client组件).按照官方案例提供3台server,如果现在需要增加第四台,第五台...那么问题就来了,所有eureka client的serverUrls列表是否都得更新(修改配置文件)?
一句话总结如上问题就是:eureka集群有什么办法能支持动态集群(集群数量可增减客户端不需要改动任何内容)?
经过寻找发现spring cloud eureka client提供一个eureka.client.useDnsForFetchingServiceUrls选项,使用Dns获取服务地址.
经过各种了解,明确了该配置项就是启用dns来存储eureka server列表的,可以实现动态eureka server集群的功能.但是问题又来了,相关属性还有那些?dns又该如何配置呢?
相关属性好找,有网友提供的例子,dns这块没有比较明确的说明,为了弄明白这块自己尝试着看了看源码,结果找到了DNS里面关于具体如何使用DNS结果的相关代码.代码如下:
getDiscoveryServiceUrls的作用是获取所有eureka service urls,该方法首先判断是否需要从DNS获取服务列表,图中红框部分就是从DNS获取服务列表,继续往下分析:
在分析之前,插播一个知识点:eureka集群与region和zone这几个概念的关系.http://www.vccoo.com/v/bqq4vj
继续看代码:
复制代码
1 /**
2 * Get the list of all eureka service urls from DNS for the eureka client to
3 * talk to. The client picks up the service url from its zone and then fails over to
4 * other zones randomly. If there are multiple servers in the same zone, the client once
5 * again picks one randomly. This way the traffic will be distributed in the case of failures.
6 *
7 * @param clientConfig the clientConfig to use
8 * @param instanceZone The zone in which the client resides.
9 * @param preferSameZone true if we have to prefer the same zone as the client, false otherwise.
10 * @param randomizer a randomizer to randomized returned urls
11 *
12 * @return The list of all eureka service urls for the eureka client to talk to.
13 */
14 public static List getServiceUrlsFromDNS(EurekaClientConfig clientConfig, String instanceZone, boolean preferSameZone, ServiceUrlRandomizer randomizer) {
15 String region = getRegion(clientConfig);
16 // Get zone-specific DNS names for the given region so that we can get a
17 // list of available zones
18 Map> zoneDnsNamesMap = getZoneBasedDiscovery