研究spring cloud天然少不了配置中心,項目中使用的是apollo,想研究下spring cloud config基於git的。html
一、首先在git上面建立你的配置文件信息,我用本身搭建的gitlab來搞下,建立了兩個配置文件,文件內容很簡單。git
testconfig: my is devweb
testconfig: my is prodspring
二、先添加引用在說,個人項目是多模塊項目,在根pom下添加以下引用。bootstrap
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
三、在註冊中心的項目添加pom引用app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
四、在註冊中心的項目application.yml配置文件中添加配置信息,並在啓動類添加註解@EnableConfigServer,註冊中心就完事了。spring-boot
server: port: 8081 spring: application: name: spring-cloud-config-server cloud: config: server: git: #git配置文件的地址 uri: http://10.138.61.43:8081/root/spring-cloud.git username: root password: 12345678
運行起來能夠直接訪問 http://localhost:8081/spring-cloud-config/dev 來查看效果。倉庫中的配置文件會被轉換成web接口,訪問能夠參照如下的規則:gitlab
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.propertiespost
在我這裏,個人配置文件是spring-cloud-config-dev.yml,application就是spring-cloud-config,profile就是dev,label暫時不知道啥玩意。網站
五、而後咱們開始搞客戶端,來使用註冊中心的配置信息。同樣先添加引用
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
六、在application.yml配置文件和bootstrap.yml文件中分別加入下面兩段配置信息
server: port: 8082 spring: application: name: spring-cloud-config-client
spring: cloud: config: name: spring-cloud-config profile: dev #配置中心的具體地址 uri: http://localhost:8081/ #對應git的分支,若是配置中心使用的是本地存儲,則該參數無用 label: master
七、而後直接在使用的地方用註解使用便可。
@RestController public class HomeController { @Value("${testconfig}") private String testconfig; @RequestMapping("/index") public String index() { return "這裏是client-test:" + testconfig; } }
而後運行客戶端就能夠看到效果了。總的來說不復雜比較簡單的。可是手工修改配置文件後,你會發現註冊中心的信息更新了,可是在客戶端一直沒有更新。接下來就搞下讓他能及時更新下。
一、首先在客戶端添加pom引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
二、在使用配置文件的類添加註解@RefreshScope
//使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。 @RefreshScope @RestController public class HomeController { @Value("${testconfig}") private String testconfig; @RequestMapping("/index") public String index() { return "這裏是client-test:" + testconfig; } }
三、在application.yml文件中添加配置信息。不添加配置信息在接下來更新操做會出現404,注意....
management: endpoints: web: exposure: include: "*"
四、而後用post請求調用 http://localhost:8082/actuator/refresh 地址刷新下,在刷新網站就能夠實現了。
還有另一種方法實現自動刷新的方式:經過git的WebHooks來動態刷新。其實就是偵聽提交事件,而後執行相應的操做便可。
參考地址:
http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html