如何使用Spring有选择地禁用Eureka发现客户端?

问题描述 投票:15回答:6

有没有办法根据弹簧配置文件禁用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

但这没效果。

spring spring-boot netflix-eureka
6个回答
25
投票

这样做:创建一些@Configuration注释类(类体可以省略)ex。:

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

这意味着除了“developpement”之外,每个配置文件中都会加载此配置文件(以及@EnableDiscoveryClient)。

希望有所帮助,


54
投票

您可以使用以下命令禁用application.yml中的eureka客户端:

eureka:
  client:
    enabled: false

它也适用于一个档案


5
投票

同样的问题在这里您只需在应用程序属性文件中输入以下配置:

  spring:
    profiles: development

  eureka:
    instance:
      hostname: localhost
    client:
      registerWithEureka: false
      fetchRegistry: false

5
投票

有一个标准的布尔spring-cloud属性

spring.cloud.discovery.enabled

这可能比“eureka”更具体,因为您可能正在使用其他提供程序。


3
投票

使用最新版本的Spring Cloud Finchley.SR2如果使用注释@EnableDiscoveryClient,则必须在application.properties中设置以下所有属性以禁用服务注册:

spring.cloud.service-registry.auto-registration.enabled=false
eureka.client.enabled=false
eureka.client.serviceUrl.registerWithEureka=false

1
投票

使用最新版本的Spring启动,请在bootstrap.yml文件中添加

Spring云版:Edgeware:SR3及以上版本

spring:
  application:
    name: test
  cloud:
    service-registry:
      auto-registration:
        enabled: false

这将禁用尤里卡。要启用它,我们只需要启用为true

© www.soinside.com 2019 - 2024. All rights reserved.