Search in sources :

Example 6 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 7 with ServiceDefinition

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

the class EtcdServiceDiscovery method getServices.

protected List<ServiceDefinition> getServices(Predicate<EtcdServiceDefinition> filter) {
    List<ServiceDefinition> servers = Collections.emptyList();
    if (isRunAllowed()) {
        try {
            final EtcdConfiguration conf = getConfiguration();
            final EtcdKeyGetRequest request = getClient().get(conf.getServicePath()).recursive();
            if (conf.hasTimeout()) {
                request.timeout(conf.getTimeout(), TimeUnit.SECONDS);
            }
            final EtcdKeysResponse response = request.send().get();
            if (Objects.nonNull(response.node) && !response.node.nodes.isEmpty()) {
                servers = response.node.nodes.stream().map(node -> node.value).filter(ObjectHelper::isNotEmpty).map(this::nodeFromString).filter(Objects::nonNull).filter(filter).sorted(EtcdServiceDefinition.COMPARATOR).collect(Collectors.toList());
            }
        } catch (Exception e) {
            throw new RuntimeCamelException(e);
        }
    }
    return servers;
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Logger(org.slf4j.Logger) EtcdClient(mousio.etcd4j.EtcdClient) Predicate(java.util.function.Predicate) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DefaultServiceDiscovery(org.apache.camel.impl.cloud.DefaultServiceDiscovery) ObjectHelper(org.apache.camel.util.ObjectHelper) Collections(java.util.Collections) EtcdHelper(org.apache.camel.component.etcd.EtcdHelper) ObjectHelper(org.apache.camel.util.ObjectHelper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) Objects(java.util.Objects) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 8 with ServiceDefinition

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

the class ConsulServiceDiscoveryTest method testServiceDiscovery.

// *************************************************************************
// Test
// *************************************************************************
@Test
public void testServiceDiscovery() throws Exception {
    ConsulConfiguration configuration = new ConsulConfiguration(null);
    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"));
    }
}
Also used : ConsulConfiguration(org.apache.camel.component.consul.ConsulConfiguration) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery) Test(org.junit.Test)

Aggregations

ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)8 Test (org.junit.Test)4 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 DefaultServiceDefinition (org.apache.camel.impl.cloud.DefaultServiceDefinition)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)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 Predicate (java.util.function.Predicate)1 EtcdClient (mousio.etcd4j.EtcdClient)1 EtcdKeyGetRequest (mousio.etcd4j.requests.EtcdKeyGetRequest)1 EtcdKeysResponse (mousio.etcd4j.responses.EtcdKeysResponse)1 ContextTestSupport (org.apache.camel.ContextTestSupport)1