How to selectively disable Eureka discovery client with Spring?
Asked Answered
P

7

39

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.

Pumpernickel answered 1/2, 2016 at 23:1 Comment(3)
Possible duplicate of Including bean definition when a profile is NOT activeMalayalam
To use class in all cases excluding one profile, you can write @Profile("!development")Shipping
Trying to track down where in the docs that "!development" syntax is outlined ... so far without any luck.@ShippingBasophil
S
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,

Seacock answered 2/2, 2016 at 17:1 Comment(7)
Thanks that was what I was looking forPumpernickel
Upvoting this because it's an awesome answer.Vexation
People who use SO tend to copy code right away. Please fix "developement".wasted my whole hour.Zackaryzacks
Fixed, sorry for that. I know how big pain in the ass is looking for something like that.Seacock
It works but stil issue occur when running the tests .Manners
Still trying to figure out how to avoid this while running test! Thank you @EnricoGiurin for putting this - else I was thinking this solution isn't working.Gustaf
Using profile depending settings is technically fine but poor because it degrades the quality of your production code.Slobbery
S
106

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

eureka:
  client:
    enabled: false

It's also for one profile

Shipping answered 16/6, 2016 at 7:44 Comment(5)
Please add explanations to your answer.Huehuebner
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 wayShipping
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 cloud.spring.io/spring-cloud-netflix/multi/…Valer
@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.Localism
@KaranKaw @Localism actually, spring.cloud.discovery.enabled: false worked well for me, whereas spring.cloud.config.discovery.enabled: false did not. I'm using spring-boot 2.7.12 + spring-core 5.3.30 + spring-cloud-starter-* 3.1.1, in case it could be related with some flagging difference.Fetlock
S
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,

Seacock answered 2/2, 2016 at 17:1 Comment(7)
Thanks that was what I was looking forPumpernickel
Upvoting this because it's an awesome answer.Vexation
People who use SO tend to copy code right away. Please fix "developement".wasted my whole hour.Zackaryzacks
Fixed, sorry for that. I know how big pain in the ass is looking for something like that.Seacock
It works but stil issue occur when running the tests .Manners
Still trying to figure out how to avoid this while running test! Thank you @EnricoGiurin for putting this - else I was thinking this solution isn't working.Gustaf
Using profile depending settings is technically fine but poor because it degrades the quality of your production code.Slobbery
U
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
Unkind answered 4/12, 2018 at 10:24 Comment(0)
M
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
Malvern answered 13/5, 2016 at 23:51 Comment(0)
P
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.

Pearson answered 4/8, 2016 at 15:23 Comment(1)
In my case this doesn't work but eureka.client.enabled: false does.Kerch
E
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

Elbertine answered 14/11, 2018 at 13:14 Comment(0)
V
4

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

Valer answered 13/4, 2021 at 13:6 Comment(1)
spring.cloud.discovery.enabled: false worked well for me. Thank you @KaranKawFetlock

© 2022 - 2024 — McMap. All rights reserved.