1. 程式人生 > >Spring Cloud Config - 客戶端使用

Spring Cloud Config - 客戶端使用

spring springcloud cloud

要在應用程序中使用這些功能,只需將其構建為依賴於spring-cloud-config-client的Spring引導應用程序(例如,查看配置客戶端或示例應用程序的測試用例)。添加依賴關系的最方便的方法是通過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 -->

那麽你可以創建一個標準的Spring Boot應用程序,像這個簡單的HTTP服務器:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

當它運行它將從端口8888上的默認本地配置服務器接收外部配置,如果它正在運行。要修改啟動行為,您可以使用bootstrap.properties(如application.properties)更改配置服務器的位置,但用於應用程序上下文的引導階段),例如

spring.cloud.config.uri: http://myconfigserver.com

引導屬性將在/env端點中顯示為高優先級屬性源,例如

$ curl localhost:8080/env
{
  "profiles":[],
  "configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"},
  "servletContextInitParams":{},
  "systemProperties":{...},
  ...
}

源碼來源地址

Spring Cloud Config - 客戶端使用