这个快速入门使用Spring Cloud Config Server的服务器和客户端。git
首先,启动服务器,以下所示:github
$ cd spring-cloud-config-server $ ../mvnw spring-boot:run
服务器是一个Spring Boot应用程序,所以若是你愿意,能够从IDE运行它(主类是ConfigServerApplication
)。spring
接下来尝试一个客户端,以下所示:bootstrap
$ 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"}} ]}
定位属性源的默认策略是克隆git存储库(在spring.cloud.config.server.git.uri
)并使用它来初始化一个微型SpringApplication
,微型应用程序的Environment
用于枚举属性源并在JSON端点发布它们。segmentfault
HTTP服务具备如下形式的资源:服务器
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
其中,application
做为SpringApplication
中的spring.config.name
注入(常规Spring Boot应用程序中的正常application
),profile
是一个活动的配置文件(或以逗号分隔的属性列表),label
是一个可选的git标签(默认为master
)。app
Spring Cloud Config Server从git存储库(必须提供)中提取远程客户端的配置,如如下示例所示:curl
spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo
要在应用程序中使用这些功能,你能够将其构建为依赖于spring-cloud-config-client
的Spring Boot应用程序(例如,请参阅config-client
或示例应用程序的测试用例)。添加依赖项最方便的方法是使用Spring Boot启动器org.springframework.cloud:spring-cloud-starter-config
,还有一个用于Maven用户的父pom和BOM(spring-cloud-starter-parent
)以及一个用于Gradle和Spring CLI用户的Spring IO版本管理属性文件,如下示例显示了典型的Maven配置:maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>{spring-boot-docs-version}</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>{spring-cloud-version}</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 -->
如今你能够建立一个标准的Spring Boot应用程序,例如如下HTTP服务器:spring-boot
@SpringBootApplication @RestController public class Application { @RequestMapping("/") public String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
当此HTTP服务器运行时,它从端口8888上的默认本地配置服务器(若是它正在运行)中获取外部配置,要修改启动行为,能够使用bootstrap.properties
更改配置服务器的位置(相似于application.properties
但适用于应用程序上下文的bootstrap阶段),如如下示例所示:
spring.cloud.config.uri: http://myconfigserver.com
默认状况下,若是未设置应用程序名称,则将使用application
,要修更名称,能够将如下属性添加到bootstrap.properties
文件中:
spring.application.name: myapp
设置属性${spring.application.name}
时,不要在应用程序名称前加上保留字application-
,以防止解析正确属性源的问题。
bootstrap属性在/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>
的属性源包含值为bar
且具备最高优先级的foo
属性。
属性源名称中的URL是git存储库,而不是配置服务器URL。