use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class CatalogServiceV2Impl method pageListService.
@Override
public Object pageListService(String namespaceId, String groupName, String serviceName, int pageNo, int pageSize, String instancePattern, boolean ignoreEmptyService) throws NacosException {
ObjectNode result = JacksonUtils.createEmptyJsonNode();
List<ServiceView> serviceViews = new LinkedList<>();
Collection<Service> services = patternServices(namespaceId, groupName, serviceName);
if (ignoreEmptyService) {
services = services.stream().filter(each -> 0 != serviceStorage.getData(each).ipCount()).collect(Collectors.toList());
}
result.put(FieldsConstants.COUNT, services.size());
services = doPage(services, pageNo - 1, pageSize);
for (Service each : services) {
ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(each).orElseGet(ServiceMetadata::new);
ServiceView serviceView = new ServiceView();
serviceView.setName(each.getName());
serviceView.setGroupName(each.getGroup());
serviceView.setClusterCount(serviceStorage.getClusters(each).size());
serviceView.setIpCount(serviceStorage.getData(each).ipCount());
serviceView.setHealthyInstanceCount(countHealthyInstance(serviceStorage.getData(each)));
serviceView.setTriggerFlag(isProtectThreshold(serviceView, serviceMetadata) ? "true" : "false");
serviceViews.add(serviceView);
}
result.set(FieldsConstants.SERVICE_LIST, JacksonUtils.transferToJsonNode(serviceViews));
return result;
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class HealthOperatorV2Impl method updateHealthStatusForPersistentInstance.
@Override
public void updateHealthStatusForPersistentInstance(String namespace, String fullServiceName, String clusterName, String ip, int port, boolean healthy) throws NacosException {
String groupName = NamingUtils.getGroupName(fullServiceName);
String serviceName = NamingUtils.getServiceName(fullServiceName);
Service service = Service.newService(namespace, groupName, serviceName);
Optional<ServiceMetadata> serviceMetadata = metadataManager.getServiceMetadata(service);
if (!serviceMetadata.isPresent() || !serviceMetadata.get().getClusters().containsKey(clusterName)) {
throwHealthCheckerException(fullServiceName, clusterName);
}
ClusterMetadata clusterMetadata = serviceMetadata.get().getClusters().get(clusterName);
if (!HealthCheckType.NONE.name().equals(clusterMetadata.getHealthyCheckType())) {
throwHealthCheckerException(fullServiceName, clusterName);
}
String clientId = IpPortBasedClient.getClientId(ip + InternetAddressUtil.IP_PORT_SPLITER + port, false);
Client client = clientManager.getClient(clientId);
if (null == client) {
return;
}
InstancePublishInfo oldInstance = client.getInstancePublishInfo(service);
if (null == oldInstance) {
return;
}
Instance newInstance = InstanceUtil.parseToApiInstance(service, oldInstance);
newInstance.setHealthy(healthy);
clientOperationService.registerInstance(service, newInstance, clientId);
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class ServiceMetadataProcessor method addClusterMetadataToService.
private void addClusterMetadataToService(MetadataOperation<ServiceMetadata> op) {
Service service = Service.newService(op.getNamespace(), op.getGroup(), op.getServiceName(), op.getMetadata().isEphemeral());
Optional<ServiceMetadata> currentMetadata = namingMetadataManager.getServiceMetadata(service);
if (currentMetadata.isPresent()) {
currentMetadata.get().getClusters().putAll(op.getMetadata().getClusters());
} else {
Service singleton = ServiceManager.getInstance().getSingleton(service);
namingMetadataManager.updateServiceMetadata(singleton, op.getMetadata());
}
doubleWriteMetadata(service, false);
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class ServiceOperatorV2Impl method queryService.
@Override
public ObjectNode queryService(String namespaceId, String serviceName) throws NacosException {
Service service = getServiceFromGroupedServiceName(namespaceId, serviceName, true);
if (!ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.INVALID_PARAM, "service not found, namespace: " + namespaceId + ", serviceName: " + serviceName);
}
ObjectNode result = JacksonUtils.createEmptyJsonNode();
ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(service).orElse(new ServiceMetadata());
setServiceMetadata(result, serviceMetadata, service);
ArrayNode clusters = JacksonUtils.createEmptyArrayNode();
for (String each : serviceStorage.getClusters(service)) {
ClusterMetadata clusterMetadata = serviceMetadata.getClusters().containsKey(each) ? serviceMetadata.getClusters().get(each) : new ClusterMetadata();
clusters.add(newClusterNode(each, clusterMetadata));
}
result.set(FieldsConstants.CLUSTERS, clusters);
return result;
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class ServiceOperatorV2Impl method queryService.
/**
* Query service detail.
*
* @param service service
* @return service detail with cluster info
* @throws NacosException nacos exception during query
*/
public ServiceDetailInfo queryService(Service service) throws NacosException {
if (!ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.INVALID_PARAM, "service not found, namespace: " + service.getNamespace() + ", serviceName: " + service.getGroupedServiceName());
}
Service singleton = ServiceManager.getInstance().getSingleton(service);
ServiceDetailInfo result = new ServiceDetailInfo();
ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(singleton).orElse(new ServiceMetadata());
setServiceMetadata(result, serviceMetadata, singleton);
Map<String, ClusterInfo> clusters = new HashMap<>(2);
for (String each : serviceStorage.getClusters(singleton)) {
ClusterMetadata clusterMetadata = serviceMetadata.getClusters().containsKey(each) ? serviceMetadata.getClusters().get(each) : new ClusterMetadata();
clusters.put(each, newClusterNodeV2(each, clusterMetadata));
}
result.setClusterMap(clusters);
result.setEphemeral(singleton.isEphemeral());
return result;
}
Aggregations