Skip to content

mortenaa/camel-consul-leader

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

camel-consul-leader

Circle CI Coverage Status

About

This is an Apache Camel utility that will provide cluster wide leader election of a clustered route by using the ControlBus component to start and stop the route based on leader election in Consul.

Usage

Maven POM

Add the following to your POM:

<dependency>
  <groupId>com.github.jhberges</groupId>
  <artifactId>camel-consul-leader</artifactId>
  <version>${camel-consul-leader.version}</version>
</dependency>

Where ${camel-consul-leader.version} is a property containing a released version of this artifact.

The artifact is published via Sonatype's OSS repository manager, and is synced to Maven Central from there. Hence propagation to central is on it's terms.

Mark the route to be controlled as non-autostartup

For example:

from("direct:la-di-da")
  .routeId("routeId")
  .autoStartup(false);

Initialize in your Camel app

ConsulLeaderElector.Builder
  .forConsulHost(consulUrl)             // For example https://consul.acme.com:8500
  .usingBasicAuth(username, password)   // If using basic-auth. Optional
  .controllingRoute("routeId")          // The route id to start and stop, based on leadership
  .inCamelContext(camelContext)         // The Camel context to operate in
  .usingServiceName("service-name")     // camel-consul-leader will register the key: service/<service-name>/leader and use this for session-locking
  .usingExecutor(executor)              // Execution service to use
  .usingLockDelay(consulLockDelay)      // "lock-delay" (see https://www.consul.io/docs/internals/sessions.html for details)
  .usingTimeToLive(consulSessionTTL)    // TimeToLive for the session (in seconds)
  .usingRetryStrategy(countOfTries,     // Strategy for creating session in Consul
                      retryPeriodBase,  // Default: tries=5, period=2 (seconds), backOff=1.5
                      backOffMultiplier)
  .allowingIslandMode(flag)             // If set to `false` the component will terminate your app with `System.exit(1);`
  .withPollConfiguration(initialDelay, 
                          pollInterval) // Time before, and between polls (seconds)
  .build();                             // Registers and starts the service

Island-mode

If the configured Consul URL cannot be reached, the app will start up as "an island".

This means that the app will assume it's a leader and just start the route.

Time and polling

The following time values are hardcoded at the time of writing:

  • Consul polling interval: intervall for checking the leadership status in the KV store. Set to 5 seconds.
  • Session TTL: timeout for the session lock. Set to 8 seconds.

References

Contributing

Contributions are welcome and are preferred in form of normal pull requests.

Code format

This repo prefers TABs over SPACEs, and generally follow more or less normal Sun Java conventions :-p

About

Leader election of clustered Camel routes using Consul.io

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%