坐落在尤里卡
问题描述:
acceped区列表
是他们的一个办法忽略区或尤里卡定义接受区的列表,例如,如果我们有3个区(office
,shahbour
,joe
)坐落在尤里卡
我想要的服务区域shahbour
仅使用shahbour
primary和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设置这一点,但它不是 。
这是针对开发环境,我试图设置每个开发人员将他的机器用作区域,如果服务不存在,请使用Office服务器作为备份,但不要使用其他开发人员。
答
我没有找到任何地方设置尤里卡区的认可名单,但我发现的是,我们可以在在两个Feign
和Zuul
所以下面用Ribbon
创建我们的自定义ServerListFilter
是代码
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
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请求排除的路径,我使用的
@Configuration
@RibbonClients(defaultConfiguration = MyDefaultRibbonConfiguration.class)
public class MyRibbonConfiguration {
}
默认配置可用区域属性,但可以使用任何列表。