Client Side Usage
在应用中使用这些特性,仅仅需要构建成依赖spring-cloud-config-client的Spring Boot应用(参看config-client的测试用例,或者代码示例),通过 Spring Boot starter org.springframework.cloud:spring-cloud-starter-config添加依赖是最简单的方式,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 -->
你可以创建一个标准的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);
}
}
当客户端应用运行后,它会从默认的本地配置服务获取外部配置。你可以使用bootstrap.properties修改 config server的方式修改应用的启动行为。例:
spring.cloud.config.uri: http://myconfigserver.com
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>" 的属性资源包含了属性"foo",value为"bar" ,并且为最高的优先级。
注意
属性资源的URL是git仓库,而不是配置服务的URL