Search in sources :

Example 61 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class InstanceOperatorClientImplTest method testListInstance.

@Test
public void testListInstance() {
    Mockito.when(pushService.canEnablePush(Mockito.anyString())).thenReturn(true);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setGroupName("DEFAULT_GROUP");
    serviceInfo.setName("B");
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    ServiceMetadata metadata = new ServiceMetadata();
    Mockito.when(metadataManager.getServiceMetadata(Mockito.any())).thenReturn(Optional.of(metadata));
    Subscriber subscriber = new Subscriber("2.2.2.2", "", "app", "1.1.1.1", "A", "B", 8848);
    instanceOperatorClient.listInstance("A", "B", subscriber, "C", true);
    Mockito.verify(clientOperationService).subscribeService(Mockito.any(), Mockito.any(), Mockito.anyString());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Subscriber(com.alibaba.nacos.naming.pojo.Subscriber) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) Test(org.junit.Test)

Example 62 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class InstanceOperatorClientImplTest method testBatchDeleteMetadata.

@Test
public void testBatchDeleteMetadata() throws NacosException {
    Instance instance = new Instance();
    instance.setServiceName("C");
    instance.setIp("1.1.1.1");
    instance.setPort(8848);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setHosts(Collections.singletonList(instance));
    Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
    List<String> res = instanceOperatorClient.batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>());
    Assert.assertEquals(1, res.size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) Test(org.junit.Test)

Example 63 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceListTest_ITCase method getSubscribeServices_deregisterInstance.

/**
 * @throws NacosException
 * @description 删除注册,获取当前订阅的所有服务
 */
@Test
public void getSubscribeServices_deregisterInstance() throws NacosException, InterruptedException {
    listenseCount = 0;
    EventListener listener = new EventListener() {

        @Override
        public void onEvent(Event event) {
            System.out.println(((NamingEvent) event).getServiceName());
            System.out.println(((NamingEvent) event).getInstances());
            listenseCount++;
        }
    };
    List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
    int count = serviceInfoList.size();
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    naming.subscribe(serviceName, listener);
    serviceInfoList = naming.getSubscribeServices();
    Assert.assertEquals(count + 1, serviceInfoList.size());
    naming.deregisterInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    Assert.assertEquals(count + 1, serviceInfoList.size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 64 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceListTest_ITCase method getSubscribeServices.

/**
 * @throws NacosException
 * @description 获取当前订阅的所有服务
 */
@Test
public void getSubscribeServices() throws NacosException, InterruptedException {
    ListView<String> listView = naming.getServicesOfServer(1, 10);
    if (listView != null && listView.getCount() > 0) {
        naming.getAllInstances(listView.getData().get(0));
    }
    List<ServiceInfo> serviceInfoList = naming.getSubscribeServices();
    int count = serviceInfoList.size();
    String serviceName = randomDomainName();
    naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
    naming.subscribe(serviceName, new EventListener() {

        @Override
        public void onEvent(Event event) {
        }
    });
    serviceInfoList = naming.getSubscribeServices();
    Assert.assertEquals(count + 1, serviceInfoList.size());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Event(com.alibaba.nacos.api.naming.listener.Event) NamingEvent(com.alibaba.nacos.api.naming.listener.NamingEvent) EventListener(com.alibaba.nacos.api.naming.listener.EventListener) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 65 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class DoubleWriteServiceRemovalToV2Task method run.

@Override
public void run() {
    try {
        InstanceOperatorClientImpl instanceOperator = ApplicationUtils.getBean(InstanceOperatorClientImpl.class);
        ClientServiceIndexesManager clientServiceIndexesManager = ApplicationUtils.getBean(ClientServiceIndexesManager.class);
        ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
        ServiceInfo serviceInfo = serviceStorage.getPushData(service);
        for (Instance instance : serviceInfo.getHosts()) {
            instanceOperator.removeInstance(service.getNamespace(), service.getName(), instance);
        }
        int count = 0;
        while (!clientServiceIndexesManager.getAllClientsRegisteredService(service).isEmpty() && count < MAX_WAIT_TIMES) {
            TimeUnit.MILLISECONDS.sleep(100);
            count += 1;
        }
        clientServiceIndexesManager.removePublisherIndexesByEmptyService(service);
        ServiceManager.getInstance().removeSingleton(service);
        serviceStorage.removeData(service);
        NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, true));
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write removal of {} from 1 to 2 failed", service, e);
        }
        ServiceChangeV1Task retryTask = new ServiceChangeV1Task(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral(), DoubleWriteContent.BOTH, DoubleWriteAction.REMOVE);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV1Task.getKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) Instance(com.alibaba.nacos.api.naming.pojo.Instance) MetadataEvent(com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent) ClientServiceIndexesManager(com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager) ServiceChangeV1Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV1Task) InstanceOperatorClientImpl(com.alibaba.nacos.naming.core.InstanceOperatorClientImpl)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)74 Test (org.junit.Test)44 Instance (com.alibaba.nacos.api.naming.pojo.Instance)29 ArrayList (java.util.ArrayList)17 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)12 Properties (java.util.Properties)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)8 Before (org.junit.Before)8 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)7 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)5 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)4 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 HashMap (java.util.HashMap)4 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)3 Secured (com.alibaba.nacos.auth.annotation.Secured)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3