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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35142105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:57:23  来源:igfitidea点击:

How to selectively disable Eureka discovery client with Spring?

springspring-bootnetflix-eureka

提问by zinc wombat

Is there a way to disable spring-boot eureka client registration based on the spring profile?

有没有办法根据 spring 配置文件禁用 spring-boot eureka 客户端注册?

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:

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

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

But this did not work.

但这不起作用。

回答by dmitryvim

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

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

eureka:
  client:
    enabled: false

It's also for one profile

它也适用于一个配置文件

回答by patrykos91

Do it like this: create some @Configurationannotated class (class body can be omitted) ex.:

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

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

It means that this configuration file (and @EnableDiscoveryClientwithin) will be loaded in every profile except "developement".

这意味着这个配置文件(和@EnableDiscoveryClient里面)将被加载到除“开发”之外的每个配置文件中。

Hope that helps,

希望有所帮助,

回答by Timi Ruprecht

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

回答by ostmond

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 Finchley.SR2,如果您使用注解 @EnableDiscoveryClient,则必须在 application.properties 中设置以下所有属性以禁用服务注册:

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

回答by Rafael

There is a standard boolean spring-cloud property

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

spring.cloud.discovery.enabled

spring.cloud.discovery.enabled

This might be better than "eureka" specific since you might be using a different provider.

这可能比特定于“eureka”的更好,因为您可能使用不同的提供程序。

回答by denzal

With the latest version of Spring boot, Please add this in the bootstrap.yml file

使用最新版本的 Spring boot,请在 bootstrap.yml 文件中添加这个

Spring cloud version : Edgeware: SR3 and above

Spring Cloud 版本:Edgeware:SR3 及以上

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

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