@Override
    public Collection<ServiceInstance> getInstances(String serviceId) throws QueryInstanceException {
        if (!getZkClient().isStateOk()) {
            throw new QueryInstanceException("zk state is not valid!");
        }
        checkDiscoveryState();
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(ZkDiscoveryClient.class.getClassLoader());
            return convert(serviceDiscovery.queryForInstances(serviceId));
        } catch (Exception exception) {
            LOGGER.log(Level.WARNING, "Can not query service instances from registry center!", exception);
            Thread.currentThread().setContextClassLoader(contextClassLoader);
            throw new QueryInstanceException(exception.getMessage());
        } finally {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
@Test(expected = QueryInstanceException.class)
    public void getInstances() throws Exception {
        Mockito.when(serviceDiscovery.queryForInstances(serviceName)).thenReturn(Collections.emptyList());
        Assert.assertEquals(Collections.emptyList(), zkDiscoveryClient.getInstances(serviceName));
        Mockito.when(serviceDiscovery.queryForInstances(serviceName)).thenReturn(Collections.singletonList(instance));
        Assert.assertEquals(zkDiscoveryClient.getInstances(serviceName).size(), 1);

        // The simulation throws an exception
        Mockito.when(serviceDiscovery.queryForInstances(serviceName)).thenThrow(new IllegalStateException("wrong"));
        Assert.assertEquals(Collections.emptyList(), zkDiscoveryClient.getInstances(serviceName));
    }