要在應(yīng)用程序中使用這些功能衣撬,只需將其構(gòu)建為依賴于spring-cloud-config-client的Spring引導(dǎo)應(yīng)用程序(例如,查看配置客戶端或示例應(yīng)用程序的測試用例)。添加依賴關(guān)系的最方便的方法是通過Spring Boot啟動器org.springframework.cloud:spring-cloud-starter-config。還有一個Maven用戶的父pom和BOM(spring-cloud-starter-parent)和用于Gradle和Spring CLI用戶的Spring IO版本管理屬性文件勘高。示例Maven配置:
的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</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 -->
那么你可以創(chuàng)建一個標(biāo)準(zhǔn)的Spring Boot應(yīng)用程序,像這個簡單的HTTP服務(wù)器:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
當(dāng)它運(yùn)行它將從端口8888上的默認(rèn)本地配置服務(wù)器接收外部配置,如果它正在運(yùn)行华望。要修改啟動行為蕊蝗,您可以使用bootstrap.properties(如application.properties)更改配置服務(wù)器的位置,但用于應(yīng)用程序上下文的引導(dǎo)階段)赖舟,例如
spring.cloud.config.uri: http://myconfigserver.com
引導(dǎo)屬性將在/env端點(diǎn)中顯示為高優(yōu)先級屬性源蓬戚,例如
$ curl localhost:8080/env
{
"profiles":[],
"configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
"servletContextInitParams":{},
"systemProperties":{...},
...
}
(名為“configService:<遠(yuǎn)程存儲庫的URL> / <文件名>”的屬性源包含值為“bar”的屬性“foo”,是最高優(yōu)先級)宾抓。