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

The following examples show how to use com.alibaba.nacos.api.naming.pojo.Instance#setEnabled() . 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
@Override
public void offlineNode(Node node) throws Exception {
    List<String> clusters = new ArrayList<>(1);
    clusters.add(node.getCluster());
    List<Instance> instanceList = nacosNamingService.getAllInstances(ChronusConstants.NODE_NAME_CHRONUS, clusters);
    if (CollectionUtils.isEmpty(instanceList)) {
        return;
    }
    Instance instance = instanceList.stream().filter(e -> InstanceUtils.getAddressByInstance(e).equals(node.getAddress())).findFirst().orElse(null);
    if (instance != null) {
        instance.setEnabled(false);
        if (Objects.equals(ChronusConstants.Y, instance.getMetadata().get(ChronusConstants.ENABLE_MASTER))) {
            instance.getMetadata().put(ChronusConstants.IS_MASTER, ChronusConstants.N);
        }
        namingMaintainService.updateInstance(ChronusConstants.NODE_NAME_CHRONUS, instance);
    }
}
 
Example 2
Source File: NamingServiceNacosImpl.java    From chronus with Apache License 2.0 6 votes vote down vote up
@Override
public void onlineNode(Node node) throws Exception {
    List<String> clusters = new ArrayList<>(1);
    clusters.add(node.getCluster());
    List<Instance> instanceList = nacosNamingService.getAllInstances(ChronusConstants.NODE_NAME_CHRONUS, clusters);
    if (CollectionUtils.isEmpty(instanceList)) {
        return;
    }
    Instance instance = instanceList.stream().filter(e -> InstanceUtils.getAddressByInstance(e).equals(node.getAddress())).findFirst().orElse(null);
    if (instance != null) {
        instance.setEnabled(true);
        instance.getMetadata().put(ChronusConstants.REGISTER_TIME, String.valueOf(System.currentTimeMillis()));
        instance.getMetadata().put(ChronusConstants.DATA_VERSION, String.valueOf(System.currentTimeMillis()));
        namingMaintainService.updateInstance(ChronusConstants.NODE_NAME_CHRONUS, instance);
    }
}
 
Example 3
Source File: NacosSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
public Instance buildSyncInstance(Instance instance, TaskDO taskDO) {
    Instance temp = new Instance();
    temp.setIp(instance.getIp());
    temp.setPort(instance.getPort());
    temp.setClusterName(instance.getClusterName());
    temp.setServiceName(instance.getServiceName());
    temp.setEnabled(instance.isEnabled());
    temp.setHealthy(instance.isHealthy());
    temp.setWeight(instance.getWeight());

    Map<String, String> metaData = new HashMap<>();
    metaData.putAll(instance.getMetadata());
    metaData.put(SkyWalkerConstants.DEST_CLUSTERID_KEY, taskDO.getDestClusterId());
    metaData.put(SkyWalkerConstants.SYNC_SOURCE_KEY,
        skyWalkerCacheServices.getClusterType(taskDO.getSourceClusterId()).getCode());
    metaData.put(SkyWalkerConstants.SOURCE_CLUSTERID_KEY, taskDO.getSourceClusterId());
    temp.setMetadata(metaData);
    return temp;
}
 
Example 4
Source File: NacosNamingServiceUtils.java    From dubbo-registry-nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Convert the {@link ServiceInstance} to {@link Instance}
 *
 * @param serviceInstance {@link ServiceInstance}
 * @return non-null
 * @since 2.7.5
 */
public static Instance toInstance(ServiceInstance serviceInstance) {
    Instance instance = new Instance();
    instance.setInstanceId(serviceInstance.getId());
    instance.setServiceName(serviceInstance.getServiceName());
    instance.setIp(serviceInstance.getHost());
    instance.setPort(serviceInstance.getPort());
    instance.setMetadata(serviceInstance.getMetadata());
    instance.setEnabled(serviceInstance.isEnabled());
    instance.setHealthy(serviceInstance.isHealthy());
    return instance;
}
 
Example 5
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public void setStatus(Registration registration, String status) {

	if (!status.equalsIgnoreCase("UP") && !status.equalsIgnoreCase("DOWN")) {
		log.warn("can't support status {},please choose UP or DOWN", status);
		return;
	}

	String serviceId = registration.getServiceId();

	Instance instance = getNacosInstanceFromRegistration(registration);

	if (status.equalsIgnoreCase("DOWN")) {
		instance.setEnabled(false);
	}
	else {
		instance.setEnabled(true);
	}

	try {
		nacosDiscoveryProperties.namingMaintainServiceInstance()
				.updateInstance(serviceId, instance);
	}
	catch (Exception e) {
		throw new RuntimeException("update nacos instance status fail", e);
	}

}
 
Example 6
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
private Instance getNacosInstanceFromRegistration(Registration registration) {
	Instance instance = new Instance();
	instance.setIp(registration.getHost());
	instance.setPort(registration.getPort());
	instance.setWeight(nacosDiscoveryProperties.getWeight());
	instance.setClusterName(nacosDiscoveryProperties.getClusterName());
	instance.setEnabled(nacosDiscoveryProperties.isInstanceEnabled());
	instance.setMetadata(registration.getMetadata());

	return instance;
}