《Spring Cloud Config官方文档》之Spring Cloud Config客户端

10.  Spring Cloud Config客户端

Spring Boot应用程序可以立即利用Spring Config服务器(或应用程序开发人员提供的其他外部属性资源),还可以获取与Environment更改事件有关的一些其他有用的功能。

10.1 配置第一引导

这是在类路径上具有Spring Cloud Config Client的任何应用程序的默认行为。当一个配置客户端启动时,它绑定到配置服务器(通过引导配置属性spring.cloud.config.uri),并用远程属性来初始化Spring Environment

这样做的最终结果是,所有想要使用配置服务器的客户端应用程序都需要一个bootstrap.yml(或一个环境变量),并且它的服务器地址为spring.cloud.config.uri(默认为“http:// localhost:8888”)。

10.2 发现第一引导

如果您正在使用“DiscoveryClient”实现,例如Spring Cloud Netflix和Eureka服务发现或Spring Cloud Consul(Spring Cloud Zookeeper 不支持此功能),则可以让Config Server在发现服务中进行注册(如果需要),但在默认的“配置优先”模式下,客户端将无法利用注册。

如果您更喜欢用DiscoveryClient来定位配置服务器,可以通过设置spring.cloud.config.discovery.enabled=true(默认为“false”)来实现。最终的结果是,客户端应用程序都需要一个 bootstrap.yml(或一个环境变量)与适当的发现配置。例如,使用Spring Cloud Netflix,您需要定义Eureka服务器地址,例如eureka.client.serviceUrl.defaultZone。使用此选项的代价是在启动时进行额外的网络往返查找服务注册。好处是配置服务器可以改变它的坐标,前提是发现服务是一个固定的点。默认的服务ID是“configserver”,但是你可以采用spring.cloud.config.discovery.serviceId在服务器上用服务器修改服务(例如通过设置服务器spring.application.name)。

发现客户端实现都支持某种类型的元数据映射(例如我们拥有的Eureka eureka.instance.metadataMap)。Config服务器的一些其他属性可能需要在其服务注册元数据中进行配置,以便客户端可以正确连接。如果Config Server使用HTTP Basic进行安全保护,则可以将凭证配置为“用户名”和“密码”。如果配置服务器有一个上下文路径,你可以设置“configPath”。例如,对于作为Eureka客户端的Config服务器:

bootstrap.yml。

eureka:
  instance:
    ...
    metadataMap:
      user: osufhalskjrtl
      password: lviuhlszvaorhvlo5847
      configPath: /config

10.3 配置客户端快速失败

在某些情况下,如果服务无法连接到配置服务器,则可能需要启动服务失败。如果这是所需的行为,请设置引导程序配置属性 spring.cloud.config.failFast=true,客户端将通过异常暂停使用。

10.4 配置客户端重试

如果您希望在您的应用程序启动时配置服务器可能偶尔不可用,则可以要求它在发生故障后继续尝试。首先你需要设置spring.cloud.config.failFast=true,然后你需要添加 spring-retryspring-boot-starter-aop到你的类路径。默认行为是重试6次,初始回退间隔为1000ms,后续回退的指数乘数为1.1。您可以使用spring.cloud.config.retry.*配置属性来配置这些属性(和其他属性)。

要完全控制重试,请添加一个ID为“configServerRetryInterceptor” 的类型为RetryOperationsInterceptord的  @Bean。Spring Retry有一个RetryInterceptorBuilder,这将很容易创建。

10.5 查找远程配置资源

配置服务提供来自/{name}/{profile}/{label}的属性来源,客户端应用程序的默认绑定如下:

  • “name” = ${spring.application.name}
  • “profile” = ${spring.profiles.active} (实际上 Environment.getActiveProfiles())
  • “label” = “master”

所有这些都可以通过设置spring.cloud.config.* (其中*是“名称”,“配置文件”或“标签”)来覆盖。“标签”对回滚到以前版本的配置非常有用; 与默认的配置服务器实现它可以是一个git标签,分支名称或提交ID。也可以将标签作为逗号分隔列表提供,在这种情况下,列表中的项目将逐个尝试,直到成功完成为止。这在处理特性分支时非常有用,例如,当你想要将分配标签与你的分支对齐时,使其成为可选的(例如spring.cloud.config.label=myfeature,develop)。

10.6 安全

如果您在服务器上使用HTTP Basic安全性,则客户端只需知道密码(如果不是默认密码,则需要输入用户名)。你可以通过配置服务器URI,或通过单独的用户名和密码属性,例如

bootstrap.yml。

spring:
   cloud:
     config:
      uri:https:// user:secret@myconfig.mycompany.com

或者是

bootstrap.yml。

spring:
   cloud:
     config:     uri:https
 :
 //myconfig.mycompany.com      username:user
      password:secret

spring.cloud.config.passwordspring.cloud.config.username 值将覆盖在URI中提供的任何东西。

如果您在Cloud Foundry上部署应用程序,则提供密码的最佳方式是通过服务凭据(例如,在URI中),因为它甚至不需要在配置文件中。Cloud Foundry上名为“configserver”的本地和用户提供的服务的示例:

bootstrap.yml。

spring:
   cloud:
     config:
      uri:$ {vcap.services.configserver.credentials.uri:http:// user:password @ localhost :8888 }

如果您使用安全的另一种形式,你可能需要提供 RestTemplate 到ConfigServicePropertySourceLocator(在引导方面抓住它,并注入一个EG)。

10.6.1 健康指标

Config客户端提供了一个Spring Boot Health Indicator,它试图从配置服务器加载配置。健康指示器可以通过设置health.config.enabled=false来禁用。由于性能原因,响应也被缓存。默认的缓存时间是5分钟。要更改该值,请设置该health.config.time-to-live属性(以毫秒为单位)。

10.6.2 提供自定义RestTemplate

在某些情况下,您可能需要自定义从客户端向配置服务器发出的请求。通常这包括传递特殊的Authorization头来验证对服务器的请求。要提供自定义RestTemplate,请按照以下步骤操作。

1.用一个PropertySourceLocator实现创建一个新的配置bean 。 CustomConfigServiceBootstrapConfiguration.java。

@Configuration
public class CustomConfigServiceBootstrapConfiguration {
    @Bean
    public ConfigServicePropertySourceLocator configServicePropertySourceLocator() {
        ConfigClientProperties clientProperties = configClientProperties();
       ConfigServicePropertySourceLocator configServicePropertySourceLocator =  new ConfigServicePropertySourceLocator(clientProperties);
        configServicePropertySourceLocator.setRestTemplate(customRestTemplate(clientProperties));
        return configServicePropertySourceLocator;
    }
}

1.在resources/META-INF创建一个名为spring.factories的文件,并指定您的自定义配置。 spring.factories。

org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration

10.6.3 保险库

将Vault用作配置服务器的后端时,客户端需要为服务器提供令牌以从Vault检索值。此令牌可在客户端内在bootstrap.yml中通过设置spring.cloud.config.token 来提供。

bootstrap.yml。

spring:
   cloud:
     config:
       token:YourVaultToken

10.7 Vault

10.7.1 Vault中的嵌套键

Vault支持将密钥嵌套到存储在Vault中的值中。例如

echo -n '{"appA": {"secret": "appAsecret"}, "bar": "baz"}' | vault write secret/myapp -

这个命令将把一个JSON对象写入你的Vault。要在Spring中访问这些值,可以使用传统的点(.)注释。例如

@Value(“$ {appA.secret}”) 
String name = “World” ;

上面的代码将设置name变量到appAsecret

原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 《Spring Cloud Config官方文档》之Spring Cloud Config客户端

  • Trackback 关闭
  • 评论 (0)
  1. 暂无评论

return top