Skip to content

Commit

Permalink
CAMEL-10686: service-call eip : generate service-call auto configurat…
Browse files Browse the repository at this point in the history
…ions for spring-boot
  • Loading branch information
lburgazzoli committed Feb 21, 2017
1 parent 2c3fa6c commit 6153145
Show file tree
Hide file tree
Showing 33 changed files with 869 additions and 350 deletions.
19 changes: 6 additions & 13 deletions camel-core/src/main/docs/eips/filter-eip.adoc
@@ -1,6 +1,5 @@
[[MessageFilter-MessageFilter]]
Message Filter
^^^^^^^^^^^^^^
## Filter EIP
### Message Filter

The http://www.enterpriseintegrationpatterns.com/Filter.html[Message
Filter] from the link:enterprise-integration-patterns.html[EIP patterns]
Expand Down Expand Up @@ -84,9 +83,7 @@ For further examples of this pattern in use you could look at the
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/FilterTest.java?view=markup[junit
test case]

[[MessageFilter-Usingstop]]
*Using stop*
++++++++++++
#### *Using stop*

Stop is a bit different than a message filter as it will filter out all
messages and end the route entirely (filter only applies to its child
Expand All @@ -98,9 +95,7 @@ In the example below we do not want to route messages any further that
has the word `Bye` in the message body. Notice how we prevent this in
the when predicate by using the `.stop()`.

[[MessageFilter-Knowingifwasfilteredornot]]
Knowing if link:exchange.html[Exchange] was filtered or not
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#### Knowing if link:exchange.html[Exchange] was filtered or not

*Available as of Camel 2.5*

Expand All @@ -118,13 +113,11 @@ with the value set based on the results of the last
link:message-filter.html[Message Filter] link:predicate.html[Predicate]
evaluated.

[[MessageFilter-UsingThisPattern]]
Using This Pattern
++++++++++++++++++
#### Using This Pattern

If you would like to use this EIP Pattern then please read the
link:getting-started.html[Getting Started], you may also find the
link:architecture.html[Architecture] useful particularly the description
of link:endpoint.html[Endpoint] and link:uris.html[URIs]. Then you could
try out some of the link:examples.html[Examples] first before trying
this pattern out.
this pattern out.
Expand Up @@ -26,6 +26,7 @@
import java.util.stream.Collectors;

import org.apache.camel.cloud.ServiceDefinition;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.StringHelper;

/**
Expand All @@ -52,6 +53,17 @@ public void setServers(List<String> servers) {
servers.forEach(this::addServer);
}

public void addServers(String serviceName, List<String> servers) {
for (String server : servers) {
String host = StringHelper.before(server, ":");
String port = StringHelper.after(server, ":");

if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) {
addServer(serviceName, host, Integer.valueOf(port));
}
}
}

/**
* Set the servers.
*
Expand Down Expand Up @@ -83,7 +95,9 @@ public void addServer(String serverString) {
String host = StringHelper.before(part, ":");
String port = StringHelper.after(part, ":");

addServer(service, host, Integer.valueOf(port));
if (ObjectHelper.isNotEmpty(host) && ObjectHelper.isNotEmpty(port)) {
addServer(service, host, Integer.valueOf(port));
}
}
}

Expand Down
Expand Up @@ -753,6 +753,10 @@ private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, Ser
if (answer == null && config != null) {
answer = retrieve(ServiceDiscovery.class, camelContext, config::getServiceDiscovery, config::getServiceDiscoveryRef);
}

if (answer == null) {
answer = camelContext.getRegistry().lookupByNameAndType("service-discovery", ServiceDiscovery.class);
}
if (answer == null) {
answer = findByType(camelContext, ServiceDiscovery.class);
}
Expand Down Expand Up @@ -803,6 +807,9 @@ private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCa
}
}

if (answer == null) {
answer = camelContext.getRegistry().lookupByNameAndType("service-filter", ServiceFilter.class);
}
if (answer == null) {
answer = findByType(camelContext, ServiceFilter.class);
}
Expand Down Expand Up @@ -833,6 +840,10 @@ private ServiceChooser retrieveServiceChooser(CamelContext camelContext, Service
}
}
}

if (answer == null) {
answer = camelContext.getRegistry().lookupByNameAndType("service-chooser", ServiceChooser.class);
}
if (answer == null) {
answer = findByType(camelContext, ServiceChooser.class);
}
Expand All @@ -856,6 +867,10 @@ private LoadBalancer retrieveLoadBalancer(CamelContext camelContext, ServiceCall
if (answer == null && config != null) {
answer = retrieve(LoadBalancer.class, camelContext, config::getLoadBalancer, config::getLoadBalancerRef);
}

if (answer == null) {
answer = camelContext.getRegistry().lookupByNameAndType("load-balancer", LoadBalancer.class);
}
if (answer == null) {
answer = findByType(camelContext, LoadBalancer.class);
}
Expand Down
Expand Up @@ -26,7 +26,11 @@ public class ConsulServiceDiscoveryFactory implements ServiceDiscoveryFactory {
private final ConsulConfiguration configuration;

public ConsulServiceDiscoveryFactory() {
this.configuration = new ConsulConfiguration();
this(new ConsulConfiguration());
}

public ConsulServiceDiscoveryFactory(ConsulConfiguration configuration) {
this.configuration = configuration;
}

// *************************************************************************
Expand Down
Expand Up @@ -28,6 +28,10 @@ public DnsServiceDiscoveryFactory() {
this.configuration = new DnsConfiguration();
}

public DnsServiceDiscoveryFactory(DnsConfiguration configuration) {
this.configuration = configuration;
}

// *************************************************************************
// Properties
// *************************************************************************
Expand Down
Expand Up @@ -28,7 +28,11 @@ public class EtcdServiceDiscoveryFactory implements ServiceDiscoveryFactory {
private String type;

public EtcdServiceDiscoveryFactory() {
this.configuration = new EtcdConfiguration();
this(new EtcdConfiguration());
}

public EtcdServiceDiscoveryFactory(EtcdConfiguration configuration) {
this.configuration = configuration;
}

// *************************************************************************
Expand Down
Expand Up @@ -27,7 +27,11 @@ public class KubernetesServiceDiscoveryFactory implements ServiceDiscoveryFactor
private String lookup;

public KubernetesServiceDiscoveryFactory() {
this.configuration = new KubernetesConfiguration();
this(new KubernetesConfiguration());
}

public KubernetesServiceDiscoveryFactory(KubernetesConfiguration configuration) {
this.configuration = configuration;
}

// *************************************************************************
Expand Down
Expand Up @@ -16,11 +16,19 @@
*/
package org.apache.camel.spring.cloud;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "camel.cloud")
public class CamelCloudConfigurationProperties {
private boolean enabled = true;
private LoadBalancer loadBalancer = new LoadBalancer();
private ServiceDiscovery serviceDiscovery = new ServiceDiscovery();
private ServiceFilter serviceFilter = new ServiceFilter();
private ServiceChooser serviceChooser = new ServiceChooser();

public boolean isEnabled() {
return enabled;
Expand All @@ -29,4 +37,91 @@ public boolean isEnabled() {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public LoadBalancer getLoadBalancer() {
return loadBalancer;
}

public ServiceDiscovery getServiceDiscovery() {
return serviceDiscovery;
}

public ServiceFilter getServiceFilter() {
return serviceFilter;
}

public ServiceChooser getServiceChooser() {
return serviceChooser;
}

// *****************************************
// Nested configurations
// *****************************************

public static class LoadBalancer {
private boolean enabled = true;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

public static class ServiceDiscovery {
private boolean enabled = true;
private Map<String, List<String>> services = new HashMap<>();
private String cacheTimeout;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Map<String, List<String>> getServices() {
return services;
}

public String getCacheTimeout() {
return cacheTimeout;
}

public void setCacheTimeout(String cacheTimeout) {
this.cacheTimeout = cacheTimeout;
}
}

public static class ServiceFilter {
private boolean enabled = true;
private Map<String, List<String>> blacklist = new HashMap<>();

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public Map<String, List<String>> getBlacklist() {
return blacklist;
}
}

public static class ServiceChooser {
private boolean enabled = true;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}
Expand Up @@ -17,30 +17,28 @@

package org.apache.camel.spring.cloud;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.camel.cloud.ServiceDiscovery;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class CamelCloudDiscoveryClient implements DiscoveryClient {
private final String description;
private final Map<String, List<ServiceInstance>> services;
private final ServiceDiscovery serviceDiscovery;
private ServiceInstance localInstance;

public CamelCloudDiscoveryClient(String description) {
this(description, Collections.emptyMap());
public CamelCloudDiscoveryClient(String description, ServiceDiscovery serviceDiscovery) {
this(description, null, serviceDiscovery);
}

public CamelCloudDiscoveryClient(String description, Map<String, List<ServiceInstance>> services) {
public CamelCloudDiscoveryClient(String description, ServiceInstance localServiceDiscovery,ServiceDiscovery serviceDiscovery) {
this.description = description;
this.services = new LinkedHashMap<>(services);
this.localInstance = null;
this.serviceDiscovery = serviceDiscovery;
this.localInstance = localServiceDiscovery;
}

@Override
Expand All @@ -53,31 +51,20 @@ public ServiceInstance getLocalServiceInstance() {
return this.localInstance;
}

public CamelCloudDiscoveryClient setLocalServiceInstance(ServiceInstance instance) {
this.localInstance = instance;
return this;
}

@Override
public List<ServiceInstance> getInstances(String serviceId) {
return services.get(serviceId);
return serviceDiscovery.getUpdatedListOfServices(serviceId).stream()
.map(s -> new DefaultServiceInstance(s.getName(), s.getHost(), s.getPort(), false, s.getMetadata()))
.collect(Collectors.toList());
}

@Override
public List<String> getServices() {
return new ArrayList<>(services.keySet());
}

public CamelCloudDiscoveryClient addServiceInstance(ServiceInstance instance) {
services.computeIfAbsent(instance.getServiceId(), key -> new LinkedList<>()).add(instance);
return this;
}

public CamelCloudDiscoveryClient addServiceInstance(String serviceId, String host, int port) {
return addServiceInstance(new DefaultServiceInstance(serviceId, host, port, false));
}

public CamelCloudDiscoveryClient setLocalServiceInstance(ServiceInstance instance) {
this.localInstance = instance;
return this;
}

public CamelCloudDiscoveryClient setLocalServiceInstance(String serviceId, String host, int port) {
return setLocalServiceInstance(new DefaultServiceInstance(serviceId, host, port, false));
return Collections.emptyList();
}
}

0 comments on commit 6153145

Please sign in to comment.