Search in sources :

Example 1 with UriSpec

use of org.apache.curator.x.discovery.UriSpec in project oozie by apache.

the class FixedJsonInstanceSerializer method deserialize.

@Override
public ServiceInstance<T> deserialize(final byte[] pBytes) throws Exception {
    final ByteArrayInputStream bais = new ByteArrayInputStream(pBytes);
    final JsonNode rootNode = mMapper.readTree(bais);
    final ServiceInstanceBuilder<T> builder = ServiceInstance.builder();
    {
        final String address = getTextField(rootNode, "address");
        if (address != null) {
            builder.address(address);
        }
    }
    {
        final String id = getTextField(rootNode, "id");
        if (id != null) {
            builder.id(id);
        }
    }
    {
        final String name = getTextField(rootNode, "name");
        if (name != null) {
            builder.name(name);
        }
    }
    {
        final Integer port = getIntegerField(rootNode, "port");
        if (port != null) {
            builder.port(port);
        }
    }
    {
        final Integer sslPort = getIntegerField(rootNode, "sslPort");
        if (sslPort != null) {
            builder.sslPort(sslPort);
        }
    }
    {
        final Long registrationTimeUTC = getLongField(rootNode, "registrationTimeUTC");
        if (registrationTimeUTC != null) {
            builder.registrationTimeUTC(registrationTimeUTC);
        }
    }
    {
        final T payload = getObject(rootNode, "payload", mPayloadClass);
        if (payload != null) {
            builder.payload(payload);
        }
    }
    {
        final ServiceType serviceType = getObject(rootNode, "serviceType", ServiceType.class);
        if (serviceType != null) {
            builder.serviceType(serviceType);
        }
    }
    {
        final UriSpec uriSpec = getObject(rootNode, "uriSpec", UriSpec.class);
        if (uriSpec != null) {
            builder.uriSpec(uriSpec);
        }
    }
    return builder.build();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceType(org.apache.curator.x.discovery.ServiceType) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriSpec(org.apache.curator.x.discovery.UriSpec)

Example 2 with UriSpec

use of org.apache.curator.x.discovery.UriSpec in project microservices by pwillhan.

the class Zookeeper method register.

public void register() {
    CuratorFramework curator = CuratorFrameworkFactory.newClient(host + ":" + port, new RetryNTimes(3, 3000));
    curator.start();
    try {
        final ServiceInstance<Object> serviceInstance = ServiceInstance.builder().uriSpec(new UriSpec("{scheme}://{address}:{port}")).address(getIp()).port(getPort()).name("geolocation").build();
        final ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class).basePath("com.packt.microservices").client(curator).thisInstance(serviceInstance).build();
        serviceDiscovery.start();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                serviceDiscovery.unregisterService(serviceInstance);
            } catch (Exception e) {
                System.err.println("Error while unregistering service in Zookeeper. Details: " + e.getMessage());
                e.printStackTrace();
            }
        }));
    } catch (Exception e) {
        System.err.println("Error while registering service in Zookeeper. Details: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) UriSpec(org.apache.curator.x.discovery.UriSpec) UnknownHostException(java.net.UnknownHostException)

Aggregations

UriSpec (org.apache.curator.x.discovery.UriSpec)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnknownHostException (java.net.UnknownHostException)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 RetryNTimes (org.apache.curator.retry.RetryNTimes)1 ServiceType (org.apache.curator.x.discovery.ServiceType)1