小编典典

如何通过Spring有选择地禁用Eureka Discovery客户端?

spring-boot

有没有一种方法可以基于spring配置文件禁用spring-boot eureka客户端注册?

目前,我使用以下注释:

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer

public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

我需要的是有条件的(例如,伪代码)

@if (Profile!="development")
@EnableDiscoveryClient
@endif

或以某种方式在应用程序属性文件中。我尝试将application.yml文件设置为:

spring:
  profiles: development
  cloud:
    discovery:
      enabled: false

但这没有用。


阅读 739

收藏
2020-05-30

共1个答案

小编典典

这样做:创建一些带@Configuration注释的类(类主体可以省略),例如:

@Profile("!development")
@Configuration
@EnableDiscoveryClient
public class EurekaClientConfiguration {
}

这意味着此配置文件(及其@EnableDiscoveryClient内部)将被加载到除“开发”之外的每个配置文件中。

希望能有所帮助,

2020-05-30