Skip to content

Instantly share code, notes, and snippets.

@donovanmuller
Last active May 6, 2016 21:22
Show Gist options
  • Save donovanmuller/ec3fab8be0cc5f10f06a160abb04e3d9 to your computer and use it in GitHub Desktop.
Save donovanmuller/ec3fab8be0cc5f10f06a160abb04e3d9 to your computer and use it in GitHub Desktop.
Spring Cloud Consul, Bus (with Kafka binder) and Config causing double Consul registrations

The following scenario causes double Consul registrations:

  • spring.cloud.bus.enabled=true
  • spring.cloud.config.discovery.enabled=true
...
2016-05-06 22:23:54.086  INFO 92515 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-05-06 22:23:54.087  INFO 92515 --- [           main] o.s.c.consul.discovery.ConsulLifecycle   : Registering service with consul: NewService{id='consul-bus-double-1324109920', name='consul-bus-double', tags=[], address='192.168.1.6', port=8080, check=Check{script='null', interval=10s, ttl=null, http=http://192.168.1.6:8080/health, timeout=null}}
2016-05-06 22:23:54.104  INFO 92515 --- [           main] o.s.c.consul.discovery.ConsulLifecycle   : Registering service with consul: NewService{id='consul-bus-double-912460132', name='consul-bus-double', tags=[], address='192.168.1.6', port=8080, check=Check{script='null', interval=10s, ttl=null, http=http://192.168.1.6:8080/health, timeout=null}}
2016-05-06 22:23:54.110  INFO 92515 --- [           main] io.switchbit.Application                 : Started Application in 18.38 seconds (JVM running for 18.759)
...

When either spring.cloud.bus.enabled=false or spring.cloud.config.discovery.enabled=false and the other is true then only a single registration (expected) takes place.

...
2016-05-06 22:28:02.542  INFO 92602 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-05-06 22:28:02.542  INFO 92602 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147482647
2016-05-06 22:28:02.603  INFO 92602 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-05-06 22:28:02.607  INFO 92602 --- [           main] o.s.c.consul.discovery.ConsulLifecycle   : Registering service with consul: NewService{id='consul-bus-double-1075211196', name='consul-bus-double', tags=[], address='192.168.1.6', port=8080, check=Check{script='null', interval=10s, ttl=null, http=http://192.168.1.6:8080/health, timeout=null}}
2016-05-06 22:28:03.189  INFO 92602 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
...
package io.switchbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
spring:
cloud:
bus:
enabled: true
stream:
kafka:
binder:
zkNodes: kafka
brokers: kafka
spring:
application:
name: ${project.artifactId}
cloud:
consul:
enabled: true
discovery:
instanceId: ${spring.application.name}
preferIpAddress: true
host: consul
config:
enabled: true
fail-fast: true
discovery:
enabled: true # if spring.cloud.bus.enabled=true and this is false, then *no* double registration
serviceId: config-server
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.switchbit</groupId>
<artifactId>consul-bus-double</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RC2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
@spencergibb
Copy link

If you are just using consul for discovery and not configuration, then use spring-cloud-starter-consul-discovery. You could also just import one bom if you imported spring-cloud-dependencies:Brixton.RC2, then you don't have to know 1.0.0 vs 1.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment