use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata 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.metadata.ServiceMetadata 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.metadata.ServiceMetadata in project nacos by alibaba.
the class InstanceOperatorClientImpl method listInstance.
@Override
public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster, boolean healthOnly) {
Service service = getService(namespaceId, serviceName, true);
// For adapt 1.X subscribe logic
if (subscriber.getPort() > 0 && pushService.canEnablePush(subscriber.getAgent())) {
String clientId = IpPortBasedClient.getClientId(subscriber.getAddrStr(), true);
createIpPortClientIfAbsent(clientId);
clientOperationService.subscribeService(service, subscriber, clientId);
}
ServiceInfo serviceInfo = serviceStorage.getData(service);
ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(service).orElse(null);
ServiceInfo result = ServiceUtil.selectInstancesWithHealthyProtection(serviceInfo, serviceMetadata, cluster, healthOnly, true, subscriber.getIp());
// adapt for v1.x sdk
result.setName(NamingUtils.getGroupedName(result.getName(), result.getGroupName()));
return result;
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class PushExecuteTask method generatePushData.
private PushDataWrapper generatePushData() {
ServiceInfo serviceInfo = delayTaskEngine.getServiceStorage().getPushData(service);
ServiceMetadata serviceMetadata = delayTaskEngine.getMetadataManager().getServiceMetadata(service).orElse(null);
return new PushDataWrapper(serviceMetadata, serviceInfo);
}
use of com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata in project nacos by alibaba.
the class CatalogServiceV2ImplTest method testGetServiceDetail.
@Test
public void testGetServiceDetail() {
try {
ServiceMetadata serviceMetadata = new ServiceMetadata();
serviceMetadata.setProtectThreshold(0.75F);
Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(serviceMetadata));
Mockito.when(serviceStorage.getClusters(Mockito.any())).thenReturn(Collections.singleton("C"));
Object obj = catalogServiceV2Impl.getServiceDetail("A", "B", "C");
ObjectNode objectNode = (ObjectNode) obj;
Assert.assertEquals("C", objectNode.get(FieldsConstants.SERVICE).get(FieldsConstants.NAME).asText());
Assert.assertEquals("B", objectNode.get(FieldsConstants.SERVICE).get(FieldsConstants.GROUP_NAME).asText());
Assert.assertEquals("none", objectNode.get(FieldsConstants.SERVICE).get(FieldsConstants.SELECTOR).get("type").asText());
Assert.assertEquals(0, objectNode.get(FieldsConstants.SERVICE).get(FieldsConstants.METADATA).size());
Assert.assertEquals(0.75, objectNode.get(FieldsConstants.SERVICE).get(FieldsConstants.PROTECT_THRESHOLD).asDouble(), 0.1);
} catch (NacosException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations