4. 快速入门
启动服务器:
$ cd spring-cloud-config-server
$ ../mvnw spring-boot:run
服务器是一个 Spring Boot application,因此如果您愿意,可以从 IDE 中运行它(主 class 是ConfigServerApplication
)。然后尝试一个 client:
$ curl localhost:8888/foo/development
{"name":"foo","label":"master","propertySources":[
{"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},
{"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}
]}
定位 property 源的默认策略是克隆 git repository(在spring.cloud.config.server.git.uri
)并使用它来初始化 mini SpringApplication
。 mini-application 的Environment
用于枚举 property 源并通过 JSON 端点发布它们。
HTTP 服务具有以下形式的资源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中“application”作为SpringApplication
注入SpringApplication
(i.e.常规 Spring Boot 应用程序中通常为“application”),“ profile”是 active profile(或 properties 的 comma-separated 列表),“label”是一个可选的 git 标签(默认为“master”.)
Spring Cloud Config Server 从 git repository(必须提供)中为 remote clients 提取 configuration:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
4.1 Client Side Usage
要在 application 中使用这些 features,只需将其构建为依赖于 spring-cloud-config-client 的 Spring Boot application(e.g. 请参阅 config-client 或 sample 应用程序的测试用例)。添加依赖项最方便的方法是通过 Spring Boot starter org.springframework.cloud:spring-cloud-starter-config
。 Maven 用户还有 parent pom 和 BOM(spring-cloud-starter-parent
),Gradle 和 Spring CLI 用户也有 Spring IO version management properties 文件。 Example Maven configuration:
pom.xml.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- repositories also needed for snapshots and milestones -->
然后你可以像这个简单的 HTTP 服务器一样创建一个标准的 Spring Boot application:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
当它运行时,它将从 port 8888 上的默认本地配置服务器获取外部 configuration,如果它是 running。要修改启动行为,可以使用bootstrap.properties
更改配置服务器的位置(如application.properties
,但对于 application context 的引导阶段),e.g.
spring.cloud.config.uri: http://myconfigserver.com
引导程序 properties 将作为 high-priority property 源 e.g 显示在/env
端点中。
$ curl localhost:8080/env
{
"profiles":[],
"configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
"servletContextInitParams":{},
"systemProperties":{...},
...
}
(一个名为“configService:<URL of remote repository>/<file name>”的 property 源包含带有 value“bar”的 property“foo”,并且是最高优先级)。
property source name 中的 URL 是 git repository 而不是配置服务器 URL。