ConsulServiceDiscovery extends AbstractServiceDiscovery implements EventListener<ServiceInstancesChangedEvent> { @Override public List<ServiceInstance> getInstances(String serviceName) throws NullPointerException { Response<List<HealthService>> response = getHealthServices(serviceName, -1, buildWatchTimeout()); Long consulIndex = response.getConsulIndex(); ConsulNotifier notifier = notifiers.get(serviceName); if (notifier == null) { notifier = new ConsulNotifier(serviceName, consulIndex); notifiers.put(serviceName, notifier); } return convert(response.getValue()); }  @Override void onEvent(ServiceInstancesChangedEvent event); @Override void initialize(URL registryURL); @Override URL getUrl(); @Override void destroy(); @Override void register(ServiceInstance serviceInstance); @Override void addServiceInstancesChangedListener(ServiceInstancesChangedListener listener); @Override void update(ServiceInstance serviceInstance); @Override void unregister(ServiceInstance serviceInstance); @Override Set<String> getServices(); @Override List<ServiceInstance> getInstances(String serviceName);  }
@Test public void testGetInstances() throws Exception { String serviceName = "ConsulTest77Service"; assertTrue(consulServiceDiscovery.getInstances(serviceName).isEmpty()); int portA = NetUtils.getAvailablePort(); int portB = NetUtils.getAvailablePort(); consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", portA)); consulServiceDiscovery.register(new DefaultServiceInstance(valueOf(System.nanoTime()), serviceName, "127.0.0.1", portB)); Thread.sleep(5000); Assertions.assertFalse(consulServiceDiscovery.getInstances(serviceName).isEmpty()); List<String> r = convertToIpPort(consulServiceDiscovery.getInstances(serviceName)); assertTrue(r.contains("127.0.0.1:" + portA)); assertTrue(r.contains("127.0.0.1:" + portB)); }