Merge pull request #9755 from carloscaverobarca/BAEL-3217-Leadership-Election-with-Spring-Cloud-Consul

BAEL-3217 Leadership Election with Consul
This commit is contained in:
davidmartinezbarua 2020-08-19 10:13:44 -03:00 committed by GitHub
commit aa98315666
4 changed files with 58 additions and 6 deletions

View File

@ -12,6 +12,13 @@
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -25,18 +32,32 @@
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>${spring-cloud-starter-consul.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.kinguinltdhk</groupId>
<artifactId>leadership-consul</artifactId>
<version>${kinguinltdhk.version}</version>
<exclusions>
<exclusion>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<spring-cloud-starter-consul.version>1.3.0.RELEASE</spring-cloud-starter-consul.version>
<spring-boot-starter-test.version>1.5.10.RELEASE</spring-boot-starter-test.version>
<spring-cloud-starter-consul.version>2.2.3.RELEASE</spring-cloud-starter-consul.version>
<kinguinltdhk.version>0.3.1</kinguinltdhk.version>
<start-class>com.baeldung.spring.cloud.consul.discovery.DiscoveryClientApplication</start-class>
</properties>
</project>

View File

@ -11,7 +11,7 @@ import static org.springframework.boot.WebApplicationType.NONE;
public class DiscoveryClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(NONE)
new SpringApplicationBuilder(DiscoveryClientApplication.class)
.run(args);
}

View File

@ -0,0 +1,16 @@
package com.baeldung.spring.cloud.consul.leadership;
import java.util.concurrent.ExecutionException;
import net.kinguin.leadership.consul.factory.SimpleConsulClusterFactory;
public class LeadershipElection {
public static void main(String[] args) throws ExecutionException, InterruptedException {
new SimpleConsulClusterFactory()
.mode(SimpleConsulClusterFactory.MODE_MULTI)
.debug(true)
.build()
.asObservable()
.subscribe(i -> System.out.println(i));
}
}

View File

@ -11,4 +11,19 @@ spring:
enabled: true
instanceId: ${spring.application.name}:${random.value}
server:
port: 0
port: 0
cluster:
leader:
serviceName: cluster
serviceId: node-1
consul:
host: localhost
port: 8500
discovery:
enabled: false
session:
ttl: 15
refresh: 7
election:
envelopeTemplate: services/%s/leader