问题描述
是否有一种方法可以基于spring配置文件来禁用spring-boot eureka客户端注册?
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
或以某种方式在应用程序属性文件中.我尝试将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
但这没用.
推荐答案
这样做:创建一些带有@Configuration
注释的类(类主体可以省略),例如:
Do it like this: create some @Configuration
annotated class (class body can be omitted) ex.:
@Profile("!development")
@Configuration
@EnableDiscoveryClient
public class EurekaClientConfiguration {
}
这意味着此配置文件(以及其中的@EnableDiscoveryClient
)将加载到除开发"之外的每个配置文件中.
It means that this configuration file (and @EnableDiscoveryClient
within) will be loaded in every profile except "developement".
希望有帮助,
这篇关于如何通过Spring有选择地禁用Eureka Discovery客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!