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");
}
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;
}
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;
}
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();
}
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;
}
Aggregations