com.orbitz.consul.AgentClient Java Examples

The following examples show how to use com.orbitz.consul.AgentClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Advertiser.java    From thorntail with Apache License 2.0 6 votes vote down vote up
public void advertise(Registration registration) {
    if (this.advertisements.contains(registration)) {
        return;
    }

    AgentClient client = this.agentClientInjector.getValue();

    com.orbitz.consul.model.agent.Registration consulReg = ImmutableRegistration.builder()
            .address(registration.getAddress())
            .port(registration.getPort())
            .id(serviceId(registration))
            .name(registration.getName())
            .addTags(registration.getTags().toArray(new String[]{}))
            .check(com.orbitz.consul.model.agent.Registration.RegCheck.ttl(checkTTL))
            .build();
    client.register(consulReg);

    this.advertisements.add(registration);

    log.info("Registered service " + consulReg.getId() + ", checkTTL: " + checkTTL);
}
 
Example #2
Source File: Advertiser.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
public void advertise(Registration registration) {
    if (this.advertisements.contains(registration)) {
        return;
    }

    AgentClient client = this.agentClientInjector.getValue();

    com.orbitz.consul.model.agent.Registration consulReg = ImmutableRegistration.builder()
            .address(registration.getAddress())
            .port(registration.getPort())
            .id(serviceId(registration))
            .name(registration.getName())
            .addTags(registration.getTags().toArray(new String[]{}))
            .check(com.orbitz.consul.model.agent.Registration.RegCheck.ttl(3L))
            .build();
    client.register(consulReg);

    this.advertisements.add(registration);
}
 
Example #3
Source File: ConsulCoordinator.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void registerRemote(RemoteInstance remoteInstance) throws ServiceRegisterException {
    if (needUsingInternalAddr()) {
        remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true));
    }

    AgentClient agentClient = client.agentClient();

    this.selfAddress = remoteInstance.getAddress();
    TelemetryRelatedContext.INSTANCE.setId(selfAddress.toString());

    Registration registration = ImmutableRegistration.builder()
                                                     .id(remoteInstance.getAddress().toString())
                                                     .name(serviceName)
                                                     .address(remoteInstance.getAddress().getHost())
                                                     .port(remoteInstance.getAddress().getPort())
                                                     .check(Registration.RegCheck.grpc(remoteInstance.getAddress()
                                                                                                     .getHost() + ":" + remoteInstance
                                                         .getAddress()
                                                         .getPort(), 5)) // registers with a TTL of 5 seconds
                                                     .build();

    agentClient.register(registration);
}
 
Example #4
Source File: ConsulModule.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final Binder binder) {
    loading();

    cfgs.forEach((id, cfg) -> {
        final Consul consul = build(cfg);
        binder.bind(Consul.class).annotatedWith(Names.named(CONSUL_PREFIX + id)).toInstance(consul);
        binder.bind(AgentClient.class).annotatedWith(Names.named(CONSUL_AGENT_CLIENT_PREFIX + id)).toInstance(consul.agentClient());
        binder.bind(HealthClient.class).annotatedWith(Names.named(CONSUL_HEALTH_CLIENT_PREFIX + id)).toInstance(consul.healthClient());
        binder.bind(KeyValueClient.class).annotatedWith(Names.named(CONSUL_KEY_VALUE_CLIENT_PREFIX + id)).toInstance(consul.keyValueClient());
        binder.bind(CatalogClient.class).annotatedWith(Names.named(CONSUL_CATALOG_CLIENT_PREFIX + id)).toInstance(consul.catalogClient());
        binder.bind(StatusClient.class).annotatedWith(Names.named(CONSUL_STATUS_CLIENT_PREFIX + id)).toInstance(consul.statusClient());
        binder.bind(SessionClient.class).annotatedWith(Names.named(CONSUL_SESSION_CLIENT_PREFIX + id)).toInstance(consul.sessionClient());
        binder.bind(EventClient.class).annotatedWith(Names.named(CONSUL_EVENT_CLIENT_PREFIX + id)).toInstance(consul.eventClient());
        binder.bind(PreparedQueryClient.class).annotatedWith(Names.named(CONSUL_PREPARED_QUERY_CLIENT_PREFIX + id))
                .toInstance(consul.preparedQueryClient());
        binder.bind(CoordinateClient.class).annotatedWith(Names.named(CONSUL_COORDINATE_CLIENT_PREFIX + id))
                .toInstance(consul.coordinateClient());
        binder.bind(OperatorClient.class).annotatedWith(Names.named(CONSUL_OPERATOR_CLIENT + id)).toInstance(consul.operatorClient());
    });

}
 
