8. 嵌入配置服务器
Config Server 作为独立的 application 运行最佳。但是,如果需要,可以将其嵌入另一个 application 中。为此,请使用@EnableConfigServer
annotation。在这种情况下,名为spring.cloud.config.server.bootstrap
的可选 property 非常有用。这是一个 flag 来指示服务器是否应该从自己的 remote repository 配置自己。默认情况下,flag 处于关闭状态,因为它可以延迟启动。但是,当嵌入到另一个 application 中时,以与任何其他 application 相同的方式初始化是有意义的。将spring.cloud.config.server.bootstrap
设置为true
时,还必须使用复合环境 repository configuration。例如
spring:
application:
name: configserver
profiles:
active: composite
cloud:
config:
server:
composite:
- type: native
search-locations: ${HOME}/Desktop/config
bootstrap: true
如果使用 bootstrap flag,则配置服务器需要在
bootstrap.yml
中配置其 name 和 repository URI。
要更改服务器 endpoints 的位置,您可以(可选)设置spring.cloud.config.server.prefix
(对于 example,/config
),以便为前缀下的资源提供服务。前缀应该开始但不以/
结束。它应用于 Config Server 中的@RequestMappings
(即 Spring Boot server.servletPath
和server.contextPath
前缀下面)。
如果你想直接从后端 repository(而不是从配置服务器)读取 application 的 configuration,你基本上会看到一个没有 endpoints 的嵌入式配置服务器。您可以通过不使用@EnableConfigServer
annotation(设置spring.cloud.config.server.bootstrap=true
)完全关闭 endpoints。