Search in sources :

Example 56 with Service

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";
}
Also used : ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 57 with Service

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;
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) CommonParams(com.alibaba.nacos.api.naming.CommonParams) WebUtils(com.alibaba.nacos.core.utils.WebUtils) URLDecoder(java.net.URLDecoder) HttpRequestInstanceBuilder(com.alibaba.nacos.naming.pojo.instance.HttpRequestInstanceBuilder) Autowired(org.springframework.beans.factory.annotation.Autowired) CanDistro(com.alibaba.nacos.naming.web.CanDistro) NAMESPACE_SERVICE_CONNECTOR(com.alibaba.nacos.naming.misc.UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR) Selector(com.alibaba.nacos.api.selector.Selector) PutMapping(org.springframework.web.bind.annotation.PutMapping) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperatorServiceImpl(com.alibaba.nacos.naming.core.InstanceOperatorServiceImpl) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) MetricsMonitor(com.alibaba.nacos.naming.monitor.MetricsMonitor) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) PostMapping(org.springframework.web.bind.annotation.PostMapping) SelectorManager(com.alibaba.nacos.naming.selector.SelectorManager) DoubleWriteDelayTaskEngine(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.DoubleWriteDelayTaskEngine) Collection(java.util.Collection) Set(java.util.Set) InstanceOperatorClientImpl(com.alibaba.nacos.naming.core.InstanceOperatorClientImpl) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) UtilsAndCommons(com.alibaba.nacos.naming.misc.UtilsAndCommons) Constants(com.alibaba.nacos.api.common.Constants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) ActionTypes(com.alibaba.nacos.plugin.auth.constant.ActionTypes) Optional(java.util.Optional) Secured(com.alibaba.nacos.auth.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) ServiceOperator(com.alibaba.nacos.naming.core.ServiceOperator) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashSet(java.util.HashSet) NumberUtils(com.alibaba.nacos.common.utils.NumberUtils) DEFAULT_GROUP(com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServiceUtil(com.alibaba.nacos.naming.utils.ServiceUtil) CollectionUtils(org.apache.commons.collections.CollectionUtils) SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) NacosException(com.alibaba.nacos.api.exception.NacosException) GetMapping(org.springframework.web.bind.annotation.GetMapping) SERVICE_INFO_SPLITER(com.alibaba.nacos.api.common.Constants.SERVICE_INFO_SPLITER) ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) ConvertUtils(com.alibaba.nacos.common.utils.ConvertUtils) Iterator(java.util.Iterator) ServiceOperatorV2Impl(com.alibaba.nacos.naming.core.ServiceOperatorV2Impl) UpgradeJudgement(com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) InstanceOperator(com.alibaba.nacos.naming.core.InstanceOperator) NamingUtils(com.alibaba.nacos.api.naming.utils.NamingUtils) ServiceOperatorV1Impl(com.alibaba.nacos.naming.core.ServiceOperatorV1Impl) NoneSelector(com.alibaba.nacos.naming.selector.NoneSelector) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 58 with Service

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";
}
Also used : ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 59 with Service

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;
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Cluster(com.alibaba.nacos.api.naming.pojo.Cluster) NacosException(com.alibaba.nacos.api.exception.NacosException) Cluster(com.alibaba.nacos.api.naming.pojo.Cluster) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Example 60 with Service

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;
}
Also used : ServiceView(com.alibaba.nacos.naming.pojo.ServiceView) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) LinkedList(java.util.LinkedList) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Aggregations

Service (com.alibaba.nacos.naming.core.v2.pojo.Service)75 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)20 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)19 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)17 Test (org.junit.Test)16 Instance (com.alibaba.nacos.api.naming.pojo.Instance)13 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)13 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)13 NacosException (com.alibaba.nacos.api.exception.NacosException)11 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)11 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)11 HashSet (java.util.HashSet)11 Secured (com.alibaba.nacos.auth.annotation.Secured)10 Before (org.junit.Before)10 Client (com.alibaba.nacos.naming.core.v2.client.Client)9 ClientOperationEvent (com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent)8 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)8 InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)8 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)7 Set (java.util.Set)7