Search in sources :

Example 26 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceControllerV2 method create.

/**
 * 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(value = "/{serviceName}")
@Secured(action = ActionTypes.WRITE)
public RestResult<String> create(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @PathVariable String serviceName, @RequestParam(defaultValue = Constants.DEFAULT_GROUP) String groupName, @RequestParam(required = false, defaultValue = "false") boolean ephemeral, @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));
    serviceMetadata.setEphemeral(ephemeral);
    serviceOperatorV2.create(Service.newService(namespaceId, groupName, serviceName, ephemeral), serviceMetadata);
    return RestResultUtils.success("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 27 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class CatalogServiceV2Impl method pageListServiceDetail.

@Override
public Object pageListServiceDetail(String namespaceId, String groupName, String serviceName, int pageNo, int pageSize) throws NacosException {
    List<ServiceDetailInfo> result = new ArrayList<>();
    Collection<Service> services = patternServices(namespaceId, groupName, serviceName);
    services = doPage(services, pageNo - 1, pageSize);
    for (Service each : services) {
        ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo();
        serviceDetailInfo.setServiceName(each.getName());
        serviceDetailInfo.setGroupName(each.getGroup());
        ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(each).orElseGet(ServiceMetadata::new);
        serviceDetailInfo.setMetadata(serviceMetadata.getExtendData());
        serviceDetailInfo.setClusterMap(getClusterMap(each));
        result.add(serviceDetailInfo);
    }
    return result;
}
Also used : ServiceDetailInfo(com.alibaba.nacos.naming.pojo.ServiceDetailInfo) ArrayList(java.util.ArrayList) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Example 28 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class CatalogServiceV2Impl method doPage.

private Collection<Service> doPage(Collection<Service> services, int pageNo, int pageSize) {
    if (services.size() < pageSize) {
        return services;
    }
    Collection<Service> result = new LinkedList<>();
    int i = 0;
    for (Service each : services) {
        if (i++ < pageNo * pageSize) {
            continue;
        }
        result.add(each);
        if (result.size() >= pageSize) {
            break;
        }
    }
    return result;
}
Also used : Service(com.alibaba.nacos.naming.core.v2.pojo.Service) LinkedList(java.util.LinkedList)

Example 29 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class CatalogServiceV2Impl method listInstances.

@Override
public List<? extends Instance> listInstances(String namespaceId, String groupName, String serviceName, String clusterName) 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));
    }
    if (!serviceStorage.getClusters(service).contains(clusterName)) {
        throw new NacosException(NacosException.NOT_FOUND, "cluster " + clusterName + " is not found!");
    }
    ServiceInfo serviceInfo = serviceStorage.getData(service);
    ServiceInfo result = ServiceUtil.selectInstances(serviceInfo, clusterName);
    return result.getHosts();
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 30 with Service

use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.

the class ServiceManager method batchOperate.

/**
 * batch operate kinds of resources.
 *
 * @param namespace       namespace.
 * @param operationInfo   operation resources description.
 * @param operateFunction some operation defined by kinds of situation.
 */
public List<Instance> batchOperate(String namespace, InstanceOperationInfo operationInfo, Function<InstanceOperationContext, List<Instance>> operateFunction) {
    List<Instance> operatedInstances = new ArrayList<>();
    try {
        String serviceName = operationInfo.getServiceName();
        NamingUtils.checkServiceNameFormat(serviceName);
        // type: ephemeral/persist
        InstanceOperationContext operationContext;
        String type = operationInfo.getConsistencyType();
        if (!StringUtils.isEmpty(type)) {
            switch(type) {
                case UtilsAndCommons.EPHEMERAL:
                    operationContext = new InstanceOperationContext(namespace, serviceName, true, true);
                    operatedInstances.addAll(operateFunction.apply(operationContext));
                    break;
                case UtilsAndCommons.PERSIST:
                    operationContext = new InstanceOperationContext(namespace, serviceName, false, true);
                    operatedInstances.addAll(operateFunction.apply(operationContext));
                    break;
                default:
                    Loggers.SRV_LOG.warn("UPDATE-METADATA: services.all value is illegal, it should be ephemeral/persist. ignore the service '" + serviceName + "'");
                    break;
            }
        } else {
            List<Instance> instances = (List<Instance>) operationInfo.getInstances();
            if (!CollectionUtils.isEmpty(instances)) {
                // ephemeral:instances or persist:instances
                Map<Boolean, List<Instance>> instanceMap = instances.stream().collect(Collectors.groupingBy(ele -> ele.isEphemeral()));
                for (Map.Entry<Boolean, List<Instance>> entry : instanceMap.entrySet()) {
                    operationContext = new InstanceOperationContext(namespace, serviceName, entry.getKey(), false, entry.getValue());
                    operatedInstances.addAll(operateFunction.apply(operationContext));
                }
            }
        }
    } catch (Exception e) {
        Loggers.SRV_LOG.warn("UPDATE-METADATA: update metadata failed, ignore the service '" + operationInfo.getServiceName() + "'", e);
    }
    return operatedInstances;
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) Datum(com.alibaba.nacos.naming.consistency.Datum) RecordListener(com.alibaba.nacos.naming.consistency.RecordListener) ServerMemberManager(com.alibaba.nacos.core.cluster.ServerMemberManager) ServiceStatusSynchronizer(com.alibaba.nacos.naming.misc.ServiceStatusSynchronizer) KeyBuilder(com.alibaba.nacos.naming.consistency.KeyBuilder) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) NetUtils(com.alibaba.nacos.naming.misc.NetUtils) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) Collection(java.util.Collection) InternetAddressUtil(com.alibaba.nacos.common.utils.InternetAddressUtil) Loggers(com.alibaba.nacos.naming.misc.Loggers) Resource(javax.annotation.Resource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) RaftPeerSet(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Objects(java.util.Objects) InstanceOperationContext(com.alibaba.nacos.naming.pojo.InstanceOperationContext) UtilsAndCommons(com.alibaba.nacos.naming.misc.UtilsAndCommons) List(java.util.List) Constants(com.alibaba.nacos.api.common.Constants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) PostConstruct(javax.annotation.PostConstruct) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) NacosException(com.alibaba.nacos.api.exception.NacosException) Message(com.alibaba.nacos.naming.misc.Message) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) CollectionUtils(com.alibaba.nacos.common.utils.CollectionUtils) UPDATE_INSTANCE_METADATA_ACTION_REMOVE(com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_REMOVE) Iterator(java.util.Iterator) Synchronizer(com.alibaba.nacos.naming.misc.Synchronizer) NamingUtils(com.alibaba.nacos.api.naming.utils.NamingUtils) EmptyServiceAutoCleaner(com.alibaba.nacos.naming.core.v2.cleaner.EmptyServiceAutoCleaner) TimeUnit(java.util.concurrent.TimeUnit) UPDATE_INSTANCE_METADATA_ACTION_UPDATE(com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_UPDATE) Component(org.springframework.stereotype.Component) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) StringJoiner(java.util.StringJoiner) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) GlobalExecutor(com.alibaba.nacos.naming.misc.GlobalExecutor) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) InstanceOperationContext(com.alibaba.nacos.naming.pojo.InstanceOperationContext) NacosException(com.alibaba.nacos.api.exception.NacosException)

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