代码之家  ›  专栏  ›  技术社区  ›  Shahbour

在Eureka中设置已访问区域的列表

  •  3
  • Shahbour  · 技术社区  · 9 年前

    它们是一种忽略区域或定义Eureka中可接受区域列表的方法吗,例如,如果我们有3个区域( office , shahbour joe )

    仅使用中定义的服务 沙布尔 作为次要区域,忽略本例中的所有其他区域 乔 .

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

    我认为设置可用区可以设置这个,但事实并非如此。

    这是为了开发环境,我试图设置每个开发人员使用他的机器作为一个区域,如果服务不存在,使用office server作为备份,但不要使用其他开发人员。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Shahbour    9 年前

    我没有找到任何地方来设置eureka中可接受的区域列表,但我发现我们可以创建自定义区域 ServerListFilter 在里面 Ribbon Feign 和 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 ,我使用了availability zone属性,但可以使用任何列表。

    github sample