Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to selectively disable Eureka discovery client with Spring?

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.

like image 495
zinc wombat Avatar asked Feb 01 '16 23:02

zinc wombat


People also ask

How do I turn off Eureka Discovery client?

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

How do I turn on spring boot on Eureka client?

The @EnableEurekaClient annotation makes your Spring Boot application act as a Eureka client. To register the Spring Boot application into Eureka Server we need to add the following configuration in our application. properties file or application. yml file and specify the Eureka Server URL in our configuration.

What is the difference between Eureka client and Discovery client?

There is no difference.

What is Eureka client serviceUrl defaultZone?

application. eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/


3 Answers

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

eureka:
  client:
    enabled: false

It's also for one profile

like image 126
dmitryvim Avatar answered Oct 22 '22 05:10

dmitryvim


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,

like image 31
patrykos91 Avatar answered Oct 22 '22 05:10

patrykos91


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
like image 19
ostmond Avatar answered Oct 22 '22 04:10

ostmond