在 Eureka 中设置接受区域列表

Set a list of acceped zones in Eureka

他们是在 Eureka 中忽略区域或定义可接受区域列表的一种方法,例如,如果我们有 3 个区域(officeshahbourjoe

我希望区域 shahbour 中的服务仅使用定义在 shahbour 主要区域和 office 中定义的次要服务,并忽略此示例 joe 中的所有其他区域。

我尝试如下所示,它正在努力选择相同的区域,但如果它们在同一区域没有服务,它会在所有其他区域进行负载平衡

spring:
  profiles: shahbour
eureka:
  instance:
    metadataMap:
      zone: shahbour
  client:
    region: lebanon
    serviceUrl:
      defaultZone: http://office:8761/eureka/
    preferSameZoneEureka: true
    availabilityZones:
      lebanon: shahbour,office

我以为设置 availabilityZones 会设置这个,但事实并非如此。

这是用于开发环境的,我正在尝试设置每个开发人员将他的机器用作区域,如果服务不存在,则使用办公室服务器作为备份,但不要使用其他开发人员。

我没有找到任何地方来设置尤里卡接受的区域列表,但我发现我们可以在 Ribbon 中创建我们的自定义 ServerListFilter,它在 [=15] 中使用=] 和 Zuul 所以下面是代码

public class DevServerListFilter extends ZonePreferenceServerListFilter {

    private final List<String> acceptedZone = new ArrayList<>();

    public DevServerListFilter(String[] acceptedZones) {
        for (String zone: acceptedZones) {
            this.acceptedZone.add(zone);
        }
    }

    @Override
    public void initWithNiwsConfig(IClientConfig niwsClientConfig) {
        super.initWithNiwsConfig(niwsClientConfig);
    }

    @Override
    public List<Server> getFilteredListOfServers(List<Server> servers) {
        List<Server> zoneAffinityFiltered = super.getFilteredListOfServers(servers);
        Set<Server> candidates = Sets.newHashSet(zoneAffinityFiltered);
        Iterator serverIterator = candidates.iterator();
        while (serverIterator.hasNext()) {
            Server server = (Server)serverIterator.next();
            if(!acceptedZone.contains(server.getZone())) {
                zoneAffinityFiltered.remove(server);
            }

        }
        return zoneAffinityFiltered;
    }


} 

上面的过滤器扩展 ZonePreferenceServerListFilter 并检查接受区域列表,不在该列表中的任何服务器都将被忽略。

@Configuration
@RibbonClients(defaultConfiguration = MyDefaultRibbonConfiguration.class)
public class MyRibbonConfiguration {
}

我所有客户端的默认配置

@Configuration
public class MyDefaultRibbonConfiguration {

//    @Bean
//    public IPing ribbonPing(IClientConfig config) {
//        return new PingUrl();
//    }

    @Bean
    public ServerListFilter<Server> ribbonServerListFilter(IClientConfig config,EurekaClientConfigBean eurekaClientConfigBean) {
        String[] availabilityZones = eurekaClientConfigBean.getAvailabilityZones(eurekaClientConfigBean.getRegion());

        DevServerListFilter filter = new DevServerListFilter(availabilityZones);
        filter.initWithNiwsConfig(config);
        return filter;
    }

}

配置代码,请注意,这必须在@ComponentScan 的排除路径中作为 documents 中的请求,我使用可用区 属性 但任何列表可以使用。

github sample