Java Code Examples for org.springframework.cloud.client.serviceregistry.Registration#getServiceId()

The following examples show how to use org.springframework.cloud.client.serviceregistry.Registration#getServiceId() . 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: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Override
public void deregister(Registration registration) {

	log.info("De-registering from Nacos Server now...");

	if (StringUtils.isEmpty(registration.getServiceId())) {
		log.warn("No dom to de-register for nacos client...");
		return;
	}

	NamingService namingService = namingService();
	String serviceId = registration.getServiceId();
	String group = nacosDiscoveryProperties.getGroup();

	try {
		namingService.deregisterInstance(serviceId, group, registration.getHost(),
				registration.getPort(), nacosDiscoveryProperties.getClusterName());
	}
	catch (Exception e) {
		log.error("ERR_NACOS_DEREGISTER, de-register failed...{},",
				registration.toString(), e);
	}

	log.info("De-registration finished.");
}
 
Example 2
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 3
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public void register(Registration registration) {

	if (StringUtils.isEmpty(registration.getServiceId())) {
		log.warn("No service to register for nacos client...");
		return;
	}

	NamingService namingService = namingService();
	String serviceId = registration.getServiceId();
	String group = nacosDiscoveryProperties.getGroup();

	Instance instance = getNacosInstanceFromRegistration(registration);

	try {
		namingService.registerInstance(serviceId, group, instance);
		log.info("nacos registry, {} {} {}:{} register finished", group, serviceId,
				instance.getIp(), instance.getPort());
	}
	catch (Exception e) {
		log.error("nacos registry, {} register failed...{},", serviceId,
				registration.toString(), e);
		// rethrow a RuntimeException if the registration is failed.
		// issue : https://github.com/alibaba/spring-cloud-alibaba/issues/1132
		rethrowRuntimeException(e);
	}
}
 
Example 4
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);
	}

}