SpringCloud08:SpringCloud Config分布式配置中心

本系列對(duì)應(yīng)的是尚硅谷周陽(yáng)Spring Cloud的思維導(dǎo)圖整理的筆記,用來(lái)方便自己后面的知識(shí)點(diǎn)回顧。分別以每個(gè)知識(shí)點(diǎn)作為一篇文章詳細(xì)講述。

知識(shí)點(diǎn)傳送門(mén):
項(xiàng)目源碼

一、概述

1.分布式系統(tǒng)面臨的--配置問(wèn)題

微服務(wù)意味著要將單體應(yīng)用中的業(yè)務(wù)拆分成一個(gè)個(gè)子服務(wù),每個(gè)服務(wù)的粒度相對(duì)較小,因此系統(tǒng)中會(huì)出現(xiàn)大量的服務(wù)。由于每個(gè)服務(wù)都需要必要的配置信息才能運(yùn)行,所以一套集中式的動(dòng)態(tài)的配置管理設(shè)施是必不可少的。SpringCloud提供了ConfigServer來(lái)解決這個(gè)問(wèn)題,我們每一個(gè)微服務(wù)自己帶著一個(gè)application.yml,上百個(gè)配置文件的管理。

2.是什么

Snip20190108_1.png

SpringCloud Config為微服務(wù)架構(gòu)中的微服務(wù)提供集中化的外部配置支持,配置服務(wù)器為各個(gè)不同微服務(wù)應(yīng)用的所有環(huán)境提供了一個(gè)中心化的外部配置

SpringCloud Config分為服務(wù)端和客戶端兩部分。

服務(wù)端也稱為分布式配置中心,它是一個(gè)獨(dú)立的微服務(wù)應(yīng)用,用來(lái)連接配置服務(wù)器并為客戶端提供獲取配置信息,加密/解密信息等訪問(wèn)接口。

客戶端則是通過(guò)指定的配置中心來(lái)管理應(yīng)用資源,以及與業(yè)務(wù)相關(guān)的配置內(nèi)容,并在啟動(dòng)的時(shí)候從配置中心獲取和加載配置信息,配置服務(wù)器默認(rèn)采用git來(lái)存儲(chǔ)配置信息,這樣就有助于對(duì)環(huán)境配置進(jìn)行版本管理,并且可以通過(guò)git客戶端工具來(lái)方便的管理和訪問(wèn)配置內(nèi)容。

3.能干嘛

  1. 集中管理配置文件
  2. 不同環(huán)境不同配置,動(dòng)態(tài)化的配置更新,分環(huán)境部署比如dev/test/prod/beta/release
  3. 運(yùn)行期間動(dòng)態(tài)調(diào)整配置,不再需要在每個(gè)服務(wù)部署的機(jī)器上編寫(xiě)配置文件,服務(wù)會(huì)向配置中心統(tǒng)一拉取配置自己的信息
  4. 當(dāng)配置發(fā)生變動(dòng)時(shí),服務(wù)不需要重啟即可感知到配置的變化并應(yīng)用新的配置
  5. 將配置信息以REST就接口的形式暴露

4.與GitHub整合配置

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

二、SpringCloud Config服務(wù)端配置

1.用自己的GitHub賬號(hào)在GitHub上新建一個(gè)名為microservicecloud-config的新Respository

2.由上一步獲得SSH協(xié)議的git地址

3.本地硬盤(pán)目錄上新建git倉(cāng)庫(kù)并clone

4.在本地microservicecloud-config新建一個(gè)application.yml

保存格式必須為UTF-8

spring:
    profiles:
        active:
        - dev

---
spring:
    profiles: dev #開(kāi)發(fā)環(huán)境
    application:
        name: microservicecloud-config-atguigu-dev

---
spring:
    profiles: test #測(cè)試環(huán)境
    application:
        name: microservicecloud-config-atguigu-test
# 請(qǐng)保存為UTF-8格式

5.將上一步的YML文件推送到github上

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

7.POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>microservicecloud-config-3344</artifactId>


    <dependencies>
        <!-- springCloud Config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 避免Config的Git插件報(bào)錯(cuò):org/eclipse/jgit/api/TransportConfigCallback -->
        <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>4.10.0.201712302008-r</version>
        </dependency>
        <!-- 圖形化監(jiān)控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 熔斷 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 熱部署插件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>

8.YML

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:zzyybs/microservicecloud-config.git #GitHub上面的git倉(cāng)庫(kù)名字

9.主啟動(dòng)類(lèi)Config_3344_StartSpringCloudApp

package com.atguigu.springcloud;

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

@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
    public static void main(String[] args)
    {
        SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
    }
}

10.修改host文件,增加映射

127.0.0.1 config-3344.com

11.測(cè)試通過(guò)Config微服務(wù)是否可以從GitHub上獲取配置內(nèi)容

  1. 啟動(dòng)微服務(wù)3344
  2. http://config-3344.com:3344/application-dev.yml
  3. http://config-3344.com:3344/application-test.yml
  4. http://config-3344.com:3344/application-xxx.yml(不存在的配置)

12.配置讀取規(guī)則

訪問(wèn)配置規(guī)則地址,通常采用2,即上面的讀取方式

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml

成功實(shí)現(xiàn)了用SpringCloud Config通過(guò)GitHub獲取配置信息

三、SpringCloud Config客戶端配置與測(cè)試

1.在倉(cāng)庫(kù)新建microservicecloud-config-client.yml

2.microservicecloud-config-client.yml內(nèi)容

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/

3.提交microservicecloud-config-client.yml文件到GitHub上

4.新建microservicecloud-config-client-3355

5.修改POM文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>microservicecloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>microservicecloud-config-client-3355</artifactId>


    <dependencies>
        <!-- SpringCloud Config客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

6.bootstrap.yml

application.yml是用戶級(jí)的資源配置項(xiàng)
bootstrap.yml是系統(tǒng)級(jí)的,優(yōu)先級(jí)更加高

Spring Cloud會(huì)創(chuàng)建一個(gè) 'Bootstrap Context',作為Spring應(yīng)用的'Application Context'的父上下文,初始化的時(shí)候,'Bootstrap Context'負(fù)責(zé)從外部源加載配置屬性并解析配置。這兩個(gè)上下文共享一個(gè)從外部獲取的'Environment'。'Bootstrap'屬性有高優(yōu)先級(jí)。默認(rèn)情況下,它們不會(huì)被本地配置負(fù)載。'Bootstrap Context'和'Application Context'有著不同的約定,所以新增了一個(gè)'bootstrap.yml'文件,保證'Bootstrap Context'和'Application Context'配置的分離。

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要從github上讀取的資源名稱,注意沒(méi)有yml后綴名
      profile: test   #本次訪問(wèn)的配置項(xiàng)
      label: master   
      uri: http://config-3344.com:3344  #本微服務(wù)啟動(dòng)后先去找3344號(hào)服務(wù),通過(guò)SpringCloudConfig獲取GitHub的服務(wù)地址

7.application.yml

spring:
  application:
    name: microservicecloud-config-client

8.修改host文件,增加映射

127.0.0.1 client-config.com

9.新建rest類(lèi),驗(yàn)證是否能從GitHub上讀取配置

ConfigClientRest.java

package com.atguigu.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;
    }
}

10.主啟動(dòng)類(lèi)ConfigClient_3355_StartSpringCloudApp

11.測(cè)試

dev: http://client-config.com:8201/config
test: http://client-config.com:8202/config
成功實(shí)現(xiàn)了客戶端3355訪問(wèn)SpringCloud_Config3344通過(guò)GitHub獲取配置信息

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容