Java Code Examples for com.alibaba.nacos.api.naming.pojo.Instance#isEnabled()

The following examples show how to use com.alibaba.nacos.api.naming.pojo.Instance#isEnabled() . 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: NamingServiceNacosImpl.java    From chronus with Apache License 2.0 6 votes vote down vote up
private Node convertNode(Instance instance) {
    Node node = new Node();
    node.setHostName(instance.getMetadata().get(ChronusConstants.HOST_NAME));
    node.setCluster(instance.getMetadata().get(ChronusConstants.CLUSTER));
    node.setEnableExecutor(instance.getMetadata().get(ChronusConstants.ENABLE_EXECUTOR));
    node.setEnableMaster(instance.getMetadata().get(ChronusConstants.ENABLE_MASTER));
    node.setIsMaster(instance.getMetadata().get(ChronusConstants.IS_MASTER));
    node.setVersion(instance.getMetadata().get(ChronusConstants.REGISTER_TIME));
    node.setDataVersion(instance.getMetadata().get(ChronusConstants.DATA_VERSION));
    node.setTag(instance.getMetadata().get(ChronusConstants.TAG));
    node.setAddress(InstanceUtils.getAddressByInstance(instance));
    if (!instance.isEnabled()) {
        node.setState(NodeStateEnum.OFFLINE.getState());
    } else {
        node.setState(instance.isHealthy() ? NodeStateEnum.NORMAL.getState() : NodeStateEnum.DEAD.getState());
    }
    return node;
}
 
Example 2
Source File: DubboNacosRegistry.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
@Override
protected URL createShardUrl(String defProtocol, Instance instance) {
    Map<String, String> meta = instance.getMetadata();
    String alias = meta.remove(DUBBO_GROUP_KEY);
    alias = alias == null ? ALIAS_OPTION.getValue() : alias;
    if (!instance.isEnabled() || !url.getString(ALIAS_OPTION).equals(alias)) {
        return null;
    }
    String protocol = meta.remove(DUBBO_PROTOCOL_KEY);
    protocol = protocol == null || protocol.isEmpty() ? defProtocol : protocol;
    String ifaceName = meta.remove(DUBBO_PATH_KEY);
    String serviceVersion = meta.remove(DUBBO_SERVICE_VERSION_KEY);
    //重置alias
    meta.put(ALIAS_OPTION.getName(), alias);
    //重置serviceVersion
    meta.put(SERVICE_VERSION_OPTION.getName(), serviceVersion);
    //创建URL
    return new URL(protocol, instance.getIp(), instance.getPort(), ifaceName, meta);
}
 
Example 3
Source File: NacosServiceDiscovery.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
private InstanceInfo createInstanceInfo(Instance instance) {
    InstanceStatus instanceStatus = InstanceStatus.DOWN;
    if (instance.isEnabled()) {
        if (instance.isHealthy()) {
            instanceStatus = InstanceStatus.UP;
        } else {
            instanceStatus = InstanceStatus.OUT_OF_SERVICE;
        }
    }

    return InstanceInfo.builder()
            .serviceId(instance.getServiceName())
            .instanceId(instance.getInstanceId())
            .host(instance.getIp())
            .port(instance.getPort())
            .instanceStatus(instanceStatus)
            .build();
}
 
Example 4
Source File: NacosServiceDiscovery.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
public static ServiceInstance hostToServiceInstance(Instance instance,
		String serviceId) {
	if (instance == null || !instance.isEnabled() || !instance.isHealthy()) {
		return null;
	}
	NacosServiceInstance nacosServiceInstance = new NacosServiceInstance();
	nacosServiceInstance.setHost(instance.getIp());
	nacosServiceInstance.setPort(instance.getPort());
	nacosServiceInstance.setServiceId(serviceId);

	Map<String, String> metadata = new HashMap<>();
	metadata.put("nacos.instanceId", instance.getInstanceId());
	metadata.put("nacos.weight", instance.getWeight() + "");
	metadata.put("nacos.healthy", instance.isHealthy() + "");
	metadata.put("nacos.cluster", instance.getClusterName() + "");
	metadata.putAll(instance.getMetadata());
	nacosServiceInstance.setMetadata(metadata);

	if (metadata.containsKey("secure")) {
		boolean secure = Boolean.parseBoolean(metadata.get("secure"));
		nacosServiceInstance.setSecure(secure);
	}
	return nacosServiceInstance;
}
 
Example 5
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public Object getStatus(Registration registration) {

	String serviceName = registration.getServiceId();
	try {
		List<Instance> instances = nacosDiscoveryProperties.namingServiceInstance()
				.getAllInstances(serviceName);
		for (Instance instance : instances) {
			if (instance.getIp().equalsIgnoreCase(nacosDiscoveryProperties.getIp())
					&& instance.getPort() == nacosDiscoveryProperties.getPort()) {
				return instance.isEnabled() ? "UP" : "DOWN";
			}
		}
	}
	catch (Exception e) {
		log.error("get all instance of {} error,", serviceName, e);
	}
	return null;
}
 
Example 6
Source File: NacosRegistry.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 生成分片URL
 *
 * @param defProtocol
 * @param instance
 * @return
 */
protected URL createShardUrl(String defProtocol, Instance instance) {
    Map<String, String> meta = instance.getMetadata();
    if (!instance.isEnabled() || !url.getString(ALIAS_OPTION).equals(meta.get(ALIAS_OPTION.getName()))) {
        return null;
    }
    String protocol = meta.remove(Constants.PROTOCOL_KEY);
    protocol = protocol == null || protocol.isEmpty() ? defProtocol : protocol;
    return new URL(protocol, instance.getIp(), instance.getPort(), this.getPath(), meta);
}
 
Example 7
Source File: NacosServerListProcessor.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private InstanceStatus getInstanceStatus(Instance instance){
    if(!instance.isEnabled()){
        return InstanceStatus.DOWN;
    }
    if(!instance.isHealthy()){
        return InstanceStatus.OUT_OF_SERVICE;
    }
    return InstanceStatus.UP;
}