八、SpringCloud——Config 分佈式配置中心Demo

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

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

3)怎麼玩
SpringCloud Config分爲服務端和客戶端兩部分。
(1)服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口
(2)客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。

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

5)與GitHub整合配置

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

2 SpringCloud Config服務端配置(配置服務器3344連接遠程github獲取配置文件配置)
1)用自己的GitHub賬號在GitHub上新建一個名爲microservicecloud-config的新Repository

2)由上一步獲得SSH協議的git地址
[email protected]:manglilao/microservicecloud-config.git
在這裏插入圖片描述

3)本地硬盤目錄上新建git倉庫並clone
本地地址:C:\dev\springcloud-config
git命令:git clone https://github.com/manglilao/microservicecloud-config.git

4)在本地D:\44\mySpringCloud\microservicecloud-config裏面新建一個application.yml
yml文件內容

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

保存格式必須爲UTF-8,注意保存爲yml文件類型

5)將上一步的yml文件推送到github上
git add
git commit -m “init yml”
git push origin master
在這裏插入圖片描述
GitHub結果
在這裏插入圖片描述
6)新建Module模塊microservicecloud-config-3344它即爲Cloud的配置中心模塊

7)POM

<dependencies>
   <!-- springCloud Config -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
   </dependency>
   <!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback   idea不用-->
   <dependency>
       <groupId>org.eclipse.jgit</groupId>
       <artifactId>org.eclipse.jgit</artifactId>
       <version>4.10.0.201712302008-r</version>
   </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> 

8)配置文件


server:
  port: 3344

spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/manglilao/microservicecloud-config.git

  1. 主啓動類Config_3344_StartSpringCloudApp 添加@EnableConfigServer註解
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
  public static void main(String[] args)
  {
   SpringApplication.run(Config_3344_StartSpringCloudApp.class,args);
  }
}

  1. windows下修改hosts文件,增加映射
    127.0.0.1 config-3344.com

  2. 測試通過Config微服務是否可以從GitHub上獲取配置內容
    啓動微服務3344,訪問如下
    http://config-3344.com:3344/application-dev.properties
    http://config-3344.com:3344/application-test.properties
    在這裏插入圖片描述
    http://config-3344.com:3344/application-xxx.properties(不存在的配置)
    在這裏插入圖片描述

12)配置讀取規則
在這裏插入圖片描述
(1)/{application}-{profile}.yml
http://config-3344.com:3344/application-dev.yml
http://config-3344.com:3344/application-test.yml
http://config-3344.com:3344/application-xxx.yml(不存在的配置)
(2)/{application}/{profile}[/{label}]
http://config-3344.com:3344/application/dev/master
http://config-3344.com:3344/application/test/master
http://config-3344.com:3344/application/xxx/master
(3)/{label}/{application}-{profile}.yml
http://config-3344.com:3344/master/application-dev.yml
http://config-3344.com:3344/master/application-test.yml

13)成功實現了用SpringCloud Config通過GitHub獲取配置信息

3 SpringCloud Config客戶端配置與測試(3355客戶端獲取3344配置服務端的遠程配置信息)

1)在本地git庫C:\dev\springcloud-config路徑下新建文件
microservicecloud-config-client.yml

2)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/

3)將上一步提交到GitHub中
git add .
git commit -m " client.yml"
git push origin master

4)新建microservicecloud-config-client-3355 子maven moudle

5)POM文件

 <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> 

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

Spring Cloud會創建一個Bootstrap Context,作爲Spring應用的Application Context的父上下文。初始化的時候,Bootstrap Context負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的EnvironmentBootstrap屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。 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的服務地址
  1. application.yml
spring:
  application:
    name: microservicecloud-config-client
  1. windows下修改hosts文件,增加映射
    127.0.0.1 client-config.com

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

@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;
  }
}
 
  1. 主啓動類ConfigClient_3355_StartSpringCloudApp
@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp
{
  public static void main(String[] args)
  {
   SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class,args);
  }
}
  1. 測試
    (1)啓動Config配置中心3344微服務並自測 http://config-3344.com:3344/application-dev.yml
    (2)啓動3355作爲Client準備訪問
    (3)bootstrap.yml裏面的profile值是什麼,決定從github上讀取什麼
    a)假如目前是 profile: dev
    dev默認在github上對應的端口就是8201 http://client-config.com:8201/config
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    b ) 假如目前是 profile: test
    test默認在github上對應的端口就是8202 http://client-config.com:8202/config
    在這裏插入圖片描述
    在這裏插入圖片描述
  2. 成功實現了客戶端3355訪問SpringCloud Config3344通過GitHub獲取配置信息

