Search in sources :

Example 6 with ClusterMetadata

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();
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Before(org.junit.Before)

Example 7 with 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());
    }
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) HashMap(java.util.HashMap) ConnectionBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 8 with ClusterMetadata

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());
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 9 with ClusterMetadata

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;
}
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 10 with ClusterMetadata

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);
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) IpPortBasedClient(com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient) Client(com.alibaba.nacos.naming.core.v2.client.Client) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Aggregations

ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)12 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)7 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)6 NacosException (com.alibaba.nacos.api.exception.NacosException)4 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)3 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)3 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)2 Cluster (com.alibaba.nacos.api.naming.pojo.Cluster)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 AbstractHealthChecker (com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker)1 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)1 Secured (com.alibaba.nacos.auth.annotation.Secured)1 Header (com.alibaba.nacos.common.http.param.Header)1 Cluster (com.alibaba.nacos.naming.core.Cluster)1 Service (com.alibaba.nacos.naming.core.Service)1 Client (com.alibaba.nacos.naming.core.v2.client.Client)1