25

Is there a way to disable spring-boot eureka client registration based on the spring profile?

Currently I use the following annotations:

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer

public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

What I need is either a conditional such as (excuse the pseudo code)

@if (Profile!="development")
@EnableDiscoveryClient
@endif

Or some way in the application properties file. I have tried setting application.yml file as:

spring:
  profiles: development
  cloud:
    discovery:
      enabled: false

But this did not work.

Boyan Kushlev
  • 833
  • 1
  • 14
  • 30
zinc wombat
  • 273
  • 1
  • 4
  • 6
  • 1
    Possible duplicate of [Including bean definition when a profile is NOT active](http://stackoverflow.com/questions/13575201/including-bean-definition-when-a-profile-is-not-active) – g00glen00b Feb 02 '16 at 07:03
  • To use class in all cases excluding one profile, you can write `@Profile("!development")` – dmitryvim Oct 14 '16 at 08:10
  • Trying to track down where in the docs that "!development" syntax is outlined ... so far without any luck.@dmitryvim – James Gawron Jun 18 '19 at 16:27

7 Answers7

77

You can disable eureka client in application.yml using this:

eureka:
  client:
    enabled: false

It's also for one profile

dmitryvim
  • 1,317
  • 3
  • 11
  • 22
  • 5
    Please add explanations to your answer. – Nikolay Mihaylov Jun 16 '16 at 08:11
  • in application.yml or application.properties you can set spring variables in different profiles, as it was written in third question code sample. I just suggested to disable eureka this way – dmitryvim Oct 14 '16 at 08:09
  • To disable the Eureka Discovery Client, you can set eureka.client.enabled to false. Eureka Discovery Client will also be disabled when spring.cloud.discovery.enabled is set to false. Check Link https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html#_registering_with_eureka – Karan Kaw Apr 13 '21 at 13:03
35

Do it like this: create some @Configuration annotated class (class body can be omitted) ex.:

@Profile("!development")
@Configuration
@EnableDiscoveryClient
public class EurekaClientConfiguration {
}

It means that this configuration file (and @EnableDiscoveryClient within) will be loaded in every profile except "developement".

Hope that helps,

patrykos91
  • 3,038
  • 2
  • 18
  • 26
11

With the latest version of Spring Cloud Finchley.SR2 if you are using the annotation @EnableDiscoveryClient you have to set all of the following properties in application.properties to disable the service registration:

spring.cloud.service-registry.auto-registration.enabled=false
eureka.client.enabled=false
eureka.client.serviceUrl.registerWithEureka=false
ostmond
  • 190
  • 2
  • 12
9

Same issue here. You can simply put in your application property file the following configuration:

  spring:
    profiles: development

  eureka:
    instance:
      hostname: localhost
    client:
      registerWithEureka: false
      fetchRegistry: false
Timi Ruprecht
  • 91
  • 1
  • 3
6

There is a standard boolean spring-cloud property

spring.cloud.discovery.enabled

This might be better than "eureka" specific since you might be using a different provider.

Rafael
  • 2,153
  • 2
  • 27
  • 49
5

With the latest version of Spring boot, Please add this in the bootstrap.yml file

Spring cloud version : Edgeware: SR3 and above

spring:
  application:
    name: test
  cloud:
    service-registry:
      auto-registration:
        enabled: false

This will disable eureka. To enable it, we just need to make enabled as true

denzal
  • 997
  • 10
  • 17
0

To disable the Eureka Discovery Client, you can set eureka.client.enabled to false.
Eureka Discovery Client will also be disabled when spring.cloud.discovery.enabled is set to false.

Reference
https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html#_registering_with_eureka

Karan Kaw
  • 400
  • 6
  • 14