4 Config配置實戰
一、目前情況
1 Config服務端配置配置OK且測試通過,我們可以和config+GitHub進行配置修改並獲得內容

2 此時我們做一個eureka服務+一個Dept訪問的微服務,將兩個微服務的配置統一由於github獲得實現統一配置分佈式管理,完成多環境的變更

二、步驟

1)Git配置文件本地配置
a)本地git庫C:\dev\springcloud-config中新建microservicecloud-config-eureka-client.yml 內容如下

spring: 
  profiles: 
    active: 
    - dev
---
server: 
  port: 7001 #註冊中心佔用7001端口,冒號後面必須要有空格
   
spring: 
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒號後面必須要有空格
  client: 
    register-with-eureka: false #當前的eureka-server自己不註冊進服務列表中
    fetch-registry: false #不通過eureka獲取註冊信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/
---
server: 
  port: 7001 #註冊中心佔用7001端口,冒號後面必須要有空格
   
spring: 
  profiles: test
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒號後面必須要有空格
  client: 
    register-with-eureka: false #當前的eureka-server自己不註冊進服務列表中
    fetch-registry: false #不通過eureka獲取註冊信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/


b)本地git庫C:\dev\springcloud-config中新建新建文件microservicecloud-config-dept-client.yml 內容如下

spring: 
  profiles:
    active:
    - dev
--- 
server:
  port: 8001
spring: 
   profiles: dev
   application: 
    name: microservicecloud-config-dept-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200 
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客戶端註冊進eureka服務列表內
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: atguigu-microservicecloud-springcloudconfig01
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 8001
spring: 
   profiles: test
   application: 
    name: microservicecloud-config-dept-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200  
  
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客戶端註冊進eureka服務列表內
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: atguigu-microservicecloud-springcloudconfig02
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

c) 提交到github
git add .
git commit -m " test config"
git push origin master

2)Config版的eureka服務端
a)新建工程microservicecloud-config-eureka-client-7001
b)POM文件

<dependencies>
    <!-- SpringCloudConfig配置 -->
    <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
    </dependency> 
    <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>   
  </dependencies>

c) bootstrap.yml


spring: 
  cloud: 
    config: 
      name: microservicecloud-config-eureka-client     #需要從github上讀取的資源名稱,注意沒有yml後綴名
      profile: dev 
      label: master 
      uri: http://config-3344.com:3344      #SpringCloudConfig獲取的服務地址

d) application.yml

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


e) 主啓動類Config_Git_EurekaServerApplication

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

f)測試
先啓動microservicecloud-config-3344微服務,保證Config總配置是OK的
再啓動microservicecloud-config-eureka-client-7001微服務
http://eureka7001.com:7001/ 出現eureak主頁表示成功啓動
在這裏插入圖片描述

3)Config版的dept微服務
承接 https://mp.csdn.net/mdeditor/96200728#
a)參考之前的provider8001拷貝後新建工程microservicecloud-config-dept-client-8001

b)POM文件

  <dependencies>
        <!-- SpringCloudConfig配置 -->
        <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-eureka</artifactId>
        </dependency>
        <dependency><!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->
            <groupId>com.tang</groupId>
            <artifactId>micro-service-cloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</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>

c)新建bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要從github上讀取的資源名稱,注意沒有yml後綴名
      #profile配置是什麼就取什麼配置dev or test
      #profile: dev
      profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig獲取的服務地址

d)新建application.yml

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

e) 主啓動類及其它一套業務邏輯代碼
主啓動類

@SpringBootApplication
@EnableEurekaClient //本服務啓動後會自動註冊進eureka服務中
@EnableDiscoveryClient //服務發現
public class DeptProvider8001_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptProvider8001_App.class, args);
  }
}
 

工程最終結構

在這裏插入圖片描述

f)測試
(1)test配置默認訪問
http://localhost:8001/dept/list
可以看到數據庫配置是02
在這裏插入圖片描述
(2)本地換配置成dev
http://localhost:8001/dept/list
可以看到數據庫配置是01
在這裏插入圖片描述

配置說明

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要從github上讀取的資源名稱,注意沒有yml後綴名
      #profile配置是什麼就取什麼配置dev or test
      #profile: dev
      profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig獲取的服務地址

主要看bootstrap.yml文件裏面的
profile: 屬性具體值是什麼,從而確定它能從github上取得什麼樣的配置
假如配置dev左圖,如果配置test那就找右圖,具體各自數據庫不同,從而依據配置得到分佈式配置的目的

在這裏插入圖片描述
在這裏插入圖片描述
可以對本地庫的文件進行修改從而修改github配置

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