springcloud-Config分散式配置
1 概念描述
1.1 官方描述
官網地址:https://spring.io/projects/spring-cloud-config
Spring Cloud Config 為分散式系統中的外部化配置提供伺服器和客戶端支援。使用配置伺服器,您可以在所有環境中管理應用程式的外部屬性。客戶端和伺服器上的概念與 Spring 和抽象的對映相同,因此它們非常適合 Spring 應用程式,但可以用於以任何語言執行的任何應用程式。當應用程式通過部署管道從開發到測試再進入生產時,您可以管理這些環境之間的配置,並確保應用程式在遷移時具備執行所需的一切。伺服器儲存後端的預設實現使用 git,因此它可以輕鬆支援配置環境的標記版本,並且可以通過各種工具訪問以管理內容。很容易新增替代實現並將它們插入 Spring configuration.EnvironmentPropertySource
1.2 分散式系統面臨的配置檔案問題
微服務意味著要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此係統中會出現大量的服務,由於每個服務都需要必要的配置資訊才能執行,所以一套集中式的,動態的配置管理設施是必不可少的。spring cloud提供了configServer來解決這個問題,我們每一個微服務自己帶著一個application.yml,那上百個的配置檔案修改起來,令人頭疼!
1.3 什麼是SpringCloud config分散式配置中心?
spring cloud config 為微服務架構中的微服務提供集中化的外部支援,配置伺服器為各個不同微服務應用的所有環節提供了一箇中心化的外部配置。
1.4 spring cloud config 的服務端和客戶端
- 服務端 : 也稱為分散式配置中心,它是一個獨立的微服務應用,用來連線配置伺服器併為客戶端提供獲取配置資訊,加密,解密資訊等訪問介面。
- 客戶端 : 則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啟動的時候從配置中心獲取和載入配置資訊。配置伺服器預設採用git來儲存配置資訊,這樣就有助於對環境配置進行版本管理。並且可用通過git客戶端工具來方便的管理和訪問配置內容。
1.5 spring cloud config 分散式配置中心能幹什麼
- 集中式管理配置檔案
- 不同環境,不同配置,動態化的配置更新,分環境部署,比如 /dev /test /prod /beta /release
- 執行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置檔案,服務會向配置中心統一拉取配置自己的資訊
- 當配置發生變動時,服務不需要重啟,即可感知到配置的變化,並應用新的配置
- 將配置資訊以REST介面的形式暴露
2 Git環境搭建
由於spring cloud config 預設使用git來儲存配置檔案 (也有其他方式,比如自持SVN 和本地檔案),但是最推薦的還是git ,而且使用的是 http / https 訪問的形式。
2.1 安裝Git
參考地址:https://www.runoob.com/git/git-install-setup.html
2.2 建立碼雲倉庫
碼雲地址:https://gitee.com/
登入碼雲並新建一個倉庫
2.3 克隆專案到本地
開啟我們建立的倉庫,複製HTTPS路徑
選擇一個存放專案的資料夾,右鍵選中 Git Bash Here會彈出Git的命令視窗,然後輸入克隆專案的命令,後面的地址是我們上一步複製的HTTPS地址
git clone https://gitee.com/lv1024/springcloud-config.git
完成後,檢視在本地生成的專案檔案
2.4 設定使用者名稱和郵箱並檢視配置資訊
依次輸入如下命令設定使用者名稱和郵箱
$ git config --global user.name "lv"
$ git config --global user.email "[email protected]"
使用者名稱和郵箱設定完成後,再輸入下面的命令檢視配置資訊
$ git config --list
2.5 新建application.yaml配置檔案
在本地的專案目錄下新建一個application.yaml配置檔案
並編寫裡面的內容
spring:
profiles:
active: dev
---
spring:
profiles: dev
application:
name: springcloud-config-dev
---
spring:
profiles: test
application:
name: springcloud-config-test
2.6 把新建的yaml配置檔案提交到碼雲倉庫
進入專案目錄
$ cd springcloud-config
新增新增的檔案,並檢視狀態
$ git add .
$ git status
提交到本地倉庫
$ git commit -m "first commit"
放入到遠端倉庫
$ git push origin master
檢視碼雲倉庫,確認yaml檔案提交進來了
2.7 設定為公開的倉庫
在後面的測試中要讀取遠端倉庫中的內容,這個倉庫就是現在建立的碼雲倉庫,想要讓倉庫能被讀取到,必須保證這個倉庫是公開的,如果是私有倉庫是讀取不到的,所以在後面的測試進行之前還需將倉庫的許可權設定為開源狀態,在碼雲倉庫的管理中找到這個設定
3 搭建服務端
3.1 建立專案
在父工程新建一個普通的maven專案springcloud-config-server-3344模組
3.2 匯入依賴
springcloud-config-server-3344 : pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.lv</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-server-3344</artifactId>
<dependencies>
<!--spring-cloud-config-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.3 編寫配置檔案
在resources目錄下,新建一個application.yaml配置檔案
springcloud-config-server-3344 : src/main/resources/application.yaml
server:
port: 3344
spring:
application:
name: springcloud-config-server
#連線遠端倉庫
cloud:
config:
server:
git:
uri: https://gitee.com/lv1024/springcloud-config.git #碼雲倉庫的https地址
#通過 config-server 可以連線到git,訪問其中的資源以及配置~
3.4 編寫主啟動類
建立包結構com.lv.springcloud,並在該包下新建一個主啟動類Config_Server_3344.java
springcloud-config-server-3344 : src/main/java/com/lv/springcloud/Config_Server_3344.java
package com.lv.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer //開啟spring cloud config server服務
@SpringBootApplication
public class Config_Server_3344 {
public static void main(String[] args) {
SpringApplication.run(Config_Server_3344.class,args);
}
}
3.5 測試
執行springcloud-config-server-3344模組,通過Http服務訪問資源有如下格式
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
我們使用的是yaml配置檔案,下面按照格式依次測試一下,http://localhost:3344/application-dev.yml
http://localhost:3344/application/test/master
http://localhost:3344/master/application-dev.yml
如果測試訪問不存在的配置則不顯示 如:http://localhost:3344/master/application-aaa.yml
4 搭建客戶端
4.1 新建config-client.yaml檔案
在gitee遠端倉庫對應的本地專案目錄下新建一個config-client.yaml檔案
編寫config-client.yaml
spring:
profiles:
active: dev
---
server:
port: 8201
#spring的配置
spring:
profiles: dev
application:
name: springcloud-provider-dept
#Eureka的配置,服務註冊到哪裡
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 8202
#spring的配置
spring:
profiles: test
application:
name: springcloud-provider-dept
#Eureka的配置,服務註冊到哪裡
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
4.2 將config-client.yaml提交到碼雲倉庫
這一步驟與本篇部落格2.6完全相同
檢視碼雲倉庫,確認yaml檔案成功提交
4.3 建立專案
在父工程新建一個普通的maven專案springcloud-config-client-3355模組
4.4 匯入依賴
springcloud-config-client-3355 : pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.lv</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-client-3355</artifactId>
<dependencies>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
4.5 編寫配置檔案
在resources目錄下,新建兩個配置檔案application.yaml和bootstrap.yaml
springcloud-config-client-3355 : src/main/resources/application.yaml
# 使用者級別的配置
spring:
application:
name: springcloud-config-client-3355
springcloud-config-client-3355 : src/main/resources/bootstrap.yaml
# 系統級別的配置
spring:
cloud:
config:
name: config-client # 需要從git上讀取的資源名稱,不要字尾
profile: dev
label: master
uri: http://localhost:3344
4.6 編寫控制層
建立包結構com.lv.springcloud.controller,並在該包下新建ConfigClientController.java
springcloud-config-client-3355 : src/main/java/com/lv/springcloud/controller/ConfigClientController.java
package com.lv.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName; //獲取微服務名稱
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer; //獲取Eureka服務
@Value("${server.port}")
private String port; //獲取服務端的埠號
@RequestMapping("/config")
public String getConfig(){
return "applicationName:"+applicationName +
"eurekaServer:"+eurekaServer +
"port:"+port;
}
}
4.7 編寫主啟動類
在controller包同級目錄下建立主啟動類
springcloud-config-client-3355 : src/main/java/com/lv/springcloud/ConfigClient_3355.java
package com.lv.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConfigClient_3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355.class,args);
}
}
4.8 測試
啟動服務端springcloud-config-server-3344 再啟動客戶端springcloud-config-client-3355,訪問http://localhost:8201/config/
成功讀取到了遠端倉庫的配置
5 遠端配置測試
總體思路就是不在本地專案中編寫配置檔案,而是將配置檔案放到碼雲倉庫中,然後再將遠端的配置檔案應用到專案中
5.1 新建兩個配置檔案
在gitee遠端倉庫對應的本地專案目錄下新建config-eureka.yaml檔案和config-dept.yaml檔案
編寫兩個配置檔案中的內容
config-eureka.yaml
spring:
profiles:
active: dev
---
server:
port: 7001
#spring的配置
spring:
profiles: dev
application:
name: springcloud-config-eureka
#Eureka配置
eureka:
instance:
hostname: eureka7001.com #Eureka服務端的例項名稱
client:
register-with-eureka: false #表示是否向eureka註冊中心註冊自己
fetch-registry: false #fetch-registry如果為false,則表示自己為註冊中心
service-url:
#單機:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#叢集(關聯):
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
---
server:
port: 7001
#spring的配置
spring:
profiles: test
application:
name: springcloud-config-eureka
#Eureka配置
eureka:
instance:
hostname: eureka7001.com #Eureka服務端的例項名稱
client:
register-with-eureka: false #表示是否向eureka註冊中心註冊自己
fetch-registry: false #fetch-registry如果為false,則表示自己為註冊中心
service-url:
#單機:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#叢集(關聯):
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
config-dept.yaml
spring:
profiles:
active: dev
---
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.lv.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring的配置
spring:
profiles: dev
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource #資料來源
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
#Eureka的配置,服務註冊到哪裡
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的預設描述資訊!
#info配置
info:
app.name: lv1024-springcloud
company.name: blog.lv1024.com
---
server:
port: 8001
#mybatis配置
mybatis:
type-aliases-package: com.lv.springcloud.pojo
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
#spring的配置
spring:
profiles: test
application:
name: springcloud-config-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource #資料來源
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db02?useSSL=false&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
#Eureka的配置,服務註冊到哪裡
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的預設描述資訊!
#info配置
info:
app.name: lv1024-springcloud
company.name: blog.lv1024.com
5.2 將config-eureka.yaml檔案和config-dept.yaml提交到碼雲倉庫
這一步驟與本篇部落格2.6完全相同
檢視碼雲倉庫,確認兩個檔案成功提交
5.3 建立專案
在父工程下新建兩個maven專案springcloud-config-eureka-7001模組和springcloud-config-dept-8001模組,並將springcloud-eureka-7001模組中的內容複製到springcloud-config-eureka-7001模組,把springcloud-provider-dept-8001模組中的內容複製到springcloud-config-dept-8001模組.
5.4 修改springcloud-config-eureka-7001模組
在pom檔案中新增config依賴
springcloud-config-eureka-7001 : pom.xml
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
清空該模組的application.yml配置,並新增應用名稱即可
springcloud-config-eureka-7001 : src/main/resources/application.yaml
spring:
application:
name: springcloud-config-eureka-7001
在resources目錄下新建bootstrap.yml配置檔案連線遠端配置
springcloud-config-eureka-7001 : src/main/resources/bootstrap.yaml
spring:
cloud:
config:
name: config-eureka # 倉庫中的配置檔名稱
label: master
profile: dev
uri: http://localhost:3344
修改主啟動類的名字
springcloud-config-eureka-7001 : src/main/java/com/lv/springcloud/ConfigEurekaServer_7001.java
package com.lv.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//啟動之後,訪問http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer //表示這個類是服務端的啟動類,可以接收別人註冊進來~
public class ConfigEurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(ConfigEurekaServer_7001.class,args);
}
}
5.5 修改springcloud-config-dept-8001模組
在pom檔案中新增config依賴
springcloud-config-dept-8001 : pom.xml
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
清空該模組的application.yml配置,並新增應用名稱即可
springcloud-config-dept-8001 : src/main/resources/application.yaml
spring:
application:
name: springcloud-config-dept-8001
在resources目錄下新建bootstrap.yml配置檔案連線遠端配置
springcloud-config-dept-8001 : src/main/resources/bootstrap.yaml
spring:
cloud:
config:
name: config-dept
label: master
profile: dev
uri: http://localhost:3344
修改主啟動類的名字
springcloud-config-dept-8001 : src/main/java/com/lv/springcloud/ConfigDeptProvider_8001.java
package com.lv.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
//啟動類
@SpringBootApplication
@EnableEurekaClient// 在服務啟動後自動註冊到Eureka中
@EnableDiscoveryClient //服務發現
public class ConfigDeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(ConfigDeptProvider_8001.class,args);
}
}
5.6 測試
啟動springcloud-config-server-3344,訪問http://localhost:3344/master/config-eureka-dev.yml
遠端倉庫的配置檔案內容訪問成功,接下來依次啟動springcloud-config-client-3355,springcloud-config-eureka-7001,springcloud-config-dept-8001,訪問http://localhost:7001/
註冊中心啟動成功,並且springcloud-config-dept-8001模組成功註冊,說明遠端倉庫的配置檔案全部生效,接下來查詢一下資料庫的內容
資料查詢成功