use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.
the class TestServiceDiscovery method testUnregisterService.
// CURATOR-164
@Test
public void testUnregisterService() throws Exception {
final String name = "name";
final CountDownLatch restartLatch = new CountDownLatch(1);
List<Closeable> closeables = Lists.newArrayList();
InstanceSerializer<String> slowSerializer = new JsonInstanceSerializer<String>(String.class) {
private boolean first = true;
@Override
public byte[] serialize(ServiceInstance<String> instance) throws Exception {
if (first) {
System.out.println("Serializer first registration.");
first = false;
} else {
System.out.println("Waiting for reconnect to finish.");
// Simulate the serialize method being slow.
// This could just be a timed wait, but that's kind of non-deterministic.
restartLatch.await();
}
return super.serialize(instance);
}
};
try {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name(name).port(10064).build();
ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).serializer(slowSerializer).watchInstances(true).build();
closeables.add(discovery);
discovery.start();
Assert.assertFalse(discovery.queryForInstances(name).isEmpty(), "Service should start registered.");
server.stop();
server.restart();
discovery.unregisterService(instance);
restartLatch.countDown();
// Wait for the rest of registration to finish.
new Timing().sleepABit();
Assert.assertTrue(discovery.queryForInstances(name).isEmpty(), "Service should have unregistered.");
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.
the class TestServiceProvider method testBasic.
@Test
public void testBasic() throws Exception {
List<Closeable> closeables = Lists.newArrayList();
try {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build();
closeables.add(discovery);
discovery.start();
ServiceProvider<String> provider = discovery.serviceProviderBuilder().serviceName("test").build();
closeables.add(provider);
provider.start();
Assert.assertEquals(provider.getInstance(), instance);
List<ServiceInstance<String>> list = Lists.newArrayList();
list.add(instance);
Assert.assertEquals(provider.getAllInstances(), list);
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.
the class TestWatchedInstances method testWatchedInstances.
@Test
public void testWatchedInstances() throws Exception {
Timing timing = new Timing();
List<Closeable> closeables = Lists.newArrayList();
try {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).watchInstances(true).build();
closeables.add(discovery);
discovery.start();
Assert.assertEquals(discovery.queryForNames(), Arrays.asList("test"));
List<ServiceInstance<String>> list = Lists.newArrayList();
list.add(instance);
Assert.assertEquals(discovery.queryForInstances("test"), list);
ServiceDiscoveryImpl<String> discoveryImpl = (ServiceDiscoveryImpl<String>) discovery;
ServiceInstance<String> changedInstance = ServiceInstance.<String>builder().id(instance.getId()).address(instance.getAddress()).payload("different").name(instance.getName()).port(instance.getPort()).build();
String path = discoveryImpl.pathForInstance("test", instance.getId());
byte[] bytes = discoveryImpl.getSerializer().serialize(changedInstance);
client.setData().forPath(path, bytes);
timing.sleepABit();
ServiceInstance<String> registeredService = discoveryImpl.getRegisteredService(instance.getId());
Assert.assertNotNull(registeredService);
Assert.assertEquals(registeredService.getPayload(), "different");
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.
the class TestFastjsonTypeReference method test.
@Test
public void test() {
final NodeStatus status = new NodeStatus();
status.setActiveCount(1);
ServiceInstance<NodeStatus> instance = JSON.parseObject(new JSONObject() {
{
put("payload", status);
}
}.toJSONString(), new TypeReference<ServiceInstance<NodeStatus>>() {
});
Assert.assertEquals(instance.getPayload().getActiveCount(), 1);
}
use of org.apache.curator.x.discovery.ServiceInstance in project xian by happyyangyuan.
the class JsonInstanceSerializer method deserialize.
@SuppressWarnings({ "unchecked" })
@Override
public ServiceInstance<T> deserialize(byte[] bytes) throws Exception {
ServiceInstance rawServiceInstance = mapper.readValue(bytes, type);
// just to verify that it's the correct type
payloadClass.cast(rawServiceInstance.getPayload());
return (ServiceInstance<T>) rawServiceInstance;
}
Aggregations