Using Consul or Kubernetes Discovery Client depending on environment variable

1/17/2022

I'm trying to add 2 different discovery clients depending on my Env variable. I want to do that for easier local development, without running local k8s cluster.

  cloud:
    retry:
      initial-interval: 10
      max-interval: 20
    kubernetes:
      discovery:
        enabled: ${!CONSUL_ENABLED:true}
    consul:
      host: ${CONSUL_HOST:localhost}
      port: ${CONSUL_PORT:8500}
      discovery:
        enabled: ${CONSUL_ENABLED:false}
        fail-fast: true
        instance-id: ${spring.application.name}-${server.port}-${instance-id}
        health-check-path: /actuator/health
        health-check-interval: 20s

My gradle file:

implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes'
implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'

But this config fails with error:

Field registration in org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration required a single bean, but 2 were found:
	- getRegistration: defined by method 'getRegistration' in class path resource [org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfiguration.class]
	- consulRegistration: defined by method 'consulRegistration' in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]

Is there any way to do that?

-- Pixel
consul
kubernetes
service-discovery
spring
spring-boot

1 Answer

1/17/2022

Ok, i found way to do that, you must disable all k8s, not only discovery:

  cloud:
    retry:
      initial-interval: 10
      max-interval: 20
    kubernetes:
      discovery:
        enabled: ${K8S_ENABLED:true}
      enabled: ${K8S_ENABLED:true}
    consul:
      host: ${CONSUL_HOST:localhost}
      port: ${CONSUL_PORT:8500}
      discovery:
        enabled: ${CONSUL_ENABLED:false}
        fail-fast: true
        instance-id: ${spring.application.name}-${server.port}-${instance-id}
        health-check-path: /actuator/health
        health-check-interval: 20s

But is there a way to combine K8S_ENABLED and CONSUL_ENABLED variable to one?

-- Pixel
Source: StackOverflow