com.alibaba.cloud.nacos.ribbon.NacosServer Java Examples

The following examples show how to use com.alibaba.cloud.nacos.ribbon.NacosServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VersionIsolationRule.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 优先根据版本号取实例
 */
@Override
public Server choose(ILoadBalancer lb, Object key) {
    if (lb == null) {
        return null;
    }
    String version;
    if (key != null && !KEY_DEFAULT.equals(key)) {
        version = key.toString();
    } else {
        version = LbIsolationContextHolder.getVersion();
    }

    List<Server> targetList = null;
    List<Server> upList = lb.getReachableServers();
    if (StrUtil.isNotEmpty(version)) {
        //取指定版本号的实例
        targetList = upList.stream().filter(
                server -> version.equals(
                        ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION)
                )
        ).collect(Collectors.toList());
    }

    if (CollUtil.isEmpty(targetList)) {
        //只取无版本号的实例
        targetList = upList.stream().filter(
                server -> {
                    String metadataVersion = ((NacosServer) server).getMetadata().get(CommonConstant.METADATA_VERSION);
                    return StrUtil.isEmpty(metadataVersion);
                }
        ).collect(Collectors.toList());
    }

    if (CollUtil.isNotEmpty(targetList)) {
        return getServer(targetList);
    }
    return super.choose(lb, key);
}
 
Example #2
Source File: TestController.java    From alibabacloud-microservice-demo with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/servers", method = RequestMethod.GET)
public List<NacosServer> servers() {

	DefaultClientConfigImpl iClientConfig = new DefaultClientConfigImpl();
	iClientConfig.setClientName("service-provider");

	NacosServerList serverList = new NacosServerList(properties);
	serverList.initWithNiwsConfig(iClientConfig);

	return serverList.getInitialListOfServers();
}
 
Example #3
Source File: NacosServerListProcessor.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Server> getServers(String serviceId, List<Server> servers) {
    List<InstanceStatus> statusList = getHoldoutInstanceStatus(serviceId);
    List<Server> holdoutServers = getInstances(serviceId).stream().filter(instance -> statusList.contains(getInstanceStatus(instance)))
            .map(NacosServer::new).collect(Collectors.toList());
    if(CollectionUtils.isEmpty(holdoutServers)){
        return servers;
    }
    return ListUtils.union(servers, holdoutServers);
}
 
Example #4
Source File: MetadataAwarePredicate.java    From lion with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean apply(NacosServer server) {
    final RibbonFilterContext context = RibbonFilterContextHolder.getCurrentContext();
    final Set<Map.Entry<String, String>> attributes = Collections.unmodifiableSet(context.getAttributes().entrySet());
    final Map<String, String> metadata = server.getMetadata();
    return metadata.entrySet().containsAll(attributes);
}
 
Example #5
Source File: RuleConfigure.java    From microservices-platform with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnClass(NacosServer.class)
@ConditionalOnMissingBean
public IRule versionIsolationRule() {
    return new VersionIsolationRule();
}
 
Example #6
Source File: DiscoveryEnabledPredicate.java    From lion with Apache License 2.0 4 votes vote down vote up
@Override
public boolean apply(@Nullable PredicateKey input) {
    return input != null
            && input.getServer() instanceof NacosServer
            && apply((NacosServer) input.getServer());
}
 
Example #7
Source File: DiscoveryEnabledPredicate.java    From lion with Apache License 2.0 votes vote down vote up
protected abstract boolean apply(NacosServer server);