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;
}
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;
}
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"));
}
}
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();
}
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);
}
Aggregations