Springcloud-Alibaba 〖十一〗Config
PS: github倉庫倉庫地址項目都放到裏面了
一. 分佈式架構面臨的問題
微服務意味着要將單體應用中的業務拆分成一個個子服務, 每個服務的粒度相對較小,因此係統中會出現大量的服務。於每個服務都需要必要的配置信息才能運行,所以一套集中式的、動態的配置管理設施是必不可少的。
配置中心架構圖
SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同微服務應用的所有環境提供了一箇中心化的外部配置。
二. Config能幹嗎?
- 集中管理配置文件
- 不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release
- 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一一拉取配置自己的信息
- 當配置發生變動時,服務不需要重啓即可感知到配置的變化並應用新的配置
- 將配置信息以REST接口的形式暴露
三. 新建module cloud-config-center-3344,是cloud的配置中心模塊
3.1 項目目錄
3.2 pom
<?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>cloud2020</artifactId>
<groupId>com.aiguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-center-3344</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.3 application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/你自己的倉庫名/springcloud-config.git #github倉庫上面的git倉庫名字
##搜索目錄
search-paths:
- springcloud-config
#讀取分支
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #註冊進eureka
3.4 主啓動類
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
3.5 測試
這裏首先要去github新建一個倉庫
啓動7001與3344項目
訪問http://localhost:3344/master/config-dev.yml可以看見到我們已經可以訪問到github上的文件了
若是找不到配置,就會返回一個空的{}
3.6 總結
之前在yml配置文件裏面是這樣配置的
當啓動我們的3344項目時,可以通過三種方法找到配置文件並讀取
四. 新建客戶端module cloud-config-client-3355
- applicaiton. yml是用戶級的資源配置項
- bootstrap. yml是系統級的,優先級更加高
Spring Clould會創建一個"Bootstrap Context" , 作爲Spring應用的Application Context的父上下文。初始化的時候,BootstrapContext’負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個從外部獲取的Environment。
Bootstrap’屬性有高優先級,默認情況下,它們不會被本地配置覆蓋。 Bootstrap context’和Application Context有着不同的約定,所以新增了一個bootstrap.ymI文件, 保證Bootstrap Context’和Application Context配置的分離。
要將Client模塊下的application.yml文件改爲bootstrap.yml,這是很關鍵的,
因爲bootstrap.ym是比application.yml先加載的。bootstrap.yml優先級高於application.yml
4.1 項目目錄
4.2 pom
<?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>cloud2020</artifactId>
<groupId>com.aiguigu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<!--不帶server了,說明是客戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.3 bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客戶端配置
config:
label: master #分支名稱
name: config #配置文件名稱
profile: dev #讀取後綴名稱 上述3個綜合:master分支上config-dev.yml的配置文件被讀取 http://config-3344.com:3344/master/config-dev.yml
uri: http://localhost:3344 #配置中心地址 表示通過這個服務端訪問
#服務註冊到eureka地址
eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:7001.com/eureka
4.4 主啓動
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigCenterMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3355.class,args);
}
}
4.5 業務類
controller層
package com.atguigu.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo; //要訪問的3344上的信息
@GetMapping("/configInfo") //請求地址
public String getConfigInfo(){
return configInfo;
}
}
這裏我們返回從3344主配置中心獲取的config.info配置值
4.6 測試
成功實現了客戶端3355訪問SpringCloud Config3344通過GitHub獲取配置信息
五. 分佈式配置動態刷新
yml配置文件新增
#暴露監控端點
management:
endpoints:
web:
exposure:
include: "*"
controller層新增一個註解@RefreshScope
5.1 測試
這裏我們修改以下github上配置的版本信息
再來訪問一下3344項目的總配置,發現改變了
當我們訪問3355項目的得到配置呢?
發現還是1,這裏就需要給3355發一條post請求來激活動態配置
這裏往命令窗口扔一條命令即可
curl -X POST "http://localhost:3355/actuator/refresh"
成功刷新配置信息
5.2 但是還是存在問題~
- 假如有多個微服務客戶端3355/3366/3377。。。。。。+
- 每個微服務都要執行一次post請求 ,手動刷新?
- 可否廣播,一次通知,處處生效?
- 我們想大範圍刷新呢?