Config 服務端配置與測試
config服務端通過gitee地址訪問gitee上的文件
新建一個項目,上傳到gitee,模擬運維人員進行操作,實現遠程與本地的整體一致性。
我的例子是建一個springcloud-config倉庫,包含一個yml文件,下面會讀取文章文件中的信息
1. 新建module cloud-config-center-3344,是cloud的配置中心模塊
2. 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>cloud2020</artifactId>
<groupId>com.atguigu.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. application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/bll896217791/springcloud-config.git #github倉庫上面的git倉庫名字
##搜索目錄
search-paths:
- springcloud-config
username: ***********
password: ***********
#讀取分支
label: master
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #註冊進eureka
4. 主啓動類
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author BLL
* @create 2020/4/21 15:39
*/
@SpringBootApplication
@EnableConfigServer
public class ConfogCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfogCenterMain3344.class, args);
}
}
5. windows下修改hosts文件,增加映射
127.0.0.1 config-3344.com
6. 啓動3344 ,測試
這樣就讀到了gitee上的文件內容。說一下這個訪問地址的組合,前面我們配置了hosts文件映射,所以能用config-3344.com代替localhost,master 指的是git倉庫的master分支,config-test.yml 指的就是這個分支上的文件,就是這樣訪問的。
Config客戶端配置與測試
客戶端不直接訪問gitee,而是通過服務端訪問。
1. 新建module cloud-config-client-3355
2. 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>cloud2020</artifactId>
<groupId>com.atguigu.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>
3. yml 文件
這裏我們要學一個新的yml文件——bootstrap.yml文件
#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:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #註冊進eureka
management:
endpoints:
web:
exposure:
include: "*"
4. 主啓動
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author BLL
* @create 2020/4/21 16:50
*/
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
5. 業務類
//將配置中心以REST接口的形式暴露,然後客戶端訪問
package com.atguigu.springcloud.controller;
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;
/**
* @author BLL
* @create 2020/4/21 16:52
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config}")
private String configinfo;
@GetMapping(value = "/configinfo")
public String getConfiginfo(){
return configinfo;
}
}
6. 啓動3355,測試
先來看一下注冊中心,確保server端和client端註冊進來
客戶端通過訪問服務端REST暴露的接口 訪問到dev
這樣就成功實現了客戶端3355訪問 Config服務端3344 通過github獲取配置信息
但是。現在修改config-dev.yml 配置並提交到gitee上,比如加個變量age或者版本號version。問題隨之而來,分佈式配置的動態刷新問題
Linux運維修改gitee上的配置文件內容做調整
刷新3344,發現ConfigServer配置中心立刻響應,因爲直接連的gitee
刷新3355,發現ConfigClient客戶端沒有任何響應
3355沒有變化,除非自己重啓或者重新加載,難道每次運維修改配置文件,客戶端都需要重啓?實際生產當中某些微服務加載是很慢的,那就要使用動態刷新。
Config客戶端之動態刷新
避免每次更新配置都需要重啓客戶端微服務3355
1. 修改3355模塊,引入actuator圖形化監控
意思是說 我自己發生變化了能被別人監控到
<!--PS:gateway是不能加actuator的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 修改yml,暴露監控端口
#添加配置,暴露監控端點
management:
endpoints:
web:
exposure:
include: "*"
3. 業務類controller加@RefreshScope,刷新功能
別急,此時需要運維人員發送Post請求刷新3355,@RefreshScope的作用就是自動獲悉刷新的內容
必須是Post請求,使用curl命令,稍等一下,出現下面界面,激活3355
再來測試,3355也更新了
這樣就避免了每次修改都要重啓客戶端服務,雖然需要自己發Post請求,但是也比重啓好,兩害相權取其輕。
想想還有什麼問題?
假設有多個微服務客戶端3355/3366/3377。。。每個微服務都需要執行一次post請求,可以寫一個腳本,批量執行。但是還有沒有更優化的方法?
我們想大範圍的自動刷新,可否廣播?一次通知,處處生效?
所以引入了SpringCloud Bus消息總線,詳情可以到博客分類下查看。