How to selectively disable Eureka discovery client with Spring?

帅比萌擦擦* 提交于 2019-12-17 18:38:31

问题


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.


回答1:


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,




回答2:


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

eureka:
  client:
    enabled: false

It's also for one profile




回答3:


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



回答4:


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.




回答5:


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



回答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



来源:https://stackoverflow.com/questions/35142105/how-to-selectively-disable-eureka-discovery-client-with-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!