Spring Cloud Config爲分佈式系統中的外部化配置提供服務器和客戶端支持。使用配置服務器,您能夠在中心位置管理全部環境中應用程序的外部屬性。服務器存儲後端的默認實現使用git,所以它很容易支持配置環境的標記版本,而且能夠被用於管理內容的各類工具訪問。(以上摘自http://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_spring_cloud_config);html
Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。這裏使用默認的git配置java
https://start.spring.io/建立項目,取名spring-cloud-config-server-demo,這裏dependency選擇web,config server,actuator;如圖 mysql
application.properties: git
1 #application.properties配置Actuator,這裏能夠經過http://localhost:port/actuator/beans訪問當前application context中全部bean的信息 2 management.endpoints.web.exposure.include=beans 3 #若是但願放開多個權限,以逗號分隔 4 #management.endpoints.web.exposure.include=beans,env 5 #若是但願放開全部的權限 6 #management.endpoints.web.exposure.include=*
新建bootstrap.propertiesweb
1 spring.application.name=config-server 2 server.port=9090 3 #這裏使用了gitee的配置 4 spring.cloud.config.server.git.uri=https://git路徑/ 5 spring.cloud.config.server.git.search-paths=/ 6 spring.cloud.config.server.git.username=username 7 spring.cloud.config.server.git.password=password 8 #這裏是配置的本地目錄 9 #spring.cloud.config.server.git.uri = file:///F:/gitRepsitory
主程序入口spring
1 import org.springframework.boot.SpringApplication; 2 import org.springframework.boot.autoconfigure.SpringBootApplication; 3 import org.springframework.cloud.config.server.EnableConfigServer; 4 5 @SpringBootApplication 6 @EnableConfigServer 7 public class SpringCloudConfigServerDemoApplication { 8 9 public static void main(String[] args) { SpringApplication.run(SpringCloudConfigServerDemoApplication.class, args); 10 } 11 }
項目下載方式如上,不過config server換成config client,下載導入編輯器sql
application.properties:bootstrap
1 management.endpoints.web.exposure.include=beans,env 2 server.port=8080 3 #舉個例子,jdbc:mysql://${database.ip}:3306/test,database.ip=localhost,這樣server中配置database.ip他會覆蓋當前的database.ip 4 my.name = shit
新建bootstrap.properties:後端
1 ### bootstrap 上下文配置 2 # 配置服務器 URI 3 spring.cloud.config.uri=http://localhost:9090/ 4 spring.cloud.config.name=spring-config-client 5 # profile 是激活配置 6 spring.profiles.active=prod 7 spring.cloud.config.profile=prod 8 # label 在Git中指的分支名稱 9 spring.cloud.config.label=master
主程序入口:服務器
1 package cn.cold.springcloudconfigclientdemo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.WebApplicationType; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cloud.bootstrap.config.PropertySourceLocator; 7 import org.springframework.cloud.context.refresh.ContextRefresher; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.core.Ordered; 11 import org.springframework.core.annotation.Order; 12 import org.springframework.core.env.Environment; 13 import org.springframework.core.env.MapPropertySource; 14 import org.springframework.core.env.PropertySource; 15 import org.springframework.scheduling.annotation.EnableScheduling; 16 import org.springframework.scheduling.annotation.Scheduled; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 import java.util.Set; 21 22 @SpringBootApplication 23 @EnableScheduling 24 public class SpringCloudConfigClientDemoApplication { 25 26 public static void main(String[] args) { 27 SpringApplication application = new SpringApplication(SpringCloudConfigClientDemoApplication.class); 28 application.setWebApplicationType(WebApplicationType.SERVLET); 29 application.run(args); 30 } 31 32 private final ContextRefresher contextRefresher; 33 34 private final Environment environment; 35 36 public SpringCloudConfigClientDemoApplication(ContextRefresher contextRefresher, 37 Environment environment) { 38 this.contextRefresher = contextRefresher; 39 this.environment = environment; 40 } 41 42 /** 43 * 這裏設置定時刷新獲取當前配置,否則只能經過重啓刷新配置 44 */ 45 @Scheduled(fixedRate = 5 * 1000, initialDelay = 3 * 1000) 46 public void autoRefresh() { 47 Set<String> updatedPropertyNames = contextRefresher.refresh(); 48 updatedPropertyNames.forEach(propertyName -> 49 System.err.printf("[Thread :%s] 當前配置已更新,具體 Key:%s , Value : %s \n", 50 Thread.currentThread().getName(), 51 propertyName, 52 environment.getProperty(propertyName) 53 )); 54 } 55 }
新建controller,好比咱們取名爲FuckController:
package cn.cold.springcloudconfigclientdemo.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /* * @create 2019-05-05 19:36 * @Author 江湖人稱洗髮水 * @Description //TODO **/ @RestController @RefreshScope //這裏是刷新my.name這個屬性值 public class FuckController { @Value("${my.name}") private String myName; @GetMapping("/my-name") public String getName(){ return myName; } }
這裏上傳git配置文件有四個,能夠在裏面輸入不一樣的值
兩個項目啓動,這裏訪問http://localhost:8080/my-name會返回,git中的配置文件的值那就表明獲取到了;而後能夠進一步http://localhost:8080/actuator/env查看spring.cloud.config.uri:這裏就是config server 的地址;
經過http://localhost:8080/actuator/env查看當前配置文件信息如圖
EnvironmentEndpoint
類
Environment關聯多個帶名稱的PropertySources
參考spring 源碼 AbstractRefreshableWebApplicationContext
1 protected void initPropertySources() { 2 ConfigurableEnvironment env = getEnvironment(); 3 if (env instanceof ConfigurableWebEnvironment) { 4 ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig); 5 } 6 }
Environment 有兩種實現方式:
普通類型:StandardEnvironment
Web類型:StandardServletEnvironment
Enviroment 關聯着一個PropertySources 實例
PropertySources 關聯着多個PropertySource,而且有優先級,這裏能夠經過url http://localhost:8080/actuator/env看到
關於 Spring Boot 優先級順序,能夠參考:https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-external-config
當咱們但願本身的配置不被修改,能夠用spi來加載,例如my.name這裏我就想指定爲wuyanzu,能夠添加如下代碼
1 @Configuration 2 @Order(Ordered.HIGHEST_PRECEDENCE + 100) 3 public static class CustomizePropertySourceLocator implements PropertySourceLocator { 4 5 @Override 6 public PropertySource<?> locate(Environment environment) { 7 Map<String, Object> source = new HashMap<>(); 8 source.put("my.name", "8081"); 9 MapPropertySource propertySource = 10 new MapPropertySource("my-property-source", source); 11 return propertySource; 12 } 13 }