Search in sources :

Example 1 with ServiceDiscovery

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

the class ServiceCallDefinition method createProcessor.

// *****************************
// Processor Factory
// *****************************
@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    final CamelContext camelContext = routeContext.getCamelContext();
    final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext);
    final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext);
    final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext);
    final LoadBalancer loadBalancer = retrieveLoadBalancer(camelContext);
    final Expression expression = retrieveExpression(camelContext);
    if (loadBalancer instanceof CamelContextAware) {
        ((CamelContextAware) loadBalancer).setCamelContext(camelContext);
    }
    if (loadBalancer instanceof ServiceDiscoveryAware) {
        ((ServiceDiscoveryAware) loadBalancer).setServiceDiscovery(serviceDiscovery);
    }
    if (loadBalancer instanceof ServiceFilterAware) {
        ((ServiceFilterAware) loadBalancer).setServiceFilter(serviceFilter);
    }
    if (loadBalancer instanceof ServiceChooserAware) {
        ((ServiceChooserAware) loadBalancer).setServiceChooser(serviceChooser);
    }
    // The component is used to configure the default scheme to use (eg camel component name).
    // The component configured on EIP takes precedence vs configured on configuration.
    String component = this.component;
    if (component == null) {
        ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext);
        if (conf != null) {
            component = conf.getComponent();
        }
    }
    if (component == null) {
        ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext);
        if (conf != null) {
            component = conf.getComponent();
        }
    }
    return new DefaultServiceCallProcessor(camelContext, name, component, uri, pattern, loadBalancer, expression);
}
Also used : CamelContext(org.apache.camel.CamelContext) PassThroughServiceFilter(org.apache.camel.impl.cloud.PassThroughServiceFilter) ServiceFilter(org.apache.camel.cloud.ServiceFilter) HealthyServiceFilter(org.apache.camel.impl.cloud.HealthyServiceFilter) CamelContextAware(org.apache.camel.CamelContextAware) ServiceDiscoveryAware(org.apache.camel.cloud.ServiceDiscoveryAware) ServiceChooserAware(org.apache.camel.cloud.ServiceChooserAware) DefaultServiceCallProcessor(org.apache.camel.impl.cloud.DefaultServiceCallProcessor) DefaultLoadBalancer(org.apache.camel.impl.cloud.DefaultLoadBalancer) LoadBalancer(org.apache.camel.cloud.LoadBalancer) ServiceFilterAware(org.apache.camel.cloud.ServiceFilterAware) ServiceChooser(org.apache.camel.cloud.ServiceChooser) RoundRobinServiceChooser(org.apache.camel.impl.cloud.RoundRobinServiceChooser) RandomServiceChooser(org.apache.camel.impl.cloud.RandomServiceChooser) DefaultServiceCallExpression(org.apache.camel.impl.cloud.DefaultServiceCallExpression) Expression(org.apache.camel.Expression) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery)

Example 2 with ServiceDiscovery

use of org.apache.camel.cloud.ServiceDiscovery 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)

Example 3 with ServiceDiscovery

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

the class ServiceCallDefinition method retrieveServiceDiscovery.

// ******************************************
// ServiceDiscovery
// ******************************************
private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception {
    ServiceDiscovery answer = null;
    ServiceCallConfigurationDefinition config = function.apply(camelContext);
    if (config != null) {
        if (config.getServiceDiscoveryConfiguration() != null) {
            answer = config.getServiceDiscoveryConfiguration().newInstance(camelContext);
        } else {
            answer = retrieve(ServiceDiscovery.class, camelContext, config::getServiceDiscovery, config::getServiceDiscoveryRef);
        }
    }
    return answer;
}
Also used : ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery)

Example 4 with ServiceDiscovery

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

the class ServiceCallServiceDiscoveryConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public ServiceDiscovery newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "ServiceDiscovery factoryKey");
    ServiceDiscovery answer;
    // First try to find the factory from the registry.
    ServiceDiscoveryFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceDiscoveryFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (ServiceDiscoveryFactory.class.isAssignableFrom(type)) {
                factory = (ServiceDiscoveryFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving ServiceDiscovery: " + factoryKey + " detected type conflict: Not a ServiceDiscoveryFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : ServiceDiscoveryFactory(org.apache.camel.cloud.ServiceDiscoveryFactory) HashMap(java.util.HashMap) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery)

Aggregations

ServiceDiscovery (org.apache.camel.cloud.ServiceDiscovery)4 HashMap (java.util.HashMap)1 CamelContext (org.apache.camel.CamelContext)1 CamelContextAware (org.apache.camel.CamelContextAware)1 Expression (org.apache.camel.Expression)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 LoadBalancer (org.apache.camel.cloud.LoadBalancer)1 ServiceChooser (org.apache.camel.cloud.ServiceChooser)1 ServiceChooserAware (org.apache.camel.cloud.ServiceChooserAware)1 ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)1 ServiceDiscoveryAware (org.apache.camel.cloud.ServiceDiscoveryAware)1 ServiceDiscoveryFactory (org.apache.camel.cloud.ServiceDiscoveryFactory)1 ServiceFilter (org.apache.camel.cloud.ServiceFilter)1 ServiceFilterAware (org.apache.camel.cloud.ServiceFilterAware)1 ConsulConfiguration (org.apache.camel.component.consul.ConsulConfiguration)1 DefaultLoadBalancer (org.apache.camel.impl.cloud.DefaultLoadBalancer)1 DefaultServiceCallExpression (org.apache.camel.impl.cloud.DefaultServiceCallExpression)1 DefaultServiceCallProcessor (org.apache.camel.impl.cloud.DefaultServiceCallProcessor)1 HealthyServiceFilter (org.apache.camel.impl.cloud.HealthyServiceFilter)1 PassThroughServiceFilter (org.apache.camel.impl.cloud.PassThroughServiceFilter)1