use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.
the class ClusterOperatorV2ImplTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
Service service = Service.newService("namespace_test", "group_test", "name_test");
ServiceManager.getInstance().getSingleton(service);
clusterOperatorV2Impl = new ClusterOperatorV2Impl(metadataOperateServiceMock);
clusterMetadata = new ClusterMetadata();
}
use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.
the class HealthOperatorV2ImplTest method testUpdateHealthStatusForPersistentInstance.
@Test
public void testUpdateHealthStatusForPersistentInstance() {
try {
ServiceMetadata metadata = new ServiceMetadata();
Map<String, ClusterMetadata> clusterMap = new HashMap<>(2);
ClusterMetadata cluster = Mockito.mock(ClusterMetadata.class);
clusterMap.put("C", cluster);
metadata.setClusters(clusterMap);
Instance instance = new Instance();
instance.setIp("1.1.1.1");
instance.setPort(8080);
Mockito.when(cluster.getHealthyCheckType()).thenReturn(HealthCheckType.NONE.name());
Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
ConnectionBasedClient client = Mockito.mock(ConnectionBasedClient.class);
Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(client);
InstancePublishInfo instancePublishInfo = new InstancePublishInfo();
instancePublishInfo.setExtendDatum(new HashMap<>(2));
Mockito.when(client.getInstancePublishInfo(Mockito.any())).thenReturn(instancePublishInfo);
healthOperatorV2.updateHealthStatusForPersistentInstance("A", "B", "C", "1.1.1.1", 8080, true);
} catch (NacosException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.
the class ServiceOperatorV2ImplTest method testQueryService.
@Test
public void testQueryService() throws NacosException {
ClusterMetadata clusterMetadata = new ClusterMetadata();
Map<String, ClusterMetadata> clusterMetadataMap = new HashMap<>(2);
clusterMetadataMap.put("D", clusterMetadata);
ServiceMetadata metadata = new ServiceMetadata();
metadata.setClusters(clusterMetadataMap);
Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
Mockito.when(serviceStorage.getClusters(Mockito.any())).thenReturn(Collections.singleton("D"));
ObjectNode objectNode = serviceOperatorV2.queryService("A", "C");
Assert.assertEquals("A", objectNode.get(FieldsConstants.NAME_SPACE_ID).asText());
Assert.assertEquals("C", objectNode.get(FieldsConstants.NAME).asText());
Assert.assertEquals(1, objectNode.get(FieldsConstants.CLUSTERS).size());
}
use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata 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.metadata.ClusterMetadata in project nacos by alibaba.
the class HealthOperatorV2Impl method updateHealthStatusForPersistentInstance.
@Override
public void updateHealthStatusForPersistentInstance(String namespace, String fullServiceName, String clusterName, String ip, int port, boolean healthy) throws NacosException {
String groupName = NamingUtils.getGroupName(fullServiceName);
String serviceName = NamingUtils.getServiceName(fullServiceName);
Service service = Service.newService(namespace, groupName, serviceName);
Optional<ServiceMetadata> serviceMetadata = metadataManager.getServiceMetadata(service);
if (!serviceMetadata.isPresent() || !serviceMetadata.get().getClusters().containsKey(clusterName)) {
throwHealthCheckerException(fullServiceName, clusterName);
}
ClusterMetadata clusterMetadata = serviceMetadata.get().getClusters().get(clusterName);
if (!HealthCheckType.NONE.name().equals(clusterMetadata.getHealthyCheckType())) {
throwHealthCheckerException(fullServiceName, clusterName);
}
String clientId = IpPortBasedClient.getClientId(ip + InternetAddressUtil.IP_PORT_SPLITER + port, false);
Client client = clientManager.getClient(clientId);
if (null == client) {
return;
}
InstancePublishInfo oldInstance = client.getInstancePublishInfo(service);
if (null == oldInstance) {
return;
}
Instance newInstance = InstanceUtil.parseToApiInstance(service, oldInstance);
newInstance.setHealthy(healthy);
clientOperationService.registerInstance(service, newInstance, clientId);
}
Aggregations