Skip to content

spring integration leader starter

Patrick D edited this page Sep 23, 2018 · 28 revisions

Installation and Configuration

Add the following module as a dependency:

<dependency>
   <groupId>com.mimacom.spring</groupId>
   <artifactId>spring-integration-leader-starter</artifactId>
   <version>xxx</version>
</dependency>

Leader Election Provides

The spring-integration-leader-starter is only going to configure itself if one of the following "leader-providers" is available and configured

  • Zookeeper
  • Hazelcast
  • Lock-Registry

Zookeeper based Leader-Election

Provide a CuratorFramework bean by using a spring-boot auto-configuration aware module such as the following:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-zookeeper-core</artifactId>
</dependency>

... or manually define the bean

@Configuration
public class MyZookeeperConfiguration {

   @Bean
   CuratorFrameworkFactoryBean curatorFramework() {
       return new CuratorFrameworkFactoryBean("localhost:2181");
   }

}

(CuratorFrameworkFactoryBean is part of the spring-integration-zookeeper module)

Hazelcast based Leader-Election

Provide a HazelcastInstance by defining it manually or let the HazelcastAutoConfiguration from the spring-boot-autoconfigure module define it for you.

Lock-Registry based Leader-Election

Provide a LockRegistryLeaderInitiator

@Configuration
class JdbcLockRegistryConfiguration {

	private final DataSource dataSource;

	JdbcLockRegistryConfiguration(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Bean
	public DefaultLockRepository lockRepository() {
		return new DefaultLockRepository(this.dataSource);
	}

	@Bean
	public LockRegistry lockRegistry() {
		return new JdbcLockRegistry(this.lockRepository());
	}

	@Bean
	public LockRegistryLeaderInitiator leaderInitiator() {
		LockRegistryLeaderInitiator lockRegistryLeaderInitiator = new LockRegistryLeaderInitiator(this.lockRegistry());
		lockRegistryLeaderInitiator.start();
		return lockRegistryLeaderInitiator;
	}

 }

Usage

Leader Aware Integration Endpoints

You can define that some of your integration endpoints become "leader-aware" by declaring their endpoint bean name in your spring configuration file. The configured Endpoints are then not going to "autostart" on startup instead they wait until the application has become the leader (org.springframework.integration.leader.event.OnGrantedEvent) and if so the Endpoints are started. Visa-verca if the application's leadship has been revoked (org.springframework.integration.leader.event.OnRevokedEvent) the configured Endpoints are then gracefully stopped.

Example:

@Configuration
public class FlowExampleConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(FlowExampleConfig.class);

    private static final String POLLER_BEAN_NAME = "sample-polling-endpoint";

    @Bean
    IntegrationFlow sampleIntegrationFlow() {
        return IntegrationFlows
                .from(() -> UUID.randomUUID().toString(), c -> c
                        .id(POLLER_BEAN_NAME)
                        .poller(p -> p.fixedDelay(1000))
                        .autoStartup(true))
                .handle((GenericHandler<String>) (payload, headers) -> {
                    LOGGER.info("Handle Message: {}", payload);
                    return null;
                })
                .get();
    }
}

In your application.[yml|property] define the endpoint-bean-id in the key: spring-integration.leader-aware.endpoints

spring-integration:
  leader-aware:
    endpoints: sample-polling-endpoint

In the above example if we'd start multipe instances of the application only one - the leader - executes the sampleIntegrationFlow since it's endpoint's poller sample-polling-endpoint has been started once it received the Leader OnGrantedEvent.

Health Endpoint

A simple Health Indicator is created that show whether the current application instance is the leader. You can disable the health indicator like any actuator based health indicator in your application.[yml|property] using the key: management.health.leader.enabled

management:
  health:
    leader:
      enabled: false
Clone this wiki locally