Search in sources :

Example 1 with AgentClient

use of com.orbitz.consul.AgentClient in project jim-framework by jiangmin168168.

the class ConsulRegistryService method register.

@Override
public void register(RpcURL url) {
    Consul consul = this.buildConsul(url.getRegistryHost(), url.getRegistryPort());
    AgentClient agent = consul.agentClient();
    ImmutableRegCheck check = ImmutableRegCheck.builder().tcp(url.getHost() + ":" + url.getPort()).interval(CONSUL_HEALTH_INTERVAL).build();
    ImmutableRegistration.Builder builder = ImmutableRegistration.builder();
    builder.id(CONSUL_ID).name(CONSUL_NAME).addTags(CONSUL_TAGS).address(url.getHost()).port(url.getPort()).addChecks(check);
    agent.register(builder.build());
}
Also used : ImmutableRegCheck(com.orbitz.consul.model.agent.ImmutableRegCheck) ImmutableRegistration(com.orbitz.consul.model.agent.ImmutableRegistration) AgentClient(com.orbitz.consul.AgentClient) Consul(com.orbitz.consul.Consul)

Example 2 with AgentClient

use of com.orbitz.consul.AgentClient in project wildfly-swarm by wildfly-swarm.

the class AgentActivator method activate.

@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();
    target.addService(Advertiser.SERVICE_NAME, advertiser).addDependency(AgentClientService.SERVICE_NAME, AgentClient.class, advertiser.getAgentClientInjector()).install();
}
Also used : ServiceTarget(org.jboss.msc.service.ServiceTarget) AgentClient(com.orbitz.consul.AgentClient) Consul(com.orbitz.consul.Consul)

Example 3 with AgentClient

use of com.orbitz.consul.AgentClient in project wildfly-camel by wildfly-extras.

the class ConsulIntegrationTest method testServiceDiscovery.

@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("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()));
        }
    }
}
Also used : ConsulServiceDiscovery(org.apache.camel.component.consul.cloud.ConsulServiceDiscovery) ImmutableRegistration(com.orbitz.consul.model.agent.ImmutableRegistration) Registration(com.orbitz.consul.model.agent.Registration) AgentClient(com.orbitz.consul.AgentClient) ConsulConfiguration(org.apache.camel.component.consul.ConsulConfiguration) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery) ConsulServiceDiscovery(org.apache.camel.component.consul.cloud.ConsulServiceDiscovery) Test(org.junit.Test)

Example 4 with AgentClient

use of com.orbitz.consul.AgentClient in project wildfly-swarm by wildfly-swarm.

the class Advertiser method advertise.

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);
    log.info("Registered service " + consulReg.getId());
}
Also used : AgentClient(com.orbitz.consul.AgentClient)

Example 5 with AgentClient

use of com.orbitz.consul.AgentClient in project wildfly-swarm by wildfly-swarm.

the class Advertiser method unadvertise.

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));
}
Also used : Registration(org.wildfly.swarm.topology.runtime.Registration) Service(org.jboss.msc.service.Service) StopContext(org.jboss.msc.service.StopContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) StartContext(org.jboss.msc.service.StartContext) Logger(java.util.logging.Logger) NotRegisteredException(com.orbitz.consul.NotRegisteredException) TopologyMessages(org.wildfly.swarm.topology.TopologyMessages) ImmutableRegistration(com.orbitz.consul.model.agent.ImmutableRegistration) ServiceName(org.jboss.msc.service.ServiceName) AgentClient(com.orbitz.consul.AgentClient) Collections(java.util.Collections) InjectedValue(org.jboss.msc.value.InjectedValue) Injector(org.jboss.msc.inject.Injector) StartException(org.jboss.msc.service.StartException) Registration(org.wildfly.swarm.topology.runtime.Registration) ImmutableRegistration(com.orbitz.consul.model.agent.ImmutableRegistration) AgentClient(com.orbitz.consul.AgentClient)

Aggregations

AgentClient (com.orbitz.consul.AgentClient)5 ImmutableRegistration (com.orbitz.consul.model.agent.ImmutableRegistration)3 Consul (com.orbitz.consul.Consul)2 NotRegisteredException (com.orbitz.consul.NotRegisteredException)1 ImmutableRegCheck (com.orbitz.consul.model.agent.ImmutableRegCheck)1 Registration (com.orbitz.consul.model.agent.Registration)1 Collections (java.util.Collections)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Logger (java.util.logging.Logger)1 ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)1 ServiceDiscovery (org.apache.camel.cloud.ServiceDiscovery)1 ConsulConfiguration (org.apache.camel.component.consul.ConsulConfiguration)1 ConsulServiceDiscovery (org.apache.camel.component.consul.cloud.ConsulServiceDiscovery)1 Injector (org.jboss.msc.inject.Injector)1 Service (org.jboss.msc.service.Service)1 ServiceName (org.jboss.msc.service.ServiceName)1 ServiceTarget (org.jboss.msc.service.ServiceTarget)1 StartContext (org.jboss.msc.service.StartContext)1 StartException (org.jboss.msc.service.StartException)1