SpringCloud學習筆記(八) Config分佈式配置中心

概述

分佈式系統面臨的配置問題

微服務意味着要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此係統中會出現大量的服務。由於每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態的配置管理設施是必不可少的。SpringCloud提供了ConfigServer來解決這個問題,我們每一個微服務自己帶着一個application.ymI,上百個配置文件的管理…/(ToT)/~~

在這裏插入圖片描述

是什麼

SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一箇中心化的外部配置。

SpringCloud Config分爲服務端和客戶端兩部分。

服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口。

客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。

能幹嘛

  1. 集中管理配置文件
  2. 不同環境不同配置,動態化的配置更新,分環境部署,比如dev/test/prod/beta/release
  3. 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息
  4. 當配置發生變動時,服務不需要重啓即可感知到配置的變化並應用新的配置
  5. 將配置信息以REST接口的形式暴露

與GitHub整合配置

由於SpringCloud Config默認使用Git來存儲配置文件(也有其它方式,比如支持SVN和本地文件),但最推薦的還是Git,而且使用的是http/https訪問的形式

Config 服務端

GitHub新建倉庫 上傳yml文件

用自己的GitHub賬號在GitHub上新建一個名爲microservicecloud-config的新Repository
由上一步獲得SSH協議的git地址
本地硬盤目錄上新建git倉庫並clone(本地倉庫路徑 D:\GIT_GitHub)

application.yml

在本地D:\GIT_GitHub\microservicecloud-config裏面新建一個application.yml,文件格式必須是utf-8

spring:
  profiles:
    active:
    - dev
---
spring:
  profiles: dev     # 開發環境
  application:
    name: microservicecloud-config-demo-dev
---
spring:
  profiles: test     # 測試環境
  application:
    name: microservicecloud-config-demo-test
# 請保存爲UTF-8格式

將YML文件推送到github上

$ pwd
/d/GIT_GitHub/microservicecloud-config
$ git add .
$ git commit -m "init file"
$ git push origin master

配置中心模塊

新建Module模塊microservicecloud-config-3344,它即爲Cloud的配置中心模塊

POM

<!--主要添加以下依賴-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

YML

server:
  port: 3344

spring:
  application:
    name: microservicecloud-config-3344
  cloud:
    config:
      server:
        git:
          uri: [email protected]:用戶名/microservicecloud-config.git  # github上的地址

主啓動類

主啓動類Config_3344_StartSpringCloudApp添加註解@EnableConfigServer

hosts文件修改

C:\Windows\System32\drivers\etc下的hosts文件

127.0.0.1		config-3344.com

測試

測試通過Config微服務是否可以從GitHub上獲取配置內容

啓動微服務3344
http://config-3344.com:3344/application-dev.yml
http://config-3344.com:3344/application-test.yml
http://config-3344.com:3344/application-xx.yml(不存在的配置)

配置的讀取規則

The HTTP service has resources in the form:

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

where the “application” is injected as the spring.config.name in the SpringApplication (i.e. what is normally
“application” in a regular Spring Boot app), “profile” is an active profile (or comma-separated list of properties), and
“label” is an optional git label (defaults to “master”.)
Spring Cloud Config Server pulls configuration for remote clients from a git repository (which must be provided)

/{application}/{profile}[/{label}] 形式例如:
http://config-3344.com:3344/application/dev/master
返回的是json格式的數據

/{application}-{profile}.yml 形式例如:
http://config-3344.com:3344/application-dev.yml

/{label}/{application}-{profile}.yml 形式例如:
http://config-3344.com:3344/master/application-dev.yml

Config 客戶端

GitHub上傳yml

在本地D:\GIT_GitHub\microservicecloud-config路徑下新建文件microservicecloud-config-client.yml

spring:
  profiles:
    active:
    - dev
---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-test.com:7001/eureka/

將上一步提交到GitHub中

git add .
git commit -m "test config"
git push origin master

配置客戶端模塊

新建模塊microservicecloud-config-client-3355

POM

<!--SpringCloud Config 客戶端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

bootstrap.yml

applicaiton.yml是用戶級的資源配置項
bootstrap.yml是系統級的,優先級更高

Spring Cloud會創建一個Bootstrap Context,作爲Spring應用的Application Context父上下文

初始化的時候,Bootstrap Context負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的Environment
Bootstrap屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。Bootstrap contextApplication Context有着不同的約定,所以新增了一個bootstrap.yml文件, 保證Bootstrap ContextApplication Context配置的分離。

內容

