mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #727 from flystar32/master
add modify Nacos instance status support
This commit is contained in:
commit
bbf7bdee52
@ -17,8 +17,11 @@
|
||||
package org.springframework.cloud.alibaba.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.naming.NamingMaintainFactory;
|
||||
import com.alibaba.nacos.api.naming.NamingMaintainService;
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||
import com.alibaba.nacos.client.naming.NacosNamingMaintainService;
|
||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -147,6 +150,8 @@ public class NacosDiscoveryProperties {
|
||||
|
||||
private NamingService namingService;
|
||||
|
||||
private NamingMaintainService namingMaintainService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws SocketException {
|
||||
|
||||
@ -389,6 +394,34 @@ public class NacosDiscoveryProperties {
|
||||
return namingService;
|
||||
}
|
||||
|
||||
try {
|
||||
namingService = NacosFactory.createNamingService(getNacosProperties());
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("create naming service error!properties={},e=,", this, e);
|
||||
return null;
|
||||
}
|
||||
return namingService;
|
||||
}
|
||||
|
||||
public NamingMaintainService namingMaintainServiceInstance() {
|
||||
|
||||
if (null != namingMaintainService) {
|
||||
return namingMaintainService;
|
||||
}
|
||||
|
||||
try {
|
||||
namingMaintainService = NamingMaintainFactory
|
||||
.createMaintainService(getNacosProperties());
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("create naming service error!properties={},e=,", this, e);
|
||||
return null;
|
||||
}
|
||||
return namingMaintainService;
|
||||
}
|
||||
|
||||
private Properties getNacosProperties() {
|
||||
Properties properties = new Properties();
|
||||
properties.put(SERVER_ADDR, serverAddr);
|
||||
properties.put(NAMESPACE, namespace);
|
||||
@ -407,15 +440,7 @@ public class NacosDiscoveryProperties {
|
||||
properties.put(SECRET_KEY, secretKey);
|
||||
properties.put(CLUSTER_NAME, clusterName);
|
||||
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
||||
|
||||
try {
|
||||
namingService = NacosFactory.createNamingService(properties);
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("create naming service error!properties={},e=,", this, e);
|
||||
return null;
|
||||
}
|
||||
return namingService;
|
||||
return properties;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos.registry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.nacos.api.naming.NamingService;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import org.slf4j.Logger;
|
||||
@ -52,12 +54,7 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
String serviceId = registration.getServiceId();
|
||||
|
||||
Instance instance = new Instance();
|
||||
instance.setIp(registration.getHost());
|
||||
instance.setPort(registration.getPort());
|
||||
instance.setWeight(nacosDiscoveryProperties.getWeight());
|
||||
instance.setClusterName(nacosDiscoveryProperties.getClusterName());
|
||||
instance.setMetadata(registration.getMetadata());
|
||||
Instance instance = getNacosInstanceFromRegistration(registration);
|
||||
|
||||
try {
|
||||
namingService.registerInstance(serviceId, instance);
|
||||
@ -102,13 +99,60 @@ public class NacosServiceRegistry implements ServiceRegistry<Registration> {
|
||||
|
||||
@Override
|
||||
public void setStatus(Registration registration, String status) {
|
||||
// nacos doesn't support set status of a particular registration.
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getStatus(Registration registration) {
|
||||
// nacos doesn't support query status of a particular registration.
|
||||
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;
|
||||
}
|
||||
|
||||
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.setMetadata(registration.getMetadata());
|
||||
return instance;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user