Example #5
Source File: AgentActivator.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void activate(ServiceActivatorContext context) throws ServiceRegistryException {

    ServiceTarget target = context.getServiceTarget();

    ConsulService consul = new ConsulService(this.fraction.url());
    target.addService(ConsulService.SERVICE_NAME, consul)
            .install();

    HealthClientService healthClient = new HealthClientService();
    target.addService(HealthClientService.SERIVCE_NAME, healthClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, healthClient.getConsulInjector())
            .install();


    CatalogClientService catalogClient = new CatalogClientService();
    target.addService(CatalogClientService.SERVICE_NAME, catalogClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, catalogClient.getConsulInjector())
            .install();

    AgentClientService agentClient = new AgentClientService();
    target.addService(AgentClientService.SERVICE_NAME, agentClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, agentClient.getConsulInjector())
            .install();

    Advertiser advertiser = new Advertiser();
    advertiser.setCheckTTL(fraction.ttl());
    target.addService(Advertiser.SERVICE_NAME, advertiser)
            .addDependency(AgentClientService.SERVICE_NAME, AgentClient.class, advertiser.getAgentClientInjector())
            .install();

}
 
Example #6
Source File: Advertiser.java    From thorntail with Apache License 2.0 5 votes vote down vote up
public void unadvertise(String name, String address, int port) {

        AgentClient client = this.agentClientInjector.getValue();
        Registration r = new Registration("consul", name, address, port, "");

        this.advertisements
                .stream()
                .filter(e -> e.equals(r))
                .forEach(e -> {
                    String serviceId = serviceId(e);
                    log.info("Deregister service " + serviceId);
                    client.deregister(serviceId);
                });
        this.advertisements.removeIf(e -> e.equals(r));
    }
 
Example #7
Source File: ConsulTopologyConnector.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void start(StartContext startContext) throws StartException {
    ServiceTarget target = startContext.getChildTarget();

    ConsulService consul = new ConsulService(this.url);
    target.addService(ConsulService.SERVICE_NAME, consul)
            .install();

    HealthClientService healthClient = new HealthClientService();
    target.addService(HealthClientService.SERIVCE_NAME, healthClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, healthClient.getConsulInjector())
            .install();

    AgentClientService agentClient = new AgentClientService();
    target.addService(AgentClientService.SERVICE_NAME, agentClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, agentClient.getConsulInjector())
            .install();

    CatalogClientService catalogClient = new CatalogClientService();
    target.addService(CatalogClientService.SERVICE_NAME, catalogClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, catalogClient.getConsulInjector())
            .install();


    this.advertiser = new Advertiser();
    target.addService(Advertiser.SERVICE_NAME, advertiser)
            .addDependency(AgentClientService.SERVICE_NAME, AgentClient.class, advertiser.getAgentClientInjector())
            .install();

    CatalogWatcher watcher = new CatalogWatcher();
    target.addService(CatalogWatcher.SERVICE_NAME, watcher)
            .addDependency(CatalogClientService.SERVICE_NAME, CatalogClient.class, watcher.getCatalogClientInjector())
            .addDependency(HealthClientService.SERIVCE_NAME, HealthClient.class, watcher.getHealthClientInjector())
            .addDependency(TopologyManager.SERVICE_NAME, TopologyManager.class, watcher.getTopologyManagerInjector())
            .install();
}
 
Example #8
Source File: ITClusterModuleConsulProviderFunctionalTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void unregisterRemoteOfCluster() throws Exception {
    final String serviceName = "unregister_remote_cluster";
    ModuleProvider providerA = createProvider(serviceName);
    ModuleProvider providerB = createProvider(serviceName);

    Address addressA = new Address("127.0.0.6", 1006, true);
    Address addressB = new Address("127.0.0.7", 1007, true);

    RemoteInstance instanceA = new RemoteInstance(addressA);
    RemoteInstance instanceB = new RemoteInstance(addressB);

    getClusterRegister(providerA).registerRemote(instanceA);
    getClusterRegister(providerB).registerRemote(instanceB);

    List<RemoteInstance> remoteInstancesOfA = queryRemoteNodes(providerA, 2);
    validateServiceInstance(addressA, addressB, remoteInstancesOfA);

    List<RemoteInstance> remoteInstancesOfB = queryRemoteNodes(providerB, 2);
    validateServiceInstance(addressB, addressA, remoteInstancesOfB);

    // unregister A
    Consul client = Whitebox.getInternalState(providerA, "client");
    AgentClient agentClient = client.agentClient();
    agentClient.deregister(instanceA.getAddress().toString());

    // only B
    remoteInstancesOfB = queryRemoteNodes(providerB, 1, 120);
    assertEquals(1, remoteInstancesOfB.size());
    Address address = remoteInstancesOfB.get(0).getAddress();
    assertEquals(address, addressB);
    assertTrue(addressB.isSelf());
}
 
