38

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
  • 1,043
  • 1
  • 18
  • 35
zinc wombat
  • 403
  • 1
  • 5
  • 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

104

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

eureka:
  client:
    enabled: false

It's also for one profile

dmitryvim
  • 1,983
  • 4
  • 15
  • 27
  • 6
    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
  • 3
    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
  • @KaranKaw `spring.cloud.discovery.enabled` is incorrect. The correct property name is `spring.cloud.config.discovery.enabled`, and setting that to `false` fixed my issue. – Raphael Oct 13 '22 at 23:12
36

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,506
  • 2
  • 24
  • 30
21

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
  • 519
  • 1
  • 7
  • 13
11

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
  • 131
  • 1
  • 4
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,521
  • 2
  • 33
  • 59
6

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
  • 1,225
  • 13
  • 20
3

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
  • 510
  • 1
  • 7
  • 15