You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

ServiceCall EIP

Available as of Camel 2.18

The serviceCall EIP allows to call remote services in a distributed system. The service to call is looked up in a service registry of some sorts such as Kubernetes, Consul, etcd, zookeeper.
The EIP separates the configuration of the service registry from the calling of the service. 

Maven users will need to add the dependency for the service registry supported from the following:

  • camel-kubernetes
  • camel-ribbon

Each implementation has their own set of configuration.

Syntax

When calling a service you may just refer to the name of the service in the EIP as shown below:

from("direct:start")
    .serviceCall("foo")
    .to("mock:result");

And in XML DSL:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <serviceCall name="foo"/>
    <to uri="mock:result"/>
  </route>
</camelContext>

Camel will then lookup a service with the name "foo" from the chosen Camel component that integrates with the service registry. The lookup returns a set of IP:PORT paris that refer to which active servers that host the remote service. Camel will then pick a random server to use and then build a Camel uri with the chosen IP and PORT number. By default Camel uses the HTTP component, so the example above will resolve into a Camel uri that is called by a dynamic to endpoint as shown:

toD("http://IP:PORT")
 
<toD uri="http:IP:port"/>

You can also call the service using URI parameters such as beer=yes

serviceCall("foo?beer=yes")
 
<serviceCall name="foo?beer=yes")

You can also provide a context-path such as shown:

serviceCall("foo/beverage?beer=yes")
 
<serviceCall name="foo/beverage?beer=yes")

Service Name to Camel URI Examples

So as you can see above the service name is resolved as a Camel endpoint uri, and here is a few more examples (where -> shows what the Camel uri is resolved as)

serviceCall("myService") -> http://hostname:port
serviceCall("myService/foo") -> http://hostname:port/foo
serviceCall("http:myService/foo") -> http:hostname:port/foo
 
<serviceCall name="myService"/> -> http://hostname:port
<serviceCall name="myService/foo"/> -> http://hostname:port/foo
<serviceCall name="http:myService/foo"/> -> http:hostname:port/foo

If you want full control of the resolved URI you can provide an additional uri parameter where you specify the Camel uri as you want. In the uri you can use the service name which are then resolved to IP:PORT as shown:

serviceCall("myService", "http:myService.host:myService.port/foo") -> http:hostname:port/foo
serviceCall("myService", "netty4:tcp:myService?connectTimeout=1000") -> netty:tcp:hostname:port?connectTimeout=1000
 
<serviceCall name="myService" uri="http:myService.host:myService.port/foo"/> -> http:hostname:port/foo
<serviceCall name="myService" uri="netty4:tcp:myService?connectTimeout=1000"/> -> netty:tcp:hostname:port?connectTimeout=1000

In the example above we want to call a service named "myService" and we can control the resolved URI as in the 2nd parameter. Notice how in the 1st we can use serviceName.host and serviceName.port to refer to either the IP or PORT. If you just use serviceName then its resolved as IP:PORT.

Configuring Service Call

By default Camel will call the service using the HTTP component, but you can configure to use a different component such as HTTP4Netty4 HTTP as shown:

KubernetesConfigurationDefinition config = new KubernetesConfigurationDefinition();
config.setComponent("netty4-http");
 
// register the service call configuration
context.setServiceCallConfiguration(config);
 
from("direct:start")
    .serviceCall("foo")
    .to("mock:result");

.. and in XML DSL:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <kubernetesConfiguration id="kubernetes" component="netty4-http"/>
  <route>
    <from uri="direct:start"/>
    <serviceCall name="foo"/>
    <to uri="mock:result"/>
  </route>
</camelContext>

Common Configuration

These are the common configuration that each implementation is sharing.

Name

Default
Value

Description
componenthttp

Sets the default Camel component to use for calling the remote service.
By default the http component is used. You can configure this to use netty4-http, jetty, restlet or some other components of choice.
If the service is not HTTP protocol you can use other components such as mqtt, jms, amqp</tt> etc.
If the service call has been configured using an uri, then the component from the uri is used instead of this default component.

loadBalancerRef Sets a reference to a custom org.apache.camel.spi.ServiceCallLoadBalancer to use.
serverListStrategyRef Sets a reference to a custom org.apache.camel.spi.ServiceCallServerListStrategy to use.
clientProperty These properties are specific to what service call implementation are in use.
For example if using ribbon, then the client properties are define in com.netflix.client.config.CommonClientConfigKey.

 

Kubernetes Configuration

Name

Default Value

Description

lookupenvironmentWhat strategy to lookup the service. Possible values: environment, dns, client. By default enviornment is used to use environment variables. dns is for using DNS domain names. client is for use Java Client to call the kubernetes master API and query which servers are active hosting the service.
dnsDomain Sets the DNS domain to use for DNS lookup.
namespace The kubernetes namespace to use. Will by default use namespace from the ENV variable KUBERNETES_MASTER.
apiVersion Client lookup. Kubernetes API version.
 masterUrl Client lookup. The url for the Kubernets master. 
username Sets the username for authentication when using client lookup
password Sets the password for authentication when using client lookup
oauthToken Sets the OAUTH token for authentication (instead of username/password) when using client lookup
caCertData Sets the Certificate Authority data when using client lookup
caCertFile Sets the Certificate Authority data that are loaded from the file when using client lookup
clientCertData Sets the Client Certificate data when using client lookup
clientCertFile Sets the Client Certificate data that are loaded from the file when using client lookup
clientKeyAlgo Sets the Client Keystore algorithm, such as RSA when using client lookup
clientKeyData Sets the Client Keystore data when using client lookup
clientKeyFile Sets the Client Keystore data that are loaded from the file when using client lookup
clientKeyPassphrase Sets the Client Keystore passphrase when using client lookup
trustCertsfalseSets whether to turn on trust certificate check when using client lookup

Ribbon Configuration

Name

Default Value

Type

Description

    

 

   

 

Example

Below is an example route that calls a service in kubernetes with the name foo.

from("direct:start")
    .serviceCall("foo")
    .to("mock:result");

And in XML DSL:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <serviceCall name="foo"/>
    <to uri="mock:result"/>
  </route>
</camelContext>
  • No labels