爲Spring Cloud Config Server配置遠程git倉庫

簡介

雖然在開發過程,在本地建立git倉庫操做起來很是方便,可是在實際項目應用中,多個項目組須要經過一箇中心服務器來共享配置,因此Spring Cloud配置中心支持遠程git倉庫,以使分散的項目組更方便的進行協做。git

基礎環境

  • JDK 1.8
  • Maven 3.3.9
  • IntelliJ 2018.1
  • Git

項目源碼

Gitee碼雲web

配置遠程git倉庫

首先我在gitee上建立了一個遠程倉庫https://gitee.com/zxuqian/spring-cloud-config-remote專門用來存放配置文件,而後咱們會經過配置文件來訪問此倉庫。而後咱們把之前本地的配置文件遷移到此庫中。爲了測試效果,咱們把web-client.ymlmessage的值修改成:此條消息來自於遠程配置倉庫spring

配置configserver

如今在咱們以前的configserver中做一些配置上的改動。首先爲了保留以前的本地倉庫的配置,咱們把application.yml重命名爲application-local.ymlbootstrap

這個-local是一個profile,它的值是-後面的,即local,咱們能夠在bootstrap.yml中指定使用哪一個profile。好比實際項目中開發階段和生產階段的配置有所不一樣,因此會有application-development.ymlapplication-production.yml等兩種或以上的配置。服務器

而後新建一個application-remote.yml文件,添加以下配置內容:app

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/zxuqian/spring-cloud-config-remote
          username: 您的gitee用戶名
          password: 您的gitee密碼

由於是自用帳號的倉庫,因此就不提供帳號密碼了,改爲本身對應的。這裏uri配置了遠程git倉庫的地址。spring-boot

最後在bootstrap.yml中啓用咱們的remote profile:測試

spring:
  application:
    name: config-server
  profiles:
    active: remote

spring.profiles.active即指定了咱們的remote Profile,使用application-remote.yml配置文件。code

測試

使用spring-boot:run啓動咱們的config server,而後訪問http://localhost:8888/web-client/default,看到以下結果:server

{"name":"web-client","profiles":["default"],"label":null,"version":"cbef7d379ef01d68810c3fdc2105b2226ea6c611","state":null,"propertySources":[{"name":"https://gitee.com/zxuqian/spring-cloud-config-remote/web-client.yml","source":{"message":"此條消息來自於遠程配置倉庫","management.endpoints.web.exposure.include":"*"}}]}

message的值取自於遠程倉庫。這裏的web-client/default配置文件名/profile,由於咱們的web客戶端項目沒有其餘Profile,則默認值爲default,只有這樣寫全,才能夠訪問到web-client.yml的配置。

相關文章
相關標籤/搜索