Search in sources :

Example 1 with JsonInstanceSerializer

use of org.apache.curator.x.discovery.details.JsonInstanceSerializer in project xian by happyyangyuan.

the class DiscoveryExample method main.

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);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ServiceProvider(org.apache.curator.x.discovery.ServiceProvider)

Example 2 with JsonInstanceSerializer

use of org.apache.curator.x.discovery.details.JsonInstanceSerializer in project BRFS by zhangnianli.

the class DiscoveryExample method main.

public static void main(String[] args) throws Exception {
    // This method is scaffolding to get the example up and running
    CuratorFramework client = null;
    ServiceDiscovery<InstanceDetails> serviceDiscovery = null;
    Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap();
    try {
        client = CuratorFrameworkFactory.newClient("192.168.101.86:2181", 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);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ServiceProvider(org.apache.curator.x.discovery.ServiceProvider)

Example 3 with JsonInstanceSerializer

use of org.apache.curator.x.discovery.details.JsonInstanceSerializer in project metron by apache.

the class Runner method main.

public static void main(String... argv) throws Exception {
    CommandLine cli = RunnerOptions.parse(new PosixParser(), argv);
    String zkQuorum = RunnerOptions.ZK_QUORUM.get(cli);
    String zkRoot = RunnerOptions.ZK_ROOT.get(cli);
    String script = RunnerOptions.SCRIPT.get(cli);
    String name = RunnerOptions.NAME.get(cli);
    String version = RunnerOptions.VERSION.get(cli);
    String containerId = RunnerOptions.CONTAINER_ID.get(cli);
    String hostname = RunnerOptions.HOSTNAME.get(cli);
    CuratorFramework client = null;
    LOG.info("Running script " + script);
    LOG.info("Local Directory Contents");
    for (File f : new File(".").listFiles()) {
        LOG.info("  " + f.getName());
    }
    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.newClient(zkQuorum, retryPolicy);
        client.start();
        MaaSConfig config = ConfigUtil.INSTANCE.read(client, zkRoot, new MaaSConfig(), MaaSConfig.class);
        JsonInstanceSerializer<ModelEndpoint> serializer = new JsonInstanceSerializer<>(ModelEndpoint.class);
        try {
            serviceDiscovery = ServiceDiscoveryBuilder.builder(ModelEndpoint.class).client(client).basePath(config.getServiceRoot()).serializer(serializer).build();
        } finally {
        }
        LOG.info("Created service @ " + config.getServiceRoot());
        serviceDiscovery.start();
        File cwd = new File(script).getParentFile();
        File scriptFile = new File(cwd, script);
        if (scriptFile.exists() && !scriptFile.canExecute()) {
            scriptFile.setExecutable(true);
        }
        final String cmd = scriptFile.getAbsolutePath();
        try {
            p = new ProcessBuilder(cmd).directory(cwd).start();
        } catch (Exception e) {
            LOG.info("Unable to execute " + cmd + " from " + new File(".").getAbsolutePath());
            LOG.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage(), e);
        }
        try {
            LOG.info("Started " + cmd);
            Endpoint ep = readEndpoint(cwd);
            URL endpointUrl = correctLocalUrl(hostname, ep.getUrl());
            ep.setUrl(endpointUrl.toString());
            LOG.info("Read endpoint " + ep);
            ModelEndpoint endpoint = new ModelEndpoint();
            {
                endpoint.setName(name);
                endpoint.setContainerId(containerId);
                endpoint.setEndpoint(ep);
                endpoint.setVersion(version);
            }
            ;
            ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id(containerId).name(name).port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
            final ServiceInstance<ModelEndpoint> instance = builder.build();
            try {
                LOG.info("Installing service instance: " + instance + " at " + serviceDiscovery);
                serviceDiscovery.registerService(instance);
                LOG.info("Installed instance " + name + ":" + version + "@" + endpointUrl);
            } catch (Throwable t) {
                LOG.error("Unable to install instance " + name + ":" + version + "@" + endpointUrl, t);
            }
            Runtime.getRuntime().addShutdownHook(new Thread() {

                @Override
                public void run() {
                    LOG.info("KILLING CONTAINER PROCESS...");
                    if (p != null) {
                        LOG.info("Process destroyed forcibly");
                        p.destroyForcibly();
                    }
                }
            });
        } finally {
            if (p.waitFor() != 0) {
                String stderr = Joiner.on("\n").join(IOUtils.readLines(p.getErrorStream()));
                String stdout = Joiner.on("\n").join(IOUtils.readLines(p.getInputStream()));
                throw new IllegalStateException("Unable to execute " + script + ".  Stderr is: " + stderr + "\nStdout is: " + stdout);
            }
        }
    } finally {
        if (serviceDiscovery != null) {
            CloseableUtils.closeQuietly(serviceDiscovery);
        }
        if (client != null) {
            CloseableUtils.closeQuietly(client);
        }
    }
}
Also used : ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) MaaSConfig(org.apache.metron.maas.config.MaaSConfig) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) CuratorFramework(org.apache.curator.framework.CuratorFramework) Endpoint(org.apache.metron.maas.config.Endpoint) ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) File(java.io.File) RetryPolicy(org.apache.curator.RetryPolicy)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)3 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)3 JsonInstanceSerializer (org.apache.curator.x.discovery.details.JsonInstanceSerializer)3 ServiceProvider (org.apache.curator.x.discovery.ServiceProvider)2 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RetryPolicy (org.apache.curator.RetryPolicy)1 TestingServer (org.apache.curator.test.TestingServer)1 Endpoint (org.apache.metron.maas.config.Endpoint)1 MaaSConfig (org.apache.metron.maas.config.MaaSConfig)1 ModelEndpoint (org.apache.metron.maas.config.ModelEndpoint)1