Search in sources :

Example 1 with DefaultServiceInstance

use of org.apache.dubbo.registry.client.DefaultServiceInstance in project dubbo by alibaba.

the class EtcdServiceDiscoveryTest method testGetInstances.

@Test
public void testGetInstances() throws Exception {
    String serviceName = "EtcdTest77Service";
    Assertions.assertTrue(etcdServiceDiscovery.getInstances(serviceName).isEmpty());
    etcdServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 8080));
    etcdServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", 9809));
    Assertions.assertFalse(etcdServiceDiscovery.getInstances(serviceName).isEmpty());
    List<String> r = convertToIpPort(etcdServiceDiscovery.getInstances(serviceName));
    Assertions.assertTrue(r.contains("127.0.0.1:8080"));
    Assertions.assertTrue(r.contains("127.0.0.1:9809"));
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultServiceInstance

use of org.apache.dubbo.registry.client.DefaultServiceInstance in project dubbo by alibaba.

the class EtcdServiceDiscoveryTest method testRegistry.

@Test
public void testRegistry() throws Exception {
    ServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTestService", "127.0.0.1", 8080);
    Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    etcdServiceDiscovery.register(serviceInstance);
    Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultServiceInstance

use of org.apache.dubbo.registry.client.DefaultServiceInstance in project dubbo by alibaba.

the class EtcdServiceDiscoveryTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    DefaultServiceInstance serviceInstance = new DefaultServiceInstance(valueOf(System.nanoTime()), "EtcdTest34Service", "127.0.0.1", 8080);
    Assertions.assertNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    etcdServiceDiscovery.register(serviceInstance);
    Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    Assertions.assertEquals(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)), new Gson().toJson(serviceInstance));
    serviceInstance.setPort(9999);
    etcdServiceDiscovery.update(serviceInstance);
    Assertions.assertNotNull(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)));
    Assertions.assertEquals(etcdServiceDiscovery.etcdClient.getKVValue(etcdServiceDiscovery.toPath(serviceInstance)), new Gson().toJson(serviceInstance));
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Gson(com.google.gson.Gson) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultServiceInstance

use of org.apache.dubbo.registry.client.DefaultServiceInstance in project dubbo by alibaba.

the class ConsulServiceDiscoveryTest method testRegistration.

@Test
public void testRegistration() throws InterruptedException {
    DefaultServiceInstance serviceInstance = createServiceInstance(SERVICE_NAME, LOCALHOST, NetUtils.getAvailablePort());
    consulServiceDiscovery.register(serviceInstance);
    Thread.sleep(5000);
    List<ServiceInstance> serviceInstances = consulServiceDiscovery.getInstances(SERVICE_NAME);
    assertEquals(serviceInstances.size(), 1);
    assertEquals(serviceInstances.get(0).getId(), Integer.toHexString(serviceInstance.hashCode()));
    assertEquals(serviceInstances.get(0).getHost(), serviceInstance.getHost());
    assertEquals(serviceInstances.get(0).getServiceName(), serviceInstance.getServiceName());
    assertEquals(serviceInstances.get(0).getPort(), serviceInstance.getPort());
    consulServiceDiscovery.unregister(serviceInstance);
    Thread.sleep(5000);
    serviceInstances = consulServiceDiscovery.getInstances(SERVICE_NAME);
    System.out.println(serviceInstances.size());
    assertTrue(serviceInstances.isEmpty());
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultServiceInstance

use of org.apache.dubbo.registry.client.DefaultServiceInstance in project dubbo by alibaba.

the class NacosNamingServiceUtils method toServiceInstance.

/**
 * Convert the {@link Instance} to {@link ServiceInstance}
 *
 * @param instance {@link Instance}
 * @return non-null
 * @since 2.7.5
 */
public static ServiceInstance toServiceInstance(Instance instance) {
    DefaultServiceInstance serviceInstance = new DefaultServiceInstance(instance.getInstanceId(), NamingUtils.getServiceName(instance.getServiceName()), instance.getIp(), instance.getPort());
    serviceInstance.setMetadata(instance.getMetadata());
    serviceInstance.setEnabled(instance.isEnabled());
    serviceInstance.setHealthy(instance.isHealthy());
    return serviceInstance;
}
Also used : DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance)

Aggregations

DefaultServiceInstance (org.apache.dubbo.registry.client.DefaultServiceInstance)16 Test (org.junit.jupiter.api.Test)8 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)6 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 URL (org.apache.dubbo.common.URL)2 ServiceInstancesChangedEvent (org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent)2 Gson (com.google.gson.Gson)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)1 MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)1 ServiceInstanceCustomizer (org.apache.dubbo.registry.client.ServiceInstanceCustomizer)1 ServiceInstancesChangedListener (org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener)1 ZookeeperInstance (org.apache.dubbo.registry.zookeeper.ZookeeperInstance)1