1、概述java
一、分佈式系統面臨的問題mysql
微服務意味着要將單體應用中的業務拆分紅一個個子服務,每一個服務的粒度相對較小,所以系統中會出現大量的服務。因爲每一個服務都須要必要的配置信息才能運行,因此一套集中式的、動態的配置管理設施是必不可少的。SpringCloud提供了ConfigServer來解決這個問題,咱們每個微服務本身帶着一個application.yml,上百個配置文件的管理就很複雜了。git
二、Config是什麼?github
如圖,ABC三個微服務的配置文件放到遠程Git上,同步到Local Git由Config Server統一管理。web
SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不一樣微服務應用的全部環境提供了一個外部化的中心配置。spring
SpringCloud Config分爲服務端和客戶端兩部分。服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來鏈接配置服務器,併爲客戶端提供獲取配置信息,加密/解密信息等訪問接口。客戶端則是經過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息,配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,而且能夠經過git客戶端工具來方便的管理和訪問配置內容。sql
三、Config能幹什麼?數據庫
集中管理配置文件apache
不一樣環境不一樣配置,動態化的配置更新,分環境部署好比dev/test/prod/beta/releasebootstrap
運行期間動態調整配置,不載須要在每一個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置本身的信息
當配置發生變化時,服務不須要重啓,便可感知到配置的變化並應用新的配置
將配置信息以rest接口的形式暴露
四、與GitHub整合配置
因爲SpringCloud Config默認使用Git來存儲配置文件(也有其餘方式,好比支持svn和本地文件),但最推薦的仍是Git,並且使用的是http/https訪問的形式。
2、SpringCloud Config服務端配置
一、建立GitHub倉庫而且clone到本地
(1)用本身的GitHub帳號在GitHub上新建一個名爲microservicecloud-config的新Repository
(2) 由上一步得到SSH協議的Git地址:拷貝出來的(https://github.com/wrenlei/microservicecloud-config.git)
(3)在本地硬盤目錄上新建git倉庫並clone
新建目錄:D:\20200320\mySpringCloud,右鍵->點擊Git Base Here
執行命令:$ git clone https://github.com/wrenlei/microservicecloud-config.git,從服務器clone項目到本地
結果如圖,項目microservicecloud-config已經clone下來了,而且下面有隱藏的.git文件夾,包含了git的信息
二、模擬運維工程師在本地的git目錄操做
(1)在本地D:\20200320\mySpringCloud\microservicecloud-config下新建 application.yml,必定保存爲UTF-8格式
spring: profiles: active: - dev: --- spring: profiles: dev #開發環境 application: name: microservicecloud-config-atguigu-dev --- spring: profiles: test #測試環境 application: name: microservicecloud-config-atguigu-test #請保存爲UTF-8格式
(2)將該文件推送到GitHub上
在git命令窗口進入到microservicecloud-config目錄,執行 cd microservicecloud-config,git status 查看一下發現文件已經發生變化
執行命令:git add .(將當前目錄下修改的全部代碼從工做區添加到暫存區 . 表明當前目錄)
執行命令:git commit -m "init file"(將緩存區內容添加到本地倉庫,引號中是註釋)
執行命令:git push origin master(將本地版本庫推送到遠程服務器)
執行過程當中可能要認證,$ git config --global user.email "xxx@xxx.com" 、$ git config --global user.name "xxx",根據提示操做便可。
刷新GitHub,發現application.yml已經提交到了遠程庫。
三、新建微服務 microservicecloud-config-3344
pom.xml
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件報錯:org/eclipse/jgit/api/TransportConfigCallback -->
<!--
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>
-->
<!-- 本身定義的api -->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Ribbon相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- feign相關 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- hystrix和 hystrix-dashboard相關-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</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>
application.yml
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: https://github.com/wrenlei/microservicecloud-config.git #GitHub上面的git倉庫名字
建立主啓動類 Config_3344_StartSpringCloudApp.java
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); } }
修改hosts文件,添加映射:127.0.0.1 config-3344.com
四、測試經過Config微服務是否能夠從GitHub上獲取配置信息
(1)啓動微服務 microservicecloud-config-3344,並貼出GitHub上的配置,方便進行對比
輸入:http://config-3344.com:3344/application-dev.yml 測試能夠正確讀取GitHub上信息
輸入:http://config-3344.com:3344/application-test.yml 測試能夠正確讀取GitHub上信息
輸入:http://config-3344.com:3344/application-1234.yml(不存在的配置),只能讀到GitHub上配置的頭文件
成功實現了用SpringCloud Config經過GitHub獲取配置信息。
五、配置讀取規則
(1)/{application} -{profile}.yml
(2)/{application} /{profile}[/{label}]
舉例:http://config-3344.com:3344/application/dev
舉例:http://config-3344.com:3344/application/test
舉例:http://config-3344.com:3344/application/dev/master
舉例:http://config-3344.com:3344/application/test/master
(3)/{label}/{application}-{profile}.yml
舉例:http://config-3344.com:3344/master/application-dev.yml
舉例:http://config-3344.com:3344/master/application-test.yml
(4)/{application} -{profile}.properties
(5)/{label}/{application}-{profile}.properties
三、SpringCloud Config客戶端配置
一、在D:\20200320\mySpringCloud\microservicecloud-config下新建microservicecloud-config-client.yml,保存爲utf-8
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中
三、 新建microservicecloud-config-client-3355
pom.xml
<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> <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> <!-- Ribbon相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- feign相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- hystrix和 hystrix-dashboard相關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</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>
bootstrap.yml
spring: cloud: config: name: microservicecloud-config-client #須要從GitHub上讀取的資源名稱,注意沒有yml後綴名 profile: dev #本次訪問的配置項 label: master uri: http://config-3344.com:3344 #本微服務啓動後先去找3344號服務,經過SpringCloudConfig獲取GitHub的服務地址
application.yml
spring:
application:
name: microservicecloud-config-client
window下增長hosts映射:C:\Windows\System32\drivers\etc\hosts:127.0.0.1 client-config.com
新建ConfigClientRest.java類,驗證是否能從GitHub上讀取配置
package com.atguigu.springcloud.rest; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping;
@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 str; } }
新建主啓動類 ConfigClient_3355_StartSpringCloudApp.java
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient_3355_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args); } }
四、測試,啓動Config配置中心3344微服務和客戶端配置3355微服務
(1)自測:http://config-3344.com:3344/application-dev.yml,訪問成功
(2)bootstrap.yml裏面的profile值是什麼,決定從github上讀取什麼
bootstrap.yml裏面的profile值是dev,則讀取dev的配置,默認在github上的端口號是8201:http://client-config.com:8201/config
bootstrap.yml裏面的profile值是test,則讀取test的配置,test默認在github上的端口號是8202:http://client-config.com:8202/config
4、配置實戰
經過上面的演示,Config服務端配置完成,且測試經過,咱們能夠和Config+GitHub進行配置修改並得到內容。此時咱們作一個eureka服務+一個Dept訪問的微服務,將兩個微服務的配置統一由GitHub獲取 實現統一配置分佈式管理,完成多環境的變動。
一、Git配置文件本地配置
(1)在D:\20200320\mySpringCloud\microservicecloud-config路徑下新建microservicecloud-config-eureka-client.yml,保存爲utf-8
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 #false表示不向註冊中心註冊本身。 fetch-registry: false #false表示本身端就是註冊中心,個人職責就是維護服務實例,並不須要去檢索服務 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 #false表示不向註冊中心註冊本身。 fetch-registry: false #false表示本身端就是註冊中心,個人職責就是維護服務實例,並不須要去檢索服務 service-url: defaultZone: http://eureka7001.com:7001/eureka/
(2)在D:\20200320\mySpringCloud\microservicecloud-config路徑下新建microservicecloud-config-dept-client.yml,保存爲utf-8
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 # mysql驅動包 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 # mybatis配置文件所在路徑 type-aliases-package: com.atguigu.springcloud.entities # 全部Entity別名類所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客戶端註冊進eureka服務列表內 service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #訪問路徑能夠顯示IP地址 info: app.name: atguigu-microservicecloud 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 # mysql驅動包 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 # mybatis配置文件所在路徑 type-aliases-package: com.atguigu.springcloud.entities # 全部Entity別名類所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 eureka: client: #客戶端註冊進eureka服務列表內 service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #訪問路徑能夠顯示IP地址 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$
將這兩個文件上傳到GitHub上,參照前面的作法
二、Config版的eureka服務端
(1)新建工程 microservicecloud-config-eureka-client-7001
pom.xml
<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-eureka-client-7001</artifactId> <dependencies> <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> <!--eureka-server服務端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- Ribbon相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- feign相關 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- hystrix和 hystrix-dashboard相關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</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>
application.yml
spring:
application:
name: microservicecloud-config-eureka-client
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的服務地址
Config_Git_EurekaServerApplicaion.java
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //EurekaServer服務器端啓動類,接受其它微服務註冊進來 public class Config_Git_EurekaServerApplicaion { public static void main(String[] args) { SpringApplication.run(Config_Git_EurekaServerApplicaion.class, args); } }
(2)測試,啓動 microservicecloud-config-eureka-client-7001 輸入 http://eureka7001.com:7001,出現以下界面說明成功。
三、Config版的dept微服務
(1)參考8001,拷貝新建microservicecloud-config-dept-client-8001
pom.xml
<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-dept-client-8001</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency><!-- 引入本身定義的api通用包,可使用Dept部門Entity --> <groupId>com.atguigu.springcloud</groupId> <artifactId>microservicecloud-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> <!-- 將微服務provider側註冊進eureka --> <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-actuator</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>
application.yml
spring:
application:
name: microservicecloud-config-dept-client
bootstrap.yml
spring: cloud: config: name: microservicecloud-config-dept-client #須要從GitHub上讀取的資源名稱,注意沒有yml後綴名 profile: test label: master uri: http://config-3344.com:3344 #本微服務啓動後先去找3344號服務,經過SpringCloudConfig獲取GitHub的服務地址
主啓動類及其餘業務邏輯代碼直接copy微服務microservicecloud-provider-dept-8001的,不作改動
(2)測試,啓動334四、microservicecloud-config-eureka-client-700一、microservicecloud-config-dept-client-8001
此時microservicecloud-provider-dept-8001的bootstrap.yml配置的是test。輸入:http://localhost:8001/dept/list,能夠看到數據庫配置是02
若是microservicecloud-provider-dept-8001的bootstrap.yml配置的是dev輸入:http://localhost:8001/dept/list,能夠看到數據庫配置是01
(3)這是直接在項目工程中修改的 bootstrap.yml配置,下面驗證一下從GitHub上讀取配置
把D:\20200320\mySpringCloud\microservicecloud-config的microservicecloud-config-dept-client.yml修改dev改成讀取3號庫,傳到GitHub上
從新啓動334四、microservicecloud-config-eureka-client-700一、microservicecloud-config-dept-client-8001
此時microservicecloud-provider-dept-8001的bootstrap.yml配置的是dev輸入:http://localhost:8001/dept/list,能夠看到數據庫配置是03
測試成功。