這個快速入門使用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。