24、其他配置

79 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第24天,点击查看活动详情

24、其他配置

二、其他配置

1、服务器健康指示器 -- 服务器

默认情况下,服务器会访问配置的 SYN 仓库 uri ,如果连接不上,那么服务器的健康状态将会置为 DOWN。除了会检测 SYN 仓库的 uri 是否可以访问外,还可以进行额外配置, 检测 uri 下面的目录是否可以连接,请见以下配置:

#服务器健康指示器 -- 检测特定uri目录是否可以连接
#spring.cloud.config.server.health.repositories.book-server.label=health-test

配置服务器会去 SVN 中连接 https ://localhost/svn/test-project/health-test ,如果 SVN 上不存在该目录,在访问服务器的/health 时,将会得到以下 JSON 字符串:

{
    "configServer": {
        "error": "org.springframework.cloud.config.server.environment.NoSuchLabelException: No label found for: health-test",
        "repository": {
            "application": "book-server",
            "profiles": "default"
        },
        "status": "DOWN"
    },
    "diskSpace": {
        "free": 60238761984,
        "status": "UP",
        "threshold": 10485760,
        "total": 126578847744
    },
    "refreshScope": {
        "status": "UP"
    },
    "status": "DOWN"
}

如果不想启用健康指示器,可将 spring.cloud.config.server.health.enabled 属性设置为false

2、客户端的错误提前与重试机制 -- 客户端

在实际应用中可能会有一些特殊需求,例如客户端比较关心配置服务器是否能连接上,在启动时如果无法连接,宁可自己启动失败,也不能带着“错误”启动容器。对于这种情况,可以使用 Spring Cloud Config 的错误提前机制 。在客户端的 bootstrap.yml 中,将 spring. cloud.config.failFast 属性设置为 true ,即可实现错误提前,只要在启动时无法连接( 一次)配置服务器,则会中止容器的启动。 如果连接错误,就会有与之配套的重试机制。先将 spring.cloud.config. failFast 设置为 true, 再为客户端添加 spring-retry 和 spring-boot-starter-aop 的依赖:

<!--客户端连接配置中心 错误提前 与重试-->
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>1.5.4.RELEASE</version>
</dependency>

完成 以上两步后就可以在客户端的 bootstrap. yml 配置文件中设置重试的参数, 主要使 用以下几个参数。

#错误提前,只要启动时无法连接(一次)配置服务器,则会中止容器的启动
spring.cloud.config.fail-fast=true
#初始的重试间隔,默认为 1000 毫秒
spring.cloud.config.retry.initial-interval=1000
#最大重试次数,默认为6
spring.cloud.config.retry.max-attempts=6
#最大的重试间隔,默认为2000毫秒
spring.cloud.config.retry.max-interval=2000
#重试间隔的递增系数,默认为1.1
spring.cloud.config.retry.multiplier=1.1

spring.cloud.config.fail-fast=false 时如果配置服务处于宕机状态客户端也可以启动,设置为true之后,配置服务器必须正常状态下,客户端才可以正常启动。

3、安全配置

配置服务器可以使用 spring-boot-starter-security 模块来设置客户端的访问权限,在配置 服务器中加入以下依赖:

<!--配置服务器安全配置-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>1.5.12.RELEASE</version>
</dependency>

修改服务器的 bootstrap.properties,配置访问的用户名,密码:

#配置中心安全配置,配置用户名密码
security.user.name=root
security.user.password=songrongliang

进行以上配置后,客户端在启动或者获取配置时,就会得到 401 错误,错误为:org.springframework.web.client.HttpClientErrorException: 401 null 为客户端的bootstrap .properties 加入以下配置,即可解决权限问题:

#配置中心的安全配置,配置访问配置中心的用户名密码
spring.cloud.config.username=root
spring.cloud.config.password=songrongliang

4、访问服务器配置

在前面的章节中,启动配置服务器后,可以使用 http://localhost:8888/first-test.properties 这样的地址来访问配置。下面对配置的访问规则进行简单描述。下面的规则可以访问SVN 中的资源:

  • /{application}/{profile}/[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

以上的 HTTP 资源, application 表示客户端应用的名称,可使用 spring.application.name 或者 spring.cloud.config.name 进行配置, profile 表示客户端所使用的配置, label 表示所配置的目录标签。这三个属性在前面章节己经有过相应的描述 在此不再赘述。

image.png