Example #9
Source File: Advertiser.java    From thorntail with Apache License 2.0 4 votes vote down vote up
public Injector<AgentClient> getAgentClientInjector() {
    return this.agentClientInjector;
}
 
Example #10
Source File: AgentClientService.java    From thorntail with Apache License 2.0 4 votes vote down vote up
@Override
public AgentClient getValue() throws IllegalStateException, IllegalArgumentException {
    return this.client;
}
 
Example #11
Source File: Advertiser.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public Injector<AgentClient> getAgentClientInjector() {
    return this.agentClientInjector;
}
 
Example #12
Source File: AgentClientService.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public AgentClient getValue() throws IllegalStateException, IllegalArgumentException {
    return this.client;
}
 
Example #13
Source File: ITClusterModuleConsulProviderFunctionalTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
private ClusterModuleConsulProvider createProvider(String serviceName, String internalComHost,
    int internalComPort) throws Exception {
    ClusterModuleConsulProvider provider = new ClusterModuleConsulProvider();

    ClusterModuleConsulConfig config = (ClusterModuleConsulConfig) provider.createConfigBeanIfAbsent();

    config.setHostPort(consulAddress);
    config.setServiceName(serviceName);

    if (!StringUtil.isEmpty(internalComHost)) {
        config.setInternalComHost(internalComHost);
    }

    if (internalComPort > 0) {
        config.setInternalComPort(internalComPort);
    }

    provider.prepare();
    provider.start();
    provider.notifyAfterCompleted();

    ConsulCoordinator consulCoordinator = (ConsulCoordinator) provider.getService(ClusterRegister.class);

    // ignore health check
    ClusterRegister register = remoteInstance -> {
        if (needUsingInternalAddr(config)) {
            remoteInstance = new RemoteInstance(new Address(config.getInternalComHost(), config.getInternalComPort(), true));
        }

        Consul client = Whitebox.getInternalState(consulCoordinator, "client");
        AgentClient agentClient = client.agentClient();
        Whitebox.setInternalState(consulCoordinator, "selfAddress", remoteInstance.getAddress());
        TelemetryRelatedContext.INSTANCE.setId(remoteInstance.getAddress().toString());
        Registration registration = ImmutableRegistration.builder()
                                                         .id(remoteInstance.getAddress().toString())
                                                         .name(serviceName)
                                                         .address(remoteInstance.getAddress().getHost())
                                                         .port(remoteInstance.getAddress().getPort())
                                                         .build();

        agentClient.register(registration);
    };

    provider.registerServiceImplementation(ClusterRegister.class, register);
    return provider;
}
 
Example #14
Source File: ConsulIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testServiceDiscovery() throws Exception {
    final AgentClient client = getConsul().agentClient();
    try {
        registrations = new ArrayList<>(3);

        for (int i = 0; i < 3; i++) {
            Registration r = ImmutableRegistration.builder()
                .id("service-" + i)
                .name("my-service")
                .address("127.0.0.1")
                .addTags("a-tag")
                .addTags("key1=value1")
                .addTags("key2=value2")
                .port(9000 + i)
                .build();

            client.register(r);
            registrations.add(r);
        }

        ConsulConfiguration configuration = new ConsulConfiguration();
        configuration.setUrl(consulUrl);
        ServiceDiscovery discovery = new ConsulServiceDiscovery(configuration);

        List<ServiceDefinition> services = discovery.getServices("my-service");
        assertNotNull(services);
        assertEquals(3, services.size());

        for (ServiceDefinition service : services) {
            assertFalse(service.getMetadata().isEmpty());
            assertTrue(service.getMetadata().containsKey("service.name"));
            assertTrue(service.getMetadata().containsKey("service.id"));
            assertTrue(service.getMetadata().containsKey("service.node"));
            assertTrue(service.getMetadata().containsKey("a-tag"));
            assertTrue(service.getMetadata().containsKey("key1"));
            assertTrue(service.getMetadata().containsKey("key2"));
        }
    } finally {
        if (registrations != null && client != null) {
            registrations.forEach(r -> client.deregister(r.getId()));
        }
    }
}