Search code examples
spring-integrationapache-zookeeperconsulleader-election

Does Spring Integration have any features for leader election with respect to IntegrationFlow "ownership"?


I've been learning Spring Integration for about a month now, and it suddenly dawned on me: what if you have the requirement to only have one instance of an IntegrationFlow at any point in time, but you have multiple instances of the Spring application running in the same environment?

Meaning, I might have, say, 3 nodes/instances of my application running in the dev environment, and all 3 have @Configuration source files defining the same IntegrationFlow bean. But I only want one of the 3 to be running the flow at a time. This sounds like a problem solved in the leader election space. I'm tempted to use something like Consul or even ZK but figured I would reach out to the community in case SI or even Spring in general can offer an easier solution that doesn't come with all the operational overhead and infrastructure of running those consensus systems.

Thanks in advance for any-and-all steering!


Solution

  • See the documentation:

    https://docs.spring.io/spring-integration/docs/current/reference/html/endpoint.html#endpoint-roles

    Starting with version 4.2, endpoints can be assigned to roles. Roles let endpoints be started and stopped as a group. This is particularly useful when using leadership election, where a set of endpoints can be started or stopped when leadership is granted or revoked, respectively. For this purpose the framework registers a SmartLifecycleRoleController bean in the application context with the name IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER.

    and

    https://docs.spring.io/spring-integration/docs/current/reference/html/endpoint.html#leadership-event-handling

    Groups of endpoints can be started and stopped based on leadership being granted or revoked, respectively. This is useful in clustered scenarios where shared resources must be consumed by only a single instance. An example of this is a file inbound channel adapter that is polling a shared directory. ...

    Currently, Zookeeper and Hazelcast are supported.

    https://docs.spring.io/spring-integration/docs/current/reference/html/zookeeper.html#zk-leadership

    https://docs.spring.io/spring-integration/docs/current/reference/html/hazelcast.html#hazelcast-leader-election