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 as a type safe way of configuring endpoints.
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 29 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Connect timeout for OkHttpClient. |
Duration |
||
Deprecated Connect timeout for OkHttpClient. Deprecation note: Use connectTimeout instead. |
Long |
||
Reference to a com.orbitz.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 |
||
Deprecated Read timeout for OkHttpClient. Deprecation note: Use readTimeout instead. |
Long |
||
Set tags. You can separate multiple tags by comma. |
String |
||
The Consul agent URL. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Deprecated Write timeout for OkHttpClient. Deprecation note: Use writeTimeout instead. |
Long |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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 (28 parameters)
Name | Description | Default | Type |
---|---|---|---|
Connect timeout for OkHttpClient. |
Duration |
||
Deprecated Connect timeout for OkHttpClient. Deprecation note: Use connectTimeout instead. |
Long |
||
Reference to a com.orbitz.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 |
||
Deprecated Read timeout for OkHttpClient. Deprecation note: Use readTimeout instead. |
Long |
||
Set tags. You can separate multiple tags by comma. |
String |
||
The Consul agent URL. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Deprecated Write timeout for OkHttpClient. Deprecation note: Use writeTimeout instead. |
Long |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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 |
||
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 |
|
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())
...
Spring Boot Auto-Configuration
When using consul with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-consul-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 90 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Sets the ACL token to be used with Consul. |
String |
||
Custom service attributes. |
Map |
||
The time (in seconds) to wait for a watch event, default 10 seconds. |
10 |
Integer |
|
How often (in seconds) a service has to be marked as healthy if its check is TTL or how often the check should run. Default is 5 seconds. |
5 |
Integer |
|
The time (in seconds) to live for TTL checks. Default is 1 minute. |
60 |
Integer |
|
Connect timeout for OkHttpClient. |
Duration |
||
Use connectTimeout instead. |
Long |
||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. |
ConsistencyMode |
||
The data center. |
String |
||
How long (in seconds) to wait to deregister a service in case of unclean shutdown. Default is 1 hour. |
3600 |
Integer |
|
Should we remove all the registered services know by this registry on stop?. |
true |
Boolean |
|
Sets if the consul service registry should be enabled or not, default is false. |
false |
Boolean |
|
The first index for watch for, default 0. |
0 |
BigInteger |
|
Service Registry ID. |
String |
||
The near node to use for queries. |
String |
||
The note meta-data to use for queries. |
List |
||
Service lookup order/priority. |
Integer |
||
Should we override the service host if given ?. |
true |
Boolean |
|
Sets the password to be used for basic authentication. |
String |
||
Configure if the AgentClient should attempt a ping before returning the Consul instance. |
true |
Boolean |
|
Read timeout for OkHttpClient. |
Duration |
||
Use readTimeout instead. |
Long |
||
Recursively watch, default false. |
false |
Boolean |
|
Service host. |
String |
||
SSL configuration using an org.apache.camel.support.jsse.SSLContextParameters instance. |
SSLContextParameters |
||
Set tags. You can separate multiple tags by comma. |
Set |
||
The Consul agent URL. |
String |
||
Sets the username to be used for basic authentication. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Use writeTimeout instead. |
Long |
||
Sets the ACL token to be used with Consul. |
String |
||
Custom service attributes. |
Map |
||
The time (in seconds) to wait for a watch event, default 10 seconds. |
10 |
Integer |
|
Connect timeout for OkHttpClient. |
Duration |
||
Use connectTimeout instead. |
Long |
||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. |
ConsistencyMode |
||
The data center. |
String |
||
Sets if the consul cluster service should be enabled or not, default is false. |
false |
Boolean |
|
The first index for watch for, default 0. |
0 |
BigInteger |
|
Cluster Service ID. |
String |
||
The near node to use for queries. |
String |
||
The note meta-data to use for queries. |
List |
||
Service lookup order/priority. |
Integer |
||
Sets the password to be used for basic authentication. |
String |
||
Configure if the AgentClient should attempt a ping before returning the Consul instance. |
true |
Boolean |
|
Read timeout for OkHttpClient. |
Duration |
||
Use readTimeout instead. |
Long |
||
Recursively watch, default false. |
false |
Boolean |
|
Consul root path. |
/camel |
String |
|
The value for lockDelay. |
5 |
Integer |
|
The value of wait attribute. |
5 |
Integer |
|
The value of TTL. |
60 |
Integer |
|
SSL configuration using an org.apache.camel.support.jsse.SSLContextParameters instance. |
SSLContextParameters |
||
Set tags. You can separate multiple tags by comma. |
Set |
||
The Consul agent URL. |
String |
||
Sets the username to be used for basic authentication. |
String |
||
Write timeout for OkHttpClient. |
Duration |
||
Use writeTimeout instead. |
Long |
||
Sets the ACL token to be used with Consul. |
String |
||
The default action. Can be overridden by CamelConsulAction. |
String |
||
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 |
|
The second to wait for a watch event, default 10 seconds. |
10 |
Integer |
|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. 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 |
|
Consul configuration. The option is a org.apache.camel.component.consul.ConsulConfiguration type. |
ConsulConfiguration |
||
Connect timeout for OkHttpClient. The option is a java.time.Duration type. |
Duration |
||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. |
ConsistencyMode |
||
Reference to a com.orbitz.consul.Consul in the registry. The option is a com.orbitz.consul.Consul type. |
Consul |
||
The data center. |
String |
||
Whether to enable auto configuration of the consul component. This is enabled by default. |
Boolean |
||
The first index for watch for, default 0. The option is a java.math.BigInteger type. |
BigInteger |
||
The default key. Can be overridden by CamelConsulKey. |
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 |
|
The near node to use for queries. |
String |
||
The note meta-data to use for queries. |
List |
||
Sets the password to be used for basic authentication. |
String |
||
Configure if the AgentClient should attempt a ping before returning the Consul instance. |
true |
Boolean |
|
Read timeout for OkHttpClient. The option is a java.time.Duration type. |
Duration |
||
Recursively watch, default false. |
false |
Boolean |
|
SSL configuration using an org.apache.camel.support.jsse.SSLContextParameters instance. The option is a org.apache.camel.support.jsse.SSLContextParameters type. |
SSLContextParameters |
||
Set tags. You can separate multiple tags by comma. |
String |
||
The Consul agent URL. |
String |
||
Enable usage of global SSL context parameters. |
false |
Boolean |
|
Sets the username to be used for basic authentication. |
String |
||
Default to transform values retrieved from Consul i.e. on KV endpoint to string. |
false |
Boolean |
|
Write timeout for OkHttpClient. The option is a java.time.Duration type. |
Duration |
||
Deprecated Use datacenter instead. |
String |
||
Deprecated Use datacenter instead. |
String |
||
Deprecated Connect timeout for OkHttpClient. Deprecation note: Use connectTimeout instead. |
Long |
||
Deprecated Read timeout for OkHttpClient. Deprecation note: Use readTimeout instead. |
Long |
||
Deprecated Write timeout for OkHttpClient. Deprecation note: Use writeTimeout instead. The option is a java.lang.Long type. |
Long |