Consul
Since Camel 2.18
Both producer and consumer are supported
The Consul component is a component for integrating your application with Hashicorp Consul.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-consul</artifactId>
<version>${camel-version}</version>
</dependency>
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.
Configuring Endpoint Options
Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.
The following two sections lists all the options, firstly for the component followed by the endpoint.
Component Options
The Consul component supports 26 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Connect timeout for OkHttpClient. |
Duration |
||
Reference to a org.kiwiproject.consul.Consul in the registry. |
Consul |
||
The default key. Can be overridden by CamelConsulKey. |
String |
||
Configure if the AgentClient should attempt a ping before returning the Consul instance. |
true |
boolean |
|
Read timeout for OkHttpClient. |
Duration |
||
Set tags. You can separate multiple tags by comma. |
String |
||
The Consul agent URL. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
The default action. Can be overridden by CamelConsulAction. |
String |
||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
Default to transform values retrieved from Consul i.e. on KV endpoint to string. |
false |
boolean |
|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
|
Consul configuration. |
ConsulConfiguration |
||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. Enum values:
|
DEFAULT |
ConsistencyMode |
|
The data center. |
String |
||
The near node to use for queries. |
String |
||
The note meta-data to use for queries. |
List |
||
Sets the ACL token to be used with Consul. |
String |
||
Sets the password to be used for basic authentication. |
String |
||
SSL configuration using an org.apache.camel.support.jsse.SSLContextParameters instance. |
SSLContextParameters |
||
Enable usage of global SSL context parameters. |
false |
boolean |
|
Sets the username to be used for basic authentication. |
String |
||
The second to wait for a watch event, default 10 seconds. |
10 |
Integer |
|
The first index for watch for, default 0. |
0 |
BigInteger |
|
Recursively watch, default false. |
false |
boolean |
Endpoint Options
The Consul endpoint is configured using URI syntax:
consul:apiEndpoint
with the following path and query parameters:
Query Parameters (25 parameters)
Name | Description | Default | Type |
---|---|---|---|
Connect timeout for OkHttpClient. |
Duration |
||
Reference to a org.kiwiproject.consul.Consul in the registry. |
Consul |
||
The default key. Can be overridden by CamelConsulKey. |
String |
||
Configure if the AgentClient should attempt a ping before returning the Consul instance. |
true |
boolean |
|
Read timeout for OkHttpClient. |
Duration |
||
Set tags. You can separate multiple tags by comma. |
String |
||
The Consul agent URL. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
ExceptionHandler |
||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
|
ExchangePattern |
||
The default action. Can be overridden by CamelConsulAction. |
String |
||
Default to transform values retrieved from Consul i.e. on KV endpoint to string. |
false |
boolean |
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. Enum values:
|
DEFAULT |
ConsistencyMode |
|
The data center. |
String |
||
The near node to use for queries. |
String |
||
The note meta-data to use for queries. |
List |
||
Sets the ACL token to be used with Consul. |
String |
||
Sets the password to be used for basic authentication. |
String |
||
SSL configuration using an org.apache.camel.support.jsse.SSLContextParameters instance. |
SSLContextParameters |
||
Sets the username to be used for basic authentication. |
String |
||
The second to wait for a watch event, default 10 seconds. |
10 |
Integer |
|
The first index for watch for, default 0. |
0 |
BigInteger |
|
Recursively watch, default false. |
false |
boolean |
Message Headers
The Consul component supports 31 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
Constant: |
The Producer action. |
String |
|
Constant: |
The Key on which the action should applied. |
String |
|
Constant: |
The event id. |
String |
|
CamelConsulEventName (consumer) Constant: |
The event name. |
String |
|
CamelConsulEventLTime (consumer) Constant: |
The event LTime. |
Long |
|
CamelConsulNodeFilter (consumer) Constant: |
The Node filter. |
String |
|
CamelConsulTagFilter (consumer) Constant: |
The tag filter. |
String |
|
CamelConsulSessionFilter (consumer) Constant: |
The session filter. |
String |
|
Constant: |
The data version. |
Integer |
|
Constant: |
Flags associated with a value. |
Long |
|
Constant: |
The optional value index. |
BigInteger |
|
Constant: |
The optional value wait. |
String |
|
CamelConsulCreateIndex (consumer) Constant: |
The internal index value that represents when the entry was created. |
Long |
|
CamelConsulLockIndex (consumer) Constant: |
The number of times this key has successfully been acquired in a lock. |
Long |
|
CamelConsulModifyIndex (consumer) Constant: |
The last index that modified this key. |
Long |
|
Constant: |
Options associated to the request. |
||
Constant: |
true if the response has a result. |
Boolean |
|
Constant: |
The session id. |
String |
|
CamelConsulValueAsString (producer) Constant: |
To transform values retrieved from Consul i.e. on KV endpoint to string. |
Boolean |
|
Constant: |
The node. |
String |
|
Constant: |
The service. |
String |
|
CamelConsulDatacenter (producer) Constant: |
The data center. |
String |
|
CamelConsulNearNode (producer) Constant: |
The near node to use for queries. |
String |
|
CamelConsulNodeMeta (producer) Constant: |
The note meta-data to use for queries. |
List |
|
CamelConsulLastContact (producer) Constant: |
The last contact. |
Long |
|
CamelConsulKnownLeader (producer) Constant: |
Indicates whether it is the known leader. |
Boolean |
|
CamelConsulConsistencyMode (producer) Constant: |
The consistencyMode used for queries. |
DEFAULT |
ConsistencyMode |
CamelConsulHealthyOnly (producer) Constant: |
Only on healthy services. |
false |
Boolean |
CamelConsulHealthyState (producer) Constant: |
The state to query. Enum values:
|
State |
|
CamelConsulPreparedQueryID (producer) Constant: |
The id of the prepared query. |
String |
|
CamelConsulServiceId (producer) Constant: |
The service id for agent deregistration. |
String |
Api Endpoint
The apiEndpoint
denotes the type of consul api which should be addressed.
Domain | Producer | Consumer |
---|---|---|
kv |
ConsulKeyValueProducer |
ConsulKeyValueConsumer |
event |
ConsulEventProducer |
ConsulEventConsumer |
agent |
ConsulAgentProducer |
- |
coordinates |
ConsulCoordinatesProducer |
- |
health |
ConsulHealthProducer |
- |
status |
ConsulStatusProducer |
- |
preparedQuery |
ConsulPreparedQueryProducer |
- |
catalog |
ConsulCatalogProducer |
- |
session |
ConsulSessionProducer |
- |
Producer Examples
As an example, we will show how to use the ConsulAgentProducer
to register a service by means of the Consul agent api.
Registering and deregistering are examples for possible actions against the Consul agent api.
The desired action can be defined by setting the header ConsulConstants.CONSUL_ACTION
to a value from the ConsulXXXActions
interface of the respective Consul api. E.g. ConsulAgentActions
contains the actions for the agent api.
If you set CONSUL_ACTION
to ConsulAgentActions.REGISTER
, the agent action REGISTER
will be executed.
Which producer action invokes which consul api is defined by the respective producer.
E.g. the ConsulAgentProducer maps ConsulAgentActions.REGISTER to an invocation of AgentClient.register .
|
from("direct:registerFooService")
.setBody().constant(ImmutableRegistration.builder()
.id("foo-1")
.name("foo")
.address("localhost")
.port(80)
.build())
.setHeader(ConsulConstants.CONSUL_ACTION, constant(ConsulAgentActions.REGISTER))
.to("consul:agent");
It is also possible to set a default action on the consul endpoint and do without the header:
consul:agent?action=REGISTER
Registering Camel Routes with Consul
You can employ a ServiceRegistrationRoutePolicy
to register Camel routes as services with Consul automatically.
from("jetty:http://0.0.0.0:8080/service/endpoint").routeId("foo-1")
.routeProperty(ServiceDefinition.SERVICE_META_ID, "foo-1")
.routeProperty(ServiceDefinition.SERVICE_META_NAME, "foo")
.routePolicy(new ServiceRegistrationRoutePolicy())
...