8. 嵌入配置服务器

Config Server 最好作为独立应用程序运行。但是,如果需要,可以将其嵌入另一个应用程序。为此,请使用@EnableConfigServer注解。在这种情况下,名为spring.cloud.config.server.bootstrap的可选属性会很有用。它是一个标志,用于指示服务器是否应从其自己的远程存储库中进行配置。默认情况下,该标志为关闭状态,因为它会延迟启动。但是,当嵌入到另一个应用程序中时,以与其他任何应用程序相同的方式进行初始化是有意义的。将spring.cloud.config.server.bootstrap设置为true时,还必须使用组合环境存储库配置。例如

spring:
  application:
    name: configserver
  profiles:
    active: composite
  cloud:
    config:
      server:
        composite:
          - type: native
            search-locations: ${HOME}/Desktop/config
        bootstrap: true

Note

如果使用 bootstrap 标志,则配置服务器需要在bootstrap.yml中配置其名称和存储库 URI。

要更改服务器端点的位置,您可以(可选)设置spring.cloud.config.server.prefix(例如/config),以在前缀下提供资源。前缀应以/开头但不能以/结尾。它应用于 Config Server 中的@RequestMappings(即 Spring Boot server.servletPathserver.contextPath前缀下)。

如果要直接从后端存储库(而不是从配置服务器)读取应用程序的配置,则基本上需要没有端点的嵌入式配置服务器。您可以不使用@EnableConfigServerComments(设置spring.cloud.config.server.bootstrap=true)来完全关闭端点。