Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Setting Up a Failover Scenario Using Apache Camel ZooKeeper

April 3, 2018
Chandra Shekhar Pandey
Related topics:
JavaMicroservices
Related products:
Red Hat Fuse

Share:

    In this article, we will discuss using the Apache Camel ZooKeeper component, and demonstrate how easily we can set up a fail-over scenario for Apache Camel Routes. While working in a clustered environment, situations arise where a user wants to have a backup or slave route which will become active only when the master (or the currently active) route stops working. There are different ways to achieve this: one can use Quartz to configure a master-slave setup; JGroups can also be used. In a Fabric8 environment, there is a master component which can easily be set up as a failover scenario.

    There is one more way to discuss here in detail using ZooKeeperRoutePolicy. This policy uses a common znode (zookeeper registry) path across all instances. Each instance will be registered with a unique-id in the znode path; thus there will be a list of unique-ids. Each unique-id represents one instance of a route deployed with ZooKeeperRoutePolicy route policy.

    Whichever node is at the top of the list will be considered as the master route. Other remaining nodes will be dormant and not active. Once the master route stops, one of the slaves would be elected as the master route. Then it will continue to serve requests.

    Setting Up The Project and Configuring Camel Zookeeper as the MasterSlave Route

    Now let's discuss the coding portion. I have tested this example in Red Hat JBoss Fuse 6.3.0. You can find the code base at my personal github link.

    1. Create a Maven project with following structure:

    [cpandey@cpandey CamelZookeeperMasterSlave]$ tree
    .
    ├── pom.xml
    ├── ReadMe.txt
    └── src
     └── main
      ├── java
       └── com
        └── test
         └── pckg
          └── MyRouteBuilder.java
      └── resources
       ├── log4j.properties
       └── OSGI-INF
        └── blueprint
         └── blueprint.xml

    2. The pom.xml should have following dependencies:

     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-core</artifactId>
     </dependency>
     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-blueprint</artifactId>
     </dependency>
     <dependency>
     <groupId>org.apache.camel</groupId>
     <artifactId>camel-zookeeper</artifactId>
     </dependency>

    3. A version of these artifacts are picked from the BOM version mentioned in pom.xml:

     <dependencyManagement>
     <dependencies>
     <dependency>
     <groupId>org.jboss.fuse.bom</groupId>
     <artifactId>jboss-fuse-parent</artifactId>
     <version>6.3.0.redhat-310</version>
     <type>pom</type>
     <scope>import</scope>
     </dependency>
     </dependencies>
     </dependencyManagement>

    4. Here, I have used JAVA DSL for the Camel route. Hence in blueprint.xml, I have defined bean, which refer class com.test.pckg.MyRouteBuilder where camel route is defined.

    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    
     <camelContext id="_context1" xmlns="http://camel.apache.org/schema/blueprint">
      <routeBuilder ref="myBuilder"/>
     </camelContext>
     <bean class="com.test.pckg.MyRouteBuilder" id="myBuilder"/>
    </blueprint>
    
    

    5. Following the camel route defined in MyRouteBuilder.java</code>:

    @Override
    public void configure() {
    System.out.println("......Inside MyRouteBuilder.....");
    ZooKeeperRoutePolicy policy = new ZooKeeperRoutePolicy("zookeeper:localhost:" + 2181 + "/someapp/somepolicy", 1);
    from("file:/path/to/someFolder").routePolicy(policy).id("Master").log("Body... ${body} "+new Date());
    }
     
    The tasks performed by this piece of code are:
    • Define a ZooKeeperRoutePolicy(String uri, int enabledCount) which creates znode at path /someapp/somepolicy. ZooKeeper Server is listening at localhost:2181.
    • We want to have one instance active at a time. For a Master-slave scenario, the route is configured with 1 route instance and only the first entry in the listing will start its route. However, if there is a scenario when we need two or more route instances to be up at the same time, we can set enabledCount to 2 or more.
    • We then have a file based consumer polling at path/to/someFolder where routePolicy is set to ZooKeeperRoutePolicy.  Finally, we log the body which is set with the content of the file polled or consumed.

    How to Deploy in Red Hat JBoss Fuse with a Fabric8 Environment

    1. Fabric8 uses Apache ZooKeeper (which is a highly reliable distributed coordination service) as its registry to store the cluster configuration and node registration. Hence, we don't need to setup a different ZooKeeper server. We can register znode in an existing ZooKeeper registry and use it to set up MasterSlave route.

    2. Follow the steps below to deploy this application from Karaf terminal of Red Hat JBoss Fuse:

    profile-edit --feature fabric-zookeeper-commands default
    container-create-child root abc
    container-create-child root pqr 
    profile-create testZK
    profile-edit --feature camel-zookeeper --feature camel-blueprint testZK
    profile-edit --bundle mvn:com.mycompany/camel-zookeeper-example/1.0.0-SNAPSHOT testZK
    container-add-profile abc testZK
    container-add-profile pqr testZK

    3. As per our route, at location /path/to/someFolder, create a simple text file and add some text data to it and save it.

    4. In logs for one of the nodes we can check following; which will ensure that  znode is created and that the route is active:

    2018-03-25 17:30:30,075 | WARN | masterSlaveRoute | ZookeeperProducer | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Node '/someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf' did not exist, creating it.
    2018-03-25 17:30:30,080 | INFO | masterSlaveRoute | ZooKeeperElection | 195 - org.apache.camel.camel-zookeeper - 2.17.0.redhat-630329 | Candidate node '/someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf' has been created
    2018-03-25 17:30:30,107 | INFO | masterSlaveRoute | BlueprintCamelContext | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Route: election-route-localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf started and consuming from: Endpoint[zookeeper://localhost:2181/someapp/somepolicy]
    2018-03-25 17:30:30,112 | INFO | masterSlaveRoute | Master | 191 - org.apache.camel.camel-core - 2.17.0.redhat-630329 | Body... Sun Mar 25 17:27:32 IST 2018

    4. Using the following command from Karaf terminal, we can easily identify which nodes are registered. This unique number we can be identified in logs also:

    zk:list -r /someapp/somepolicy
    #Output we get is like
    /someapp/somepolicy/localhost.localdomain-cd65c741-4252-420a-88a4-f084a933fcaf0000000013

    5. If the route or application is stopped, this znode entry will be deleted.

     How to Deploy in Red Hat JBoss Fuse in a Standalone Environment

    1. Download ZooKeepers latest release from here.  I downloaded zookeeper-3.4.11.

    2. Extract zookeeper-3.4.11.tar.gz. Now go to zookeeper-3.4.11/conf folder:

    $ vi conf/zoo.cfg
    
    tickTime = 2000
    initLimit = 10
    syncLimit = 5
    dataDir = /path/to/zookeeper/data
    clientPort = 2181

    3.  Finally, start the ZooKeeper server:

    $ bin/zkServer.sh start

    4. Now on Red Hat JBoss Fuse 6.3.0 Karaf terminal, run the following commands:

    features:install camel-zookeeper
    osgi:install -s mvn:com.mycompany/camel-zookeeper-example/1.0.0-SNAPSHOT

    5.  As per our route, at location /path/to/someFolder, create a simple text file and add some text data to it and save it. We will get similar logs (as mentioned above) for deployment in the Fabric8 environment. Here we can have multiple Red Hat JBoss Fuse standalone environments running at different nodes having the same application deployment.

    6. If deployment and testing are done, you can stop the ZooKeeper server with the following command:

    bin/zkServer.sh stop

    That's it. I hope it helps you to to set up a master-slave scenario with camel-routes. This might help you to achieve a successful failover scenario.

    Last updated: March 23, 2023

    Recent Posts

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    • Assessing AI for OpenShift operations: Advanced configurations

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue