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
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
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.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties
,*.yaml
files, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or 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.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list 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 |
Usage
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 | - |
Examples
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 unregistering 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 invoked by which consul api is defined by the respective producer. E.g., the |
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 55 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 | |
Connect timeout for OkHttpClient. | Duration | ||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. | default | ConsistencyMode | |
The data center. | String | ||
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 | ||
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 | ||
Recursively watch, default false. | false | Boolean | |
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 | ||
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 (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 | |
Consul configuration. The option is a org.apache.camel.component.consul.ConsulConfiguration type. | ConsulConfiguration | ||
Connect timeout for OkHttpClient. | Duration | ||
The consistencyMode used for queries, default ConsistencyMode.DEFAULT. | default | ConsistencyMode | |
Reference to a org.kiwiproject.consul.Consul in the registry. The option is a org.kiwiproject.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. | 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. | Duration | ||
Deprecated 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 | |
Deprecated The time (in seconds) to live for TTL checks. Default is 1 minute. | 60 | Integer | |
Deprecated Use datacenter instead. | String | ||
Deprecated How long (in seconds) to wait to deregister a service in case of unclean shutdown. Default is 1 hour. | 3600 | Integer | |
Deprecated Should we remove all the registered services know by this registry on stop?. | true | Boolean | |
Deprecated Should we override the service host if given ?. | true | Boolean | |
Deprecated Service host. | String |