kinguinltdhk/leadership-consul


Leader election with Consul

Download


Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.kinguinltdhk:leadership-consul:0.3.1'
	}
	<dependency>
	    <groupId>com.github.kinguinltdhk</groupId>
	    <artifactId>leadership-consul</artifactId>
	    <version>0.3.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.kinguinltdhk" % "leadership-consul" % "0.3.1"
        
        

                            
    :dependencies [[com.github.kinguinltdhk/leadership-consul "0.3.1"]]
        
        

Readme


Leadership Consul

Installation

Look into releases section and pick version.

Put into Your gradle.build

repositories {
    // ...
    maven { url "https://jitpack.io" }
}

dependencies {
    // ...
    compile "com.github.kinguinltdhk:leadership-consul:0.2.0"
}

Example of plain Java usage

Use SimpleClusterFactory to build() Your Gambler instance. Every Gambler implementation provides asObservable() which facilitate access to events by subscribers. This code will create ActiveGambler and subscribe to it with simple console output printer and print Gambler events.

You should see in the output:

elected // after 5 sconds
elected.first // after 5 sconds
elected // every 10 seconds

Notice: Execution of this code require Consul to be available on localhost:8500.

new SimpleClusterFactory()
    .mode(SimpleClusterFactory.MODE_MULTI)
    .debug(true)
    .build()
    .asObservable()
    .subscribe(n -> System.out.println(n));

Spring example

@Profile("multiinstance")
@Configuration
@ConditionalOnConsulEnabled
@EnableConfigurationProperties(value = {ClusterProperties.class, ConsulProperties.class})
public class MultiInstance {
    @Autowired
    private ClusterProperties clusterProperties;

    @Autowired
    private ConsulProperties consulProperties;

    @Bean
    public SessionConsulClient sessionConsulClient() {
        return new SessionConsulClient(consulProperties.getHost(), consulProperties.getPort());
    }

    @Bean
    public KeyValueConsulClient keyValueConsulClient() {
        return new KeyValueConsulClient(consulProperties.getHost(), consulProperties.getPort());
    }

    @Bean
    @Primary
    public Gambler multiinstance(SessionConsulClient sessionConsulClient, KeyValueConsulClient keyValueConsulClient) {
        return new SimpleClusterFactory(sessionConsulClient, keyValueConsulClient)
            .mode(SimpleClusterFactory.MODE_MULTI)
            .configure(clusterProperties.getLeader())
            .build();
    }
}

Note: Above configuration only works when Consul is enabled cause of @ConditionalOnConsulEnabled. You can remove this annotation.

Configuration

@ConfigurationProperties(prefix = "cluster")
public class ClusterProperties {
    private ClusterConfiguration leader;

    public ClusterConfiguration getLeader() {
        return leader;
    }

    public void setLeader(ClusterConfiguration leader) {
        this.leader = leader;
    }
}

In application.yml put section:

cluster:
 leader:
 serviceName: cluster
 serviceId: node-1
 consul:
 host: localhost
 port: 8500
 session:
 ttl: 15
 refresh: 7
 election:
 envelopeTemplate: services/%s/leader