配置中心(十)Config:環境搭建

背景

    當一個系統中的配置文件發生改變的時候,咱們須要從新啓動該服務,才能使得新的配置文件生效,spring cloud config能夠實現微服務中的全部系統的配置文件的統一管理,並且還能夠實現當配置文件發生變化的時候,系統會自動更新獲取新的配置。git

工做原理

    Spring Cloud Config爲分佈式系統外部化配置提供了服務器端和客戶端的支持,它包括Config Server和Config Client兩部分。Config Server是一個可橫向擴展、集中式的配置服務器,它用於集中管理應用程序各個環境下的配置,默認使用Git存儲配置內容(也可以使用Subversion、本地文件系統或Vault存儲配置),所以能夠方便的實現對配置的版本控制與內容審計。Config Client 是Config Server的客戶端,用於操做存儲在Config Server中的配置屬性。github

環境搭建

示例代碼

創建config-bus-server子工程,啓動註冊中心Eureka:7001,這裏只演示git,其他的很少講

git倉庫配置中心

新增git倉庫配置中心,地址爲:https://github.com/kongliuyi/config.git,在倉庫中新增長以下配置文件:web

以上端點均可以映射到{application}-{profile}.properties這個配置文件,{application}表示微服務的名稱,{label}對應Git倉庫的分支,默認是 masterspring

其中config-client-dev.properties文件信息以下:bootstrap

 

配置中心服務端(Config-server)

 新增項目config-server

1.添加依賴

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

2.application.yml 配置文件

#端口號
server:
  port: 7010

###服務註冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka
spring:
  application:
    #註冊中心應用名稱
    name: config-server
  cloud:
    config:
      server:
        git:
          #git環境地址
          uri: https://github.com/kongliuyi/config.git
          ##搜索目錄
          search-paths: /

3.啓動ConfigServerApplication服務

package net.riking.springcloud.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer //開啓配置中心服務端
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

 啓動工程後,訪問:http://localhost:7010/config-client-dev.properties,能夠看到以下頁面:服務器

配置中心客戶端(Config-client)

新增項目config-client,怕大家把配置中心客戶端服務理解錯,因此這裏解釋一下。配置中心客戶端就是須要用到配置文件的服務,任何微服務均可以稱爲配置中心客戶端(註冊中心除外)app

1.添加依賴

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

2.bootstrap.yml 配置文件

除了默認的application.yml配置文件,還需增長一個bootstrap.yml的配置文,內容以下:分佈式

#服務啓動端口號
server:
  port: 9010

#服務名稱(服務註冊到eureka名稱)
spring:
  application:
    name: config-client   #對應config-server所獲取的配置文件的{application}
  cloud:
    config:
      #讀取後綴  對應config-server所獲取的配置文件的{profile}
      profile: dev
      label: master #讀取git倉庫分支 對應config-server所獲取的配置文件的{label}
      #讀取config-server註冊地址
      discovery:
        service-id: config-server
        enabled: true


#客戶端註冊進eureka服務列表內
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka

注:spring cloud有一個「引導上下文"的概念,這是主應用程序的父上下文。引導上下文負責從配置服務器加載配置屬性,以及解密外部配置文件中的屬性。和主應用程序加載
application.*(yml或 properties)中的屬性不一樣,引導上下文加載(bootstrap.*)中的屬性。配置在 bootstrap.*中的屬性有更高的優先級,所以默認狀況下它們不能被本地配置覆蓋。spring-boot

3.服務接口調用

package net.riking.springcloud.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config/client")
public class ConfigClientController {

    @Value("${name}")
    private String  name;

    @GetMapping
    public String name() {
        return  name ;
    }

}

4.啓動ConfigClientApplication服務

package net.riking.springcloud.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient  ///開啓對EurekaClient的支持,即:做爲Eureka客戶端,高版本可省略
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

}

5.驗證微服務

 啓動工程後,訪問:http://localhost:9010//config/client,能夠看到以下頁面:

動態刷新數據

不少場景下,須要在運行期間動態調整配置。若是配置發生了修改,微服務還會不會刷新呢?

1.修改git倉庫config-client-dev.properties配置文件

name=DEV-Charles

2.訪問http://localhost:7010/config-client-dev.properties,輸出以下

3.訪問http://localhost:9010/config/client,輸出以下

 

答案顯而易見是不會實時刷新,因此在SpringCloud中提供有兩種刷新數據方式,手動刷新配置文件和實時刷新配置文件兩種方式。其中手動方式採用actuator端點刷新數據,而實時刷新採用SpringCloud Bus消息總線

手動刷新

Config-client修改

 1.Maven依賴信息新增

       <!--自省和監控的集成功能,這裏的做用是爲配置信息手動刷新作監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.Bootstrap.xml新增

#開啓監控斷點
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.@RefreshScope註解新增

在Controller上添加註解@RefreshScope,添加這個註解的類會在配置更改時獲得特殊的處理

package net.riking.springcloud.configclient.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config/client")
@RefreshScope//手動刷新配置文件信息
public class ConfigClientController {

    @Value("${name}")
    private String  name;

    @GetMapping
    public String name() {
        return  name ;
    }

}

 啓動工程後,訪問:http://localhost:9010//config/client,能夠看到以下頁面:

4.驗證

 1.修改git倉庫config-client-dev.properties配置文件

name=Charles

 2.訪問http://localhost:7010/config-client-dev.properties,輸出以下

3.訪問http://localhost:9010/config/client,輸出以下

4.發送post請求:http://localhost:9010/refresh  進行手動刷新數據,輸出以下

5.再次訪問http://localhost:9010/config/client,輸出以下

 

自動刷新

 自動刷新我放入下篇文章,點擊進入

源碼分析

之後有時間整理

總結

做爲格式之後在總結。

配置中心技術有不少種,目前我只用過兩種,一種是攜程的阿波羅(apollo),另外一種就是本篇文章中得springCloud config,我將兩種對比一下,對於springCloud config,我推薦微服務少得小公司使用,而對通常微服務多的大公司,我推薦使用攜程的阿波羅,至於緣由,我會在日後開一篇apollo篇中講解

相關文章
相關標籤/搜索