Search in sources :

Example 1 with ServiceDefinition

use of org.apache.camel.cloud.ServiceDefinition in project camel by apache.

the class KubernetesClientServiceDiscovery method getServices.

@Override
public List<ServiceDefinition> getServices(String name) {
    LOG.debug("Discovering endpoints from namespace: {} with name: {}", getNamespace(), name);
    Endpoints endpoints = client.endpoints().inNamespace(getNamespace()).withName(name).get();
    List<ServiceDefinition> result = new ArrayList<>();
    if (endpoints != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found {} endpoints in namespace: {} for name: {} and portName: {}", endpoints.getSubsets().size(), getNamespace(), name, getPortName());
        }
        for (EndpointSubset subset : endpoints.getSubsets()) {
            if (subset.getPorts().size() == 1) {
                addServers(name, result, subset.getPorts().get(FIRST), subset);
            } else {
                final List<EndpointPort> ports = subset.getPorts();
                final int portSize = ports.size();
                EndpointPort port;
                for (int p = 0; p < portSize; p++) {
                    port = ports.get(p);
                    if (ObjectHelper.isEmpty(getPortName()) || getPortName().endsWith(port.getName())) {
                        addServers(name, result, port, subset);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Endpoints(io.fabric8.kubernetes.api.model.Endpoints) EndpointSubset(io.fabric8.kubernetes.api.model.EndpointSubset) EndpointPort(io.fabric8.kubernetes.api.model.EndpointPort) ArrayList(java.util.ArrayList) DefaultServiceDefinition(org.apache.camel.impl.cloud.DefaultServiceDefinition) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition)

Example 2 with ServiceDefinition

use of org.apache.camel.cloud.ServiceDefinition in project camel by apache.

the class DnsServiceDiscovery method getServices.

@Override
public List<ServiceDefinition> getServices(String name) {
    final Lookup lookup = cache.computeIfAbsent(name, this::createLookup);
    final Record[] records = lookup.run();
    List<ServiceDefinition> services;
    if (Objects.nonNull(records) && lookup.getResult() == Lookup.SUCCESSFUL) {
        services = Arrays.stream(records).filter(SRVRecord.class::isInstance).map(SRVRecord.class::cast).sorted(COMPARATOR).map(record -> asService(name, record)).collect(Collectors.toList());
    } else {
        services = Collections.emptyList();
    }
    return services;
}
Also used : Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) SRVRecord(org.xbill.DNS.SRVRecord) DefaultServiceDefinition(org.apache.camel.impl.cloud.DefaultServiceDefinition) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition)

Example 3 with ServiceDefinition

use of org.apache.camel.cloud.ServiceDefinition in project camel by apache.

the class DnsServiceDiscoveryTest method testServiceDiscovery.

@Test
public void testServiceDiscovery() throws Exception {
    DnsConfiguration configuration = new DnsConfiguration();
    DnsServiceDiscovery discovery = new DnsServiceDiscovery(configuration);
    configuration.setDomain("gmail.com");
    configuration.setProto("_tcp");
    List<ServiceDefinition> services = discovery.getServices("_xmpp-server");
    assertNotNull(services);
    assertFalse(services.isEmpty());
    for (ServiceDefinition service : services) {
        assertFalse(service.getMetadata().isEmpty());
        assertNotNull(service.getMetadata().get("priority"));
        assertNotNull(service.getMetadata().get("weight"));
    }
}
Also used : DnsConfiguration(org.apache.camel.component.dns.DnsConfiguration) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) Test(org.junit.Test)

Example 4 with ServiceDefinition

use of org.apache.camel.cloud.ServiceDefinition in project camel by apache.

the class EtcdServiceDiscoveryTest method testOnDemandDiscovery.

@Test
public void testOnDemandDiscovery() throws Exception {
    for (int i = 0; i < 3; i++) {
        addServer(client, "serviceType-1");
    }
    for (int i = 0; i < 2; i++) {
        addServer(client, "serviceType-2");
    }
    EtcdOnDemandServiceDiscovery strategy = new EtcdOnDemandServiceDiscovery(CONFIGURATION);
    strategy.start();
    List<ServiceDefinition> type1 = strategy.getServices("serviceType-1");
    assertEquals(3, type1.size());
    for (ServiceDefinition service : type1) {
        assertNotNull(service.getMetadata());
        assertTrue(service.getMetadata().containsKey("service_name"));
        assertTrue(service.getMetadata().containsKey("port_delta"));
    }
    List<ServiceDefinition> type2 = strategy.getServices("serviceType-2");
    assertEquals(2, type2.size());
    for (ServiceDefinition service : type2) {
        assertNotNull(service.getMetadata());
        assertTrue(service.getMetadata().containsKey("service_name"));
        assertTrue(service.getMetadata().containsKey("port_delta"));
    }
    strategy.stop();
}
Also used : ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) Test(org.junit.Test)

Example 5 with ServiceDefinition

use of org.apache.camel.cloud.ServiceDefinition in project camel by apache.

the class DefaultLoadBalancer method process.

// *************************************
// Load Balancer
// *************************************
@Override
public <T> T process(String serviceName, LoadBalancerFunction<T> function) throws Exception {
    ServiceDefinition service;
    List<ServiceDefinition> services = serviceDiscovery.getServices(serviceName);
    if (services == null || services.isEmpty()) {
        throw new RejectedExecutionException("No active services with name " + serviceName);
    } else {
        // filter services
        services = serviceFilter.apply(services);
        // let the client service chooser find which server to use
        service = services.size() > 1 ? serviceChooser.choose(services) : services.get(0);
        if (service == null) {
            throw new RejectedExecutionException("No active services with name " + serviceName);
        }
    }
    return function.apply(service);
}
Also used : ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)9 Test (org.junit.Test)5 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ServiceDiscovery (org.apache.camel.cloud.ServiceDiscovery)2 ConsulConfiguration (org.apache.camel.component.consul.ConsulConfiguration)2 DefaultServiceDefinition (org.apache.camel.impl.cloud.DefaultServiceDefinition)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AgentClient (com.orbitz.consul.AgentClient)1 ImmutableRegistration (com.orbitz.consul.model.agent.ImmutableRegistration)1 Registration (com.orbitz.consul.model.agent.Registration)1 EndpointPort (io.fabric8.kubernetes.api.model.EndpointPort)1 EndpointSubset (io.fabric8.kubernetes.api.model.EndpointSubset)1 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1