本文整理汇总了Java中org.apache.curator.x.discovery.details.JsonInstanceSerializer类的典型用法代码示例。如果您正苦于以下问题:Java JsonInstanceSerializer类的具体用法?Java JsonInstanceSerializer怎么用?Java JsonInstanceSerializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonInstanceSerializer类属于org.apache.curator.x.discovery.details包,在下文中一共展示了JsonInstanceSerializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Override
public void start() {
try {
// 1000ms - initial amount of time to wait between retries
// 3 times - max number of times to retry
client = CuratorFrameworkFactory.newClient(getIdentifier().getConnectionString(),
new ExponentialBackoffRetry(1000, 3));
client.start();
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
cache = TreeCache.newBuilder(client, buildBasePath()).build();
serializer = new JsonInstanceSerializer<HostMetadata>(HostMetadata.class);
} catch (Exception e) {
logger.error("Service registry start error for server identifier '{}' !",
getIdentifier().getConnectionString(), e);
throw new ServiceDiscoveryException(e);
}
}
示例2: ExampleServer
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception
{
// in a real application, you'd have a convention of some kind for the URI layout
UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}");
thisInstance = ServiceInstance.<InstanceDetails>builder()
.name(serviceName)
.payload(new InstanceDetails(description))
.port((int)(65535 * Math.random())) // in a real application, you'd use a common port
.uriSpec(uriSpec)
.build();
// if you mark your payload class with @JsonRootName the provided JsonInstanceSerializer will work
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class)
.client(client)
.basePath(path)
.serializer(serializer)
.thisInstance(thisInstance)
.build();
}
示例3: Server
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
public Server(CuratorFramework client, Instance instance, String basePath) throws Exception
{
this();
UriSpec uriSpec = new UriSpec("{scheme}://"+ instance.getIpAddress() + ":{port}");
this.basePath = basePath;
this.thisInstance = ServiceInstance.<Instance>builder()
.name(instance.getServiceName())
.payload(instance)
.port(instance.getPort())
.uriSpec(uriSpec)
.build();
JsonInstanceSerializer<Instance> serializer = new JsonInstanceSerializer<Instance>(Instance.class);
this.serviceDiscovery = ServiceDiscoveryBuilder.builder(Instance.class)
.client(client)
.basePath(this.basePath)
.serializer(serializer)
.thisInstance(thisInstance)
.build();
}
示例4: ZookeeperWrapper
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
public ZookeeperWrapper(String zkAddress) throws Exception {
this.zkAddress = zkAddress;
this.curatorFramework =
CuratorFrameworkFactory.newClient(this.zkAddress, new RetryNTimes(5, 1000));
curatorFramework.start();
this.serializer = new JsonInstanceSerializer<>(Instance.class);
this.serviceDiscovery = ServiceDiscoveryBuilder.builder(Instance.class)
.basePath(this.BASE_NAME)
.serializer(serializer)
.client(curatorFramework).build();
serviceDiscovery.start();
this.servers = new ArrayList<Server>();
}
示例5: testBasic
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Test
public void testBasic() throws Exception
{
JsonInstanceSerializer<String> serializer = new JsonInstanceSerializer<String>(String.class);
ServiceInstance<String> instance = new ServiceInstance<String>("name", "id", "address", 10, 20, "payload", 0, ServiceType.DYNAMIC, new UriSpec("{a}/b/{c}"), true);
byte[] bytes = serializer.serialize(instance);
ServiceInstance<String> rhs = serializer.deserialize(bytes);
Assert.assertEquals(instance, rhs);
Assert.assertEquals(instance.getId(), rhs.getId());
Assert.assertEquals(instance.getName(), rhs.getName());
Assert.assertEquals(instance.getPayload(), rhs.getPayload());
Assert.assertEquals(instance.getAddress(), rhs.getAddress());
Assert.assertEquals(instance.getPort(), rhs.getPort());
Assert.assertEquals(instance.getSslPort(), rhs.getSslPort());
Assert.assertEquals(instance.getUriSpec(), rhs.getUriSpec());
Assert.assertEquals(instance.isEnabled(), rhs.isEnabled());
}
示例6: testWrongPayloadType
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Test
public void testWrongPayloadType() throws Exception
{
JsonInstanceSerializer<String> stringSerializer = new JsonInstanceSerializer<String>(String.class);
JsonInstanceSerializer<Double> doubleSerializer = new JsonInstanceSerializer<Double>(Double.class);
byte[] bytes = stringSerializer.serialize(new ServiceInstance<String>("name", "id", "address", 10, 20, "payload", 0, ServiceType.DYNAMIC, new UriSpec("{a}/b/{c}"), true));
try
{
doubleSerializer.deserialize(bytes);
Assert.fail();
}
catch ( ClassCastException e )
{
// correct
}
}
示例7: testNoPayload
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Test
public void testNoPayload() throws Exception
{
JsonInstanceSerializer<Void> serializer = new JsonInstanceSerializer<Void>(Void.class);
ServiceInstance<Void> instance = new ServiceInstance<Void>("name", "id", "address", 10, 20, null, 0, ServiceType.DYNAMIC, new UriSpec("{a}/b/{c}"), true);
byte[] bytes = serializer.serialize(instance);
ServiceInstance<Void> rhs = serializer.deserialize(bytes);
Assert.assertEquals(instance, rhs);
Assert.assertEquals(instance.getId(), rhs.getId());
Assert.assertEquals(instance.getName(), rhs.getName());
Assert.assertEquals(instance.getPayload(), rhs.getPayload());
Assert.assertEquals(instance.getAddress(), rhs.getAddress());
Assert.assertEquals(instance.getPort(), rhs.getPort());
Assert.assertEquals(instance.getSslPort(), rhs.getSslPort());
Assert.assertEquals(instance.getUriSpec(), rhs.getUriSpec());
Assert.assertEquals(instance.isEnabled(), rhs.isEnabled());
}
示例8: UriSpec
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Test
public void testPayloadAsList() throws Exception
{
JsonInstanceSerializer<Object> serializer = new JsonInstanceSerializer<Object>(Object.class, false);
List<String> payload = new ArrayList<String>();
payload.add("Test value 1");
payload.add("Test value 2");
ServiceInstance<Object> instance = new ServiceInstance<Object>("name", "id", "address", 10, 20, payload, 0, ServiceType.DYNAMIC, new UriSpec("{a}/b/{c}"), false);
byte[] bytes = serializer.serialize(instance);
ServiceInstance<Object> rhs = serializer.deserialize(bytes);
Assert.assertEquals(instance, rhs);
Assert.assertEquals(instance.getId(), rhs.getId());
Assert.assertEquals(instance.getName(), rhs.getName());
Assert.assertEquals(instance.getPayload(), rhs.getPayload());
Assert.assertEquals(instance.getAddress(), rhs.getAddress());
Assert.assertEquals(instance.getPort(), rhs.getPort());
Assert.assertEquals(instance.getSslPort(), rhs.getSslPort());
Assert.assertEquals(instance.getUriSpec(), rhs.getUriSpec());
Assert.assertEquals(instance.isEnabled(), rhs.isEnabled());
}
示例9: Payload
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
@Test
public void testPayloadClass() throws Exception
{
JsonInstanceSerializer<Payload> serializer = new JsonInstanceSerializer<Payload>(Payload.class);
Payload payload = new Payload();
payload.setVal("Test value");
ServiceInstance<Payload> instance = new ServiceInstance<Payload>("name", "id", "address", 10, 20, payload, 0, ServiceType.DYNAMIC, new UriSpec("{a}/b/{c}"), true);
byte[] bytes = serializer.serialize(instance);
ServiceInstance<Payload> rhs = serializer.deserialize(bytes);
Assert.assertEquals(instance, rhs);
Assert.assertEquals(instance.getId(), rhs.getId());
Assert.assertEquals(instance.getName(), rhs.getName());
Assert.assertEquals(instance.getPayload(), rhs.getPayload());
Assert.assertEquals(instance.getAddress(), rhs.getAddress());
Assert.assertEquals(instance.getPort(), rhs.getPort());
Assert.assertEquals(instance.getSslPort(), rhs.getSslPort());
Assert.assertEquals(instance.getUriSpec(), rhs.getUriSpec());
Assert.assertEquals(instance.isEnabled(), rhs.isEnabled());
}
示例10: main
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// This method is scaffolding to get the example up and running
TestingServer server = new TestingServer();
CuratorFramework client = null;
ServiceDiscovery<InstanceDetails> serviceDiscovery = null;
Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap();
try {
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
client.start();
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH).serializer(serializer).build();
serviceDiscovery.start();
processCommands(serviceDiscovery, providers, client);
} finally {
for (ServiceProvider<InstanceDetails> cache : providers.values()) {
CloseableUtils.closeQuietly(cache);
}
CloseableUtils.closeQuietly(serviceDiscovery);
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
}
}
示例11: initServiceDiscovery
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
private void initServiceDiscovery() {
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<>(MetaData.class);
discovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build();
try {
discovery.start();
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
} catch (Exception e) {
log.error("failed to start discovery for {}", basePath, e);
throw new RuntimeException("failed to start discovery for " + basePath, e);
}
}
示例12: registerServiceInstance
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
public void registerServiceInstance(String region,
String availabilityZone,
String flavor,
String serviceName,
int servicePort,
String serviceAddress,
String serviceAddressV6,
String weight) throws Exception {
String regPath = new StringBuilder().append(registrationPath).append("/").append(region).append("/")
.append(availabilityZone).append("/").append(flavor).toString();
ServiceInstance<MetaData> service = getServiceInstance(serviceName, servicePort, serviceAddress, new HashMap<String, String>() {{
put("ipv4Address", serviceAddress);
put("ipv6Address", serviceAddressV6);
if (weight != null) {
put("weight", weight);
}
}});
try {
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<>(MetaData.class);
byte[] bytes = serializer.serialize(service);
String path = ZKPaths.makePath(ZKPaths.makePath(regPath, service.getName()), service.getId());
if ( ! connector.isPathExists(path)) {
connector.createEphemeral(path);
connector.save(new String(bytes), path);
} else {
log.warn("Node " + path + " already exists");
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: getServiceDiscovery
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the discovery
* @throws Exception the exception
*/
protected static ServiceDiscovery<MetaData> getServiceDiscovery(CuratorFramework curatorFramework, String basePath)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build(); // Service Discovery
serviceDiscovery.start();
return serviceDiscovery;
}
示例14: getServiceCacheBuilder
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the ServiceCacheBuilder
* @throws Exception the exception
*/
protected static ServiceDiscovery<MetaData> getServiceCacheBuilder(CuratorFramework curatorFramework, String basePath)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = ServiceDiscoveryBuilder.builder(MetaData.class).client(curatorFramework).basePath(basePath).serializer(serializer).build(); // Service Discovery
serviceDiscovery.start();
return serviceDiscovery;
}
示例15: getServiceProvider
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; //导入依赖的package包/类
/**
* Gets the discovery.
*
* @param basePath - Registration path
* @param curatorFramework - Curator
* @return the ServiceProvider
* @throws Exception the exception
*/
protected static ServiceProvider<MetaData> getServiceProvider(CuratorFramework curatorFramework, String basePath, String serviceName)
throws Exception {
new EnsurePath(basePath).ensure(curatorFramework.getZookeeperClient());
JsonInstanceSerializer<MetaData> serializer = new JsonInstanceSerializer<MetaData>(MetaData.class); // Payload Serializer
ServiceDiscovery<MetaData> serviceDiscovery = getServiceDiscovery(curatorFramework, basePath);
ServiceProvider<MetaData> serviceProvider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).build(); // Service Provider for a particular service
serviceProvider.start();
return serviceProvider;
}