spring:
  cloud:
    config:
      name: microservicecloud-config-client   # 需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: dev      # 本次訪問的配置項
      label: master
      uri: http://config-3344.com:3344      # 本微服務啓動後 先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址

application.yml

實際上bootstrap.yml配置好了之後不必再配置application.yml

spring:
  application:
    name: microservicecloud-config-client

hosts

C:\Windows\System32\drivers\etc下的hosts文件

127.0.0.1		client-config.com

ConfigClientRest.java

新建rest類ConfigClientRest.java,驗證是否能從GitHub上讀取配置

package com.demo.springCloud.rest;

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

@RestController
public class ConfigClientRest {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServers;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/config")
    public String getConfig(){
        String str ="applicationName: "+applicationName+"\t eurekaServers : "+eurekaServers+"\t port: "+port;
        System.out.println("******str: "+ str);
        return "applicationName: "+applicationName+"\t eurekaServers:" +eurekaServers+"\t port: "+port;
    }
}

主啓動類

有個基本的@SpringBootApplication註解就可以

測試

啓動Config配置中心3344微服務並自測 http://config-3344.com:3344/application-dev.yml
啓動3355作爲Client準備訪問 http://client-config.com:8201/config http://client-config.com:8202/config
bootstrap.yml裏面的profile值是什麼,決定從github上讀取什麼

成功實現了客戶端3355訪問SpringCloud Config3344通過GitHub獲取配置信息

SpringCloud Config 配置實戰

Config服務端配置OK且測試通過,我們可以和config+GitHub進行配置修改並獲得內容。
此時我們做一個 eureka服務 + 一個Dept訪問的微服務,將兩個微服務的配置統一由github獲得實現統一配置分佈式管理,完成多環境的變更。

Git配置文件本地配置

GitHub上傳yml

在本地D:\GIT_GitHub\microservicecloud-config路徑下新建文件microservicecloud-config-eureka-client.yml

spring:
  profiles:
    active:
    - dev
---
server:
  port: 7001
spring:
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: euraka7001.com
  client:
    register-with-eureka: false #false表示不向註冊中心註冊自己。只有客戶端才向Eureka進行註冊
    fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 7001
spring:
  profiles: test
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: euraka7001.com
  client:
    register-with-eureka: false #false表示不向註冊中心註冊自己。只有客戶端才向Eureka進行註冊
    fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

在本地D:\GIT_GitHub\microservicecloud-config路徑下新建文件microservicecloud-config-dept-client.yml

# 可類比複製dept-8001模塊的配置文件,新增或修改如下部分
spring:
  profiles:
    active:
    - dev
---
spring:
  profiles: dev
  datasource:
  	url: 連接cloudDB01數據庫

eureka:
  client: 
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
spring:
  profiles: test
  datasource:
  	url: 連接cloudDB02數據庫

eureka:
  client: 
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

將上一步提交到GitHub中

git add .
git commit -m "two new file"
git push origin master

Config版的eureka服務端

新建工程microservicecloud-config-eureka-client-7001

POM

<!--SpringCloud Config 配置-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka-server服務端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--修改後立即生效,熱部署-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client   # 需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: dev      # 本次訪問的配置項
      label: master
      uri: http://config-3344.com:3344	# 本微服務啓動後 先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址

application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

主啓動類

Config_Git_EurekaServerApplication

@SpringBootApplication
@EnableEurekaServer //EurekaServer服務器端啓動類,接受其它微服務註冊進來

測試

先啓動microservicecloud-config-3344微服務,保證Config總配置是OK的
再啓動microservicecloud-config-eureka-client-7001微服務
http://eureka7001.com:7001/

Config版的dept微服務

參考之前的8001拷貝後新建工程【microservicecloud-config-dept-client-8001】

POM

<!--SpringCloudConfig配置-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client   # 需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: test      # 本次訪問的配置項
      label: master
      uri: http://config-3344.com:3344      # 本微服務啓動後 先去找3344號服務,通過SpringCloudConfig獲取GitHub的服務地址

application.yml

spring:
  application:
    name: microservicecloud-config-dept-client

主啓動類DeptProvider8001_App

@SpringBootApplication
@EnableEurekaClient     //本服務啓動後會自動註冊進Eureka服務中
@EnableDiscoveryClient  //服務發現

測試

啓動順序:config-3344 -> eureka-client-7001 -> config-dept-client-8001

test配置默認訪問 http://localhost:8001/dept/list 可以看到數據庫配置是02
換配置成dev http://localhost:8001/dept/list 可以看到數據庫配置是01

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章