詳解spring cloud config整合gitlab搭建分布式的配置中心,cloudgitlab

來源:互聯網
上載者:User

詳解spring cloud config整合gitlab搭建分布式的配置中心,cloudgitlab

在前面的部落格中,我們都是將設定檔放在各自的服務中,但是這樣做有一個缺點,一旦配置修改了,那麼我們就必須停機,然後修改設定檔後再進行上線,服務少的話,這樣做還無可厚非,但是如果是成百上千的服務了,這個時候,就需要用到分布式的組態管理了。而spring cloud config正是用來解決這個問題而生的。下面就結合gitlab來實現分布式配置中心的搭建。spring cloud config配置中心由server端和client端組成,

前提:在gitlab中的工程下建立一個設定檔configserver-dev.properties

一、配置Server

1、添加依賴

<dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-config-server</artifactId>     </dependency> 

2、在Application主類開啟支援

@EnableConfigServer 

3、配置application.yml檔案

server:  port: 8888 spring:  application:   name: config  cloud:   config:    server:     git:      uri: https://gitlab.xxx.com/xxxxx/xxxxx.git   # 配置gitlab倉庫的地址,注意,此處必須以.git結尾      search-paths: /config-repo # gitlab倉庫地址下的相對位址,可以配置多個,用,分割。      username: your username                       # gitlab倉庫的帳號      password: your password                       # gitlab倉庫的密碼 

注意:如果設定檔放置在Git存放庫的根目錄下,則無需使用searchPaths參數,本例中的設定檔在config-repo目錄中,因此使用searchPaths參數提示Config伺服器搜尋config-repo子目錄

4、啟動server,並在瀏覽器輸入http://localhost:8888/configserver/dev/master

{    "name": "configserver",   "profiles": [     "dev"   ],   "label": "master",   "version": "073cda9ce85a3eed00e406f4ebcc4651ee4d9b19",   "state": null,   "propertySources": [     {       "name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties",       "source": {         "name": "chhliuxyh",         "hello": "i'm the king of the world!!!",         "profile": "profile-default"       }     }   ]  } 

可以看到server端已經可以從gitlab上讀取到設定檔了。可以通過如下表單中的方式訪問gitlab上的資源

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties 

例如在瀏覽器中輸入:http://localhost:8888/configserver-dev.yml,結果如下:

hello: i'm the king of the world!!! name: chhliuxyh profile: profile-default 

二、配置用戶端

1、添加pom依賴

<dependency>       <groupId>org.springframework.cloud</groupId>       <artifactId>spring-cloud-starter-config</artifactId>     </dependency>     <dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-web</artifactId>     </dependency> 

2、配置bootstrap.yml檔案

注意:此處的設定檔需要放在bootstrap.properties或者是bootstrap.yml檔案中,因為config的相關配置會先於application.properties,而bootstrap.properties的載入也是先於application.properties

server:  port: 8889 spring:  application:   name: configserver  # 必須與設定檔的首碼一致,例如此處我們的設定檔名是configserver-dev.properties,則此處需配置成configserver  cloud:   config:    uri: http://localhost:8888/ //配置spring cloud config服務端的url    profile: dev           # 指定profile    label: master           # 指定gitlab倉庫的分支 

3、驗證用戶端

在用戶端新增一個Controller

package com.chhliu.springcloud.config;  import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;  @SpringBootApplication @RestController @RefreshScope //註解@RefreshScope指示Config用戶端在伺服器配置改變時,也重新整理注入的屬性值 public class SpringcloudConfigClientApplication {    public static void main(String[] args) {     SpringApplication.run(SpringcloudConfigClientApplication.class, args);   }    @Value("${hello}") // 讀取gitlab設定檔中的屬性,如果我們讀取到了值,說明用戶端是OK的   private String profile;    @GetMapping("/hello")   public String hello() {     return this.profile;   } } 

在瀏覽器中訪問:http://localhost:8889/hello,結果如下:

i'm the king of the world!!! 

說明用戶端已經可以從服務端擷取到值了。

三、動態重新整理

無需重新啟動用戶端,即可更新Spring Cloud Config管理的配置

1、更新gitlab倉庫中configserver-dev.properties設定檔中hello對應的屬性值

2、訪問http://localhost:8888/configserver/dev/master,發現server端內容已經更新

3、對Conf用戶端發一個POST請求http://localhost:8889/refresh,返回200 OK。再次訪問http://localhost:8889/hello,可見在並未重啟用戶端服務的情況下,讀到的屬性值已經動態更新

PS:要想實現動態重新整理,需要在pom檔案中添加以下starter

<dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-actuator</artifactId>     </dependency> 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.