嵌入配置服务器

配置服务器作为独立应用运行效果最佳。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

然而,如有需要,您也可以将其嵌入到其他应用程序中。spring-doc.cadn.net.cn

为此,请使用 @EnableConfigServer 注解。spring-doc.cadn.net.cn

在此情况下,可选属性 spring.cloud.config.server.bootstrap 可能很有用。spring-doc.cadn.net.cn

它是一个标志,用于指示服务器是否应从其自身的远程仓库中进行自身配置。spring-doc.cadn.net.cn

默认情况下,该标志处于关闭状态,因为这可能会延迟启动时间。spring-doc.cadn.net.cn

然而,当嵌入到其他应用程序中时,以与其他应用程序相同的方式进行初始化是合理的。spring-doc.cadn.net.cn

当将 spring.cloud.config.server.bootstrap 设置为 true 时,还必须使用 复合环境仓库配置spring-doc.cadn.net.cn

例如:spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

spring:
  application:
    name: configserver
  profiles:
    active: composite
  cloud:
    config:
      server:
        composite:
          - type: native
            search-locations: ${HOME}/Desktop/config
        bootstrap: true
如果您使用了 bootstrap 标志,则配置服务器需要在 bootstrap.yml 中配置其名称和仓库 URI。

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

如果您希望直接从后端仓库读取应用程序的配置(而不是从配置服务器读取),本质上您需要一个嵌入式的配置服务器,且不包含任何端点。您可以通过完全不使用 @EnableConfigServer 注解来关闭所有端点(设置 spring.cloud.config.server.bootstrap=true)。spring-doc.cadn.net.cn