use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class UpgradeOpsController method createService.
/**
* Create a new service. This API will create a persistence service.
*
* @param namespaceId namespace id
* @param serviceName service name
* @param protectThreshold protect threshold
* @param metadata service metadata
* @param selector selector
* @return 'ok' if success
* @throws Exception exception
*/
@PostMapping("/service")
@Secured(action = ActionTypes.WRITE)
public String createService(@RequestParam(defaultValue = "v2", required = false) String ver, HttpServletRequest request, @RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(required = false, defaultValue = "0.0F") float protectThreshold, @RequestParam(defaultValue = StringUtils.EMPTY) String metadata, @RequestParam(defaultValue = StringUtils.EMPTY) String selector) throws Exception {
ServiceMetadata serviceMetadata = new ServiceMetadata();
serviceMetadata.setProtectThreshold(protectThreshold);
serviceMetadata.setSelector(parseSelector(selector));
serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(metadata));
boolean ephemeral = ConvertUtils.toBoolean(WebUtils.optional(request, "ephemeral", String.valueOf(switchDomain.isDefaultInstanceEphemeral())));
serviceMetadata.setEphemeral(ephemeral);
getServiceOperator(ver).create(namespaceId, serviceName, serviceMetadata);
return "ok";
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class UpgradeOpsController method getMetrics.
private ObjectNode getMetrics() throws NacosException {
ObjectNode result = JacksonUtils.createEmptyJsonNode();
Set<String> serviceNamesV2 = new HashSet<>();
Set<String> persistentServiceNamesV2 = new HashSet<>();
Set<String> ephemeralServiceNamesV2 = new HashSet<>();
int persistentInstanceCountV2 = 0;
int ephemeralInstanceCountV2 = 0;
Set<String> allNamespaces = com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().getAllNamespaces();
for (String ns : allNamespaces) {
Set<com.alibaba.nacos.naming.core.v2.pojo.Service> services = com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().getSingletons(ns);
for (com.alibaba.nacos.naming.core.v2.pojo.Service service : services) {
String nameWithNs = service.getNamespace() + NAMESPACE_SERVICE_CONNECTOR + service.getGroupedServiceName();
serviceNamesV2.add(nameWithNs);
if (service.isEphemeral()) {
ephemeralServiceNamesV2.add(nameWithNs);
} else {
persistentServiceNamesV2.add(nameWithNs);
}
ServiceInfo data = serviceStorage.getPushData(service);
for (com.alibaba.nacos.api.naming.pojo.Instance instance : data.getHosts()) {
if (instance.isEphemeral()) {
ephemeralInstanceCountV2 += 1;
} else {
persistentInstanceCountV2 += 1;
}
}
}
}
result.put("upgraded", upgradeJudgement.isUseGrpcFeatures());
result.put("isAll20XVersion", upgradeJudgement.isAll20XVersion());
result.put("isDoubleWriteEnabled", switchDomain.isDoubleWriteEnabled());
result.put("doubleWriteDelayTaskCount", doubleWriteDelayTaskEngine.size());
result.put("serviceCountV1", serviceManager.getServiceCount());
result.put("instanceCountV1", serviceManager.getInstanceCount());
result.put("serviceCountV2", MetricsMonitor.getDomCountMonitor().get());
result.put("instanceCountV2", MetricsMonitor.getIpCountMonitor().get());
result.put("subscribeCountV2", MetricsMonitor.getSubscriberCount().get());
result.put("responsibleServiceCountV1", serviceManager.getResponsibleServiceCount());
result.put("responsibleInstanceCountV1", serviceManager.getResponsibleInstanceCount());
result.put("ephemeralServiceCountV2", ephemeralServiceNamesV2.size());
result.put("persistentServiceCountV2", persistentServiceNamesV2.size());
result.put("ephemeralInstanceCountV2", ephemeralInstanceCountV2);
result.put("persistentInstanceCountV2", persistentInstanceCountV2);
Set<String> serviceNamesV1 = serviceManager.getAllServiceNames().entrySet().stream().flatMap(e -> e.getValue().stream().map(name -> {
if (!name.contains(SERVICE_INFO_SPLITER)) {
name = NamingUtils.getGroupedName(name, DEFAULT_GROUP);
}
return e.getKey() + NAMESPACE_SERVICE_CONNECTOR + name;
})).collect(Collectors.toSet());
result.put("service.V1.not.in.V2", String.join("\n", (Collection<String>) CollectionUtils.subtract(serviceNamesV1, serviceNamesV2)));
result.put("service.V2.not.in.V1", String.join("\n", (Collection<String>) CollectionUtils.subtract(serviceNamesV2, serviceNamesV1)));
return result;
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class UpgradeOpsController method updateService.
/**
* Update service.
*
* @param request http request
* @return 'ok' if success
* @throws Exception exception
*/
@PutMapping("/service")
@Secured(action = ActionTypes.WRITE)
public String updateService(@RequestParam(defaultValue = "v2", required = false) String ver, HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
ServiceMetadata serviceMetadata = new ServiceMetadata();
serviceMetadata.setProtectThreshold(NumberUtils.toFloat(WebUtils.required(request, "protectThreshold")));
serviceMetadata.setExtendData(UtilsAndCommons.parseMetadata(WebUtils.optional(request, "metadata", StringUtils.EMPTY)));
serviceMetadata.setSelector(parseSelector(WebUtils.optional(request, "selector", StringUtils.EMPTY)));
com.alibaba.nacos.naming.core.v2.pojo.Service service = com.alibaba.nacos.naming.core.v2.pojo.Service.newService(namespaceId, NamingUtils.getGroupName(serviceName), NamingUtils.getServiceName(serviceName));
getServiceOperator(ver).update(service, serviceMetadata);
return "ok";
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class CatalogServiceV2Impl method getServiceDetail.
@Override
public Object getServiceDetail(String namespaceId, String groupName, String serviceName) throws NacosException {
Service service = Service.newService(namespaceId, groupName, serviceName);
if (!ServiceManager.getInstance().containSingleton(service)) {
throw new NacosException(NacosException.NOT_FOUND, String.format("service %s@@%s is not found!", groupName, serviceName));
}
Optional<ServiceMetadata> metadata = metadataManager.getServiceMetadata(service);
ServiceMetadata detailedService = metadata.orElseGet(ServiceMetadata::new);
ObjectNode serviceObject = JacksonUtils.createEmptyJsonNode();
serviceObject.put(FieldsConstants.NAME, serviceName);
serviceObject.put(FieldsConstants.GROUP_NAME, groupName);
serviceObject.put(FieldsConstants.PROTECT_THRESHOLD, detailedService.getProtectThreshold());
serviceObject.replace(FieldsConstants.SELECTOR, JacksonUtils.transferToJsonNode(detailedService.getSelector()));
serviceObject.replace(FieldsConstants.METADATA, JacksonUtils.transferToJsonNode(detailedService.getExtendData()));
ObjectNode detailView = JacksonUtils.createEmptyJsonNode();
detailView.replace(FieldsConstants.SERVICE, serviceObject);
List<com.alibaba.nacos.api.naming.pojo.Cluster> clusters = new ArrayList<>();
for (String each : serviceStorage.getClusters(service)) {
ClusterMetadata clusterMetadata = detailedService.getClusters().containsKey(each) ? detailedService.getClusters().get(each) : new ClusterMetadata();
com.alibaba.nacos.api.naming.pojo.Cluster clusterView = new Cluster();
clusterView.setName(each);
clusterView.setHealthChecker(clusterMetadata.getHealthChecker());
clusterView.setMetadata(clusterMetadata.getExtendData());
clusterView.setUseIPPort4Check(clusterMetadata.isUseInstancePortForCheck());
clusterView.setDefaultPort(DEFAULT_PORT);
clusterView.setDefaultCheckPort(clusterMetadata.getHealthyCheckPort());
clusterView.setServiceName(service.getGroupedServiceName());
clusters.add(clusterView);
}
detailView.replace(FieldsConstants.CLUSTERS, JacksonUtils.transferToJsonNode(clusters));
return detailView;
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service 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;
}
Aggregations