如何使用 Spring 有选择地禁用 Eureka 发现客户端? [英] How to selectively disable Eureka discovery client with Spring?
问题描述
有没有办法根据 spring 配置文件禁用 spring-boot eureka 客户端注册?
Is there a way to disable spring-boot eureka client registration based on the spring profile?
目前我使用以下注释:
@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
或者应用程序属性文件中的某种方式.我尝试将 application.yml 文件设置为:
Or some way in the application properties file. I have tried setting application.yml file as:
spring:
profiles: development
cloud:
discovery:
enabled: false
但这没有用.
推荐答案
Do it like this: create some @Configuration
annotated class (class body can be省略) ex.:
Do it like this: create some @Configuration
annotated class (class body can be omitted) ex.:
@Profile("!development")
@Configuration
@EnableDiscoveryClient
public class EurekaClientConfiguration {
}
这意味着这个配置文件(以及其中的@EnableDiscoveryClient
)将被加载到除developement"之外的每个配置文件中.
It means that this configuration file (and @EnableDiscoveryClient
within) will be loaded in every profile except "developement".
希望有所帮助,
这篇关于如何使用 Spring 有选择地禁用 Eureka 发现客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!