硬核!8個類手寫一個配置中心!
原創:微信公眾號
碼農參上
,歡迎分享,轉載請保留出處。
配置中心是我們平常使用微服務架構時重要的一個模組,常用的配置中心組件也比較多,從早期的Spring Cloud Config,到Disconf、Apollo、Nacos等,它們支援的功能、產品的性能以及給用戶的體驗也各有不同。
雖然說功能上有不少差異,但是它們解決的最核心問題,無疑是配置文件修改後的實時生效,有時候在搬磚之餘Hydra就在好奇實時生效是如何實現的、如果讓我來設計又會怎麼去實現,於是這幾天抽出了點空閑時間,摸魚摸出了個簡易版的單機配置中心,先來看看效果:
之所以說是簡易版本,首先是因為實現的核心功能就只有配置修改後實時生效,並且程式碼的實現也非常簡單,一共只用了8個類就實現了這個核心功能,看一下程式碼的結構,核心類就是core
包中的這8個類:
看到這是不是有點好奇,雖說是低配版,就憑這麼幾個類也能實現一個配置中心?那麼先看一下總體的設計流程,下面我們再細說程式碼。
程式碼簡要說明
下面對8個核心類進行一下簡要說明並貼出核心程式碼,有的類中程式碼比較長,可能對手機瀏覽的小夥伴不是非常友好,建議收藏後以後電腦瀏覽器打開(騙波收藏,計劃通!)。另外Hydra已經把項目的全部程式碼上傳到了git
,有需要的小夥伴可以移步文末獲取地址。
1、ScanRunner
ScanRunner
實現了CommandLineRunner
介面,可以保證它在springboot啟動最後執行,這樣就能確保其他的Bean已經實例化結束並被放入了容器中。至於為什麼起名叫ScanRunner
,是因為這裡要實現的主要就是掃描類相關功能。先看一下程式碼:
@Component
public class ScanRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
doScanComponent();
}
private void doScanComponent(){
String rootPath = this.getClass().getResource("/").getPath();
List<String> fileList = FileScanner.findFileByType(rootPath,null,FileScanner.TYPE_CLASS);
doFilter(rootPath,fileList);
EnvInitializer.init();
}
private void doFilter(String rootPath, List<String> fileList) {
rootPath = FileScanner.getRealRootPath(rootPath);
for (String fullPath : fileList) {
String shortName = fullPath.replace(rootPath, "")
.replace(FileScanner.TYPE_CLASS,"");
String packageFileName=shortName.replaceAll(Matcher.quoteReplacement(File.separator),"\\.");
try {
Class clazz = Class.forName(packageFileName);
if (clazz.isAnnotationPresent(Component.class)
|| clazz.isAnnotationPresent(Controller.class)
||clazz.isAnnotationPresent(Service.class)){
VariablePool.add(clazz);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
真正實現文件掃描功能是調用的FileScanner
,它的實現我們後面具體再說,在功能上它能夠根據文件後綴名掃描某一目錄下的全部文件,這裡首先掃描出了target
目錄下全部以.class
結尾的文件:
掃描到全部class
文件後,就可以利用類的全限定名獲取到類的Class
對象,下一步是調用doFilter
方法對類進行過濾。這裡我們暫時僅考慮通過@Value
註解的方式注入配置文件中屬性值的方式,那麼下一個問題來了,什麼類中的@Value
註解會生效呢?答案是通過@Component
、@Controller
、@Service
這些註解交給spring容器管理的類。
綜上,我們通過這些註解再次進行過濾出符合條件的類,找到後交給VariablePool
對變數進行處理。
2、FileScanner
FileScanner
是掃描文件的工具類,它可以根據文件後綴名篩選出需要的某個類型的文件,除了在ScanRunner
中用它掃描了class文件外,在後面的邏輯中還會用它掃描yml文件。下面,看一下FileScanner
中實現的文件掃描的具體程式碼:
public class FileScanner {
public static final String TYPE_CLASS=".class";
public static final String TYPE_YML=".yml";
public static List<String> findFileByType(String rootPath, List<String> fileList,String fileType){
if (fileList==null){
fileList=new ArrayList<>();
}
File rootFile=new File(rootPath);
if (!rootFile.isDirectory()){
addFile(rootFile.getPath(),fileList,fileType);
}else{
String[] subFileList = rootFile.list();
for (String file : subFileList) {
String subFilePath=rootPath + "\\" + file;
File subFile = new File(subFilePath);
if (!subFile.isDirectory()){
addFile(subFile.getPath(),fileList,fileType);
}else{
findFileByType(subFilePath,fileList,fileType);
}
}
}
return fileList;
}
private static void addFile(String fileName,List<String> fileList,String fileType){
if (fileName.endsWith(fileType)){
fileList.add(fileName);
}
}
public static String getRealRootPath(String rootPath){
if (System.getProperty("os.name").startsWith("Windows")
&& rootPath.startsWith("/")){
rootPath = rootPath.substring(1);
rootPath = rootPath.replaceAll("/", Matcher.quoteReplacement(File.separator));
}
return rootPath;
}
}
查找文件的邏輯很簡單,就是在給定的根目錄rootPath
下,循環遍歷每一個目錄,對找到的文件再進行後綴名的比對,如果符合條件就加到返回的文件名列表中。
至於下面的這個getRealRootPath
方法,是因為在windows環境下,獲取到項目的運行目錄是這樣的:
/F:/Workspace/hermit-purple-config/target/classes/
而class文件名是這樣的:
F:\Workspace\hermit-purple-config\target\classes\com\cn\hermimt\purple\test\service\UserService.class
如果想要獲取一個類的全限定名,那麼首先要去掉運行目錄,再把文件名中的反斜杠\
替換成點.
,這裡就是為了刪掉文件名中的運行路徑提前做好準備。
3、VariablePool
回到上面的主流程中,每個在ScanRunner
中掃描出的帶有@Component
、@Controller
、@Service
註解的Class
,都會交給VariablePool
進行處理。顧名思義,VariablePool
就是變數池的意思,下面會用這個容器封裝所有帶@Value
註解的屬性。
public class VariablePool {
public static Map<String, Map<Class,String>> pool=new HashMap<>();
private static final String regex="^(\\$\\{)(.)+(\\})$";
private static Pattern pattern;
static{
pattern=Pattern.compile(regex);
}
public static void add(Class clazz){
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Value.class)){
Value annotation = field.getAnnotation(Value.class);
String annoValue = annotation.value();
if (!pattern.matcher(annoValue).matches())
continue;
annoValue=annoValue.replace("${","");
annoValue=annoValue.substring(0,annoValue.length()-1);
Map<Class,String> clazzMap = Optional.ofNullable(pool.get(annoValue))
.orElse(new HashMap<>());
clazzMap.put(clazz,field.getName());
pool.put(annoValue,clazzMap);
}
}
}
public static Map<String, Map<Class,String>> getPool() {
return pool;
}
}
簡單說一下這塊程式碼的設計思路:
- 通過反射拿到
Class
對象中所有的屬性,並判斷屬性是否加了@Value
註解 @Value
如果要注入配置文件中的值,一定要符合${xxx}
的格式(這裡先暫時不考慮${xxx:defaultValue}
這種設置了默認值的格式),所以需要使用正則表達式驗證是否符合,並校驗通過後去掉開頭的${
和結尾的}
,獲取真正對應的配置文件中的欄位VariablePool
中聲明了一個靜態HashMap,用於存放所有配置文件中屬性-類-類中屬性的映射關係,接下來就要把這個關係存放到這個pool
中
簡單來說,變數池就是下面這樣的結構:
這裡如果不好理解的話可以看看例子,我們引入兩個測試Service
:
@Service
public class UserService {
@Value("${person.name}")
String name;
@Value("${person.age}")
Integer age;
}
@Service
public class UserDeptService {
@Value("${person.name}")
String pname;
}
在所有Class
執行完add
方法後,變數池pool
中的數據是這樣的:
可以看到在pool
中,person.name
對應的內層Map中包含了兩條數據,分別是UserService
中的name
欄位,以及UserDeptService
中的pname
欄位。
4、EnvInitializer
在VariablePool
封裝完所有變數數據後,ScanRunner
會調用EnvInitializer
的init
方法,開始對yml文件進行解析,完成配置中心環境的初始化。其實說白了,這個環境就是一個靜態的HashMap,key
是屬性名,value
就是屬性的值。
public class EnvInitializer {
private static Map<String,Object> envMap=new HashMap<>();
public static void init(){
String rootPath = EnvInitializer.class.getResource("/").getPath();
List<String> fileList = FileScanner.findFileByType(rootPath,null,FileScanner.TYPE_YML);
for (String ymlFilePath : fileList) {
rootPath = FileScanner.getRealRootPath(rootPath);
ymlFilePath = ymlFilePath.replace(rootPath, "");
YamlMapFactoryBean yamlMapFb = new YamlMapFactoryBean();
yamlMapFb.setResources(new ClassPathResource(ymlFilePath));
Map<String, Object> map = yamlMapFb.getObject();
YamlConverter.doConvert(map,null,envMap);
}
}
public static void setEnvMap(Map<String, Object> envMap) {
EnvInitializer.envMap = envMap;
}
public static Map<String, Object> getEnvMap() {
return envMap;
}
}
首先還是使用FileScanner
掃描根目錄下所有的.yml
結尾的文件,並使用spring自帶的YamlMapFactoryBean
進行yml文件的解析。但是這裡有一個問題,所有yml文件解析後都會生成一個獨立的Map,需要進行Map的合併,生成一份配置資訊表。至於這一塊具體的操作,都交給了下面的YamlConverter
進行處理。
我們先進行一下演示,準備兩個yml文件,配置文件一:application.yml
spring:
application:
name: hermit-purple
server:
port: 6879
person:
name: Hydra
age: 18
配置文件二:config/test.yml
my:
name: John
friend:
name: Jay
sex: male
run: yeah
先來看一看環境完成初始化後,生成的數據格式是這樣的:
5、YamlConverter
YamlConverter
主要實現的方法有三個:
doConvert()
:將EnvInitializer
中提供的多個Map合併成一個單層MapmonoToMultiLayer()
:將單層Map轉換為多層Map(為了生成yml格式字元串)convert()
:yml格式的字元串解析為Map(為了判斷屬性是否發生變化)
由於後面兩個功能暫時還沒有涉及,我們先看第一段程式碼:
public class YamlConverter {
public static void doConvert(Map<String,Object> map,String parentKey,Map<String,Object> propertiesMap){
String prefix=(Objects.isNull(parentKey))?"":parentKey+".";
map.forEach((key,value)->{
if (value instanceof Map){
doConvert((Map)value,prefix+key,propertiesMap);
}else{
propertiesMap.put(prefix+key,value);
}
});
}
//...
}
邏輯也很簡單,通過循環遍歷的方式,將多個Map最終都合併到了目的envMap
中,並且如果遇到多層Map嵌套的情況,那麼將多層Map的key通過點.
進行了連接,最終得到了上面那張圖中樣式的單層Map。
其餘兩個方法,我們在下面使用到的場景再說。
6、ConfigController
ConfigController
作為控制器,用於和前端進行交互,只有兩個介面save
和get
,下面分別介紹。
get
前端頁面在開啟時會調用ConfigController
中的get
介面,填充到textArea
中。先看一下get
方法的實現:
@GetMapping("get")
public String get(){
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
String yamlContent = null;
try {
Map<String, Object> envMap = EnvInitializer.getEnvMap();
Map<String, Object> map = YamlConverter.monoToMultiLayer(envMap, null);
yamlContent = objectMapper.writeValueAsString(map);
} catch (Exception e) {
e.printStackTrace();
}
return yamlContent;
}
之前在項目啟動時,就已經把配置文件屬性封裝到了EnvInitializer
的envMap
中,並且這個envMap
是一個單層的Map,不存在嵌套關係。但是我們這裡要使用jackson
生成標準格式的yml文檔,這種格式不符合要求,需要將它還原成一個具有層級關係的多層Map,就需要調用YamlConverter
的monoToMultiLayer()
方法。
monoToMultiLayer()
方法的程式碼有點長,就不貼在這裡了,主要是根據key中的.
進行拆分並不斷創建子級的Map,轉換完成後得到的多層Map數據如下:
在獲得這種格式後的Map後,就可以調用jackson
中的方法將Map轉換為yml格式的字元串傳遞給前端了,看一下處理完成後返回給前端的字元串:
save
在前端頁面修改了yml內容後點擊保存時,會調用save
方法保存並更新配置,方法的實現如下:
@PostMapping("save")
public String save(@RequestBody Map<String,Object> newValue) {
String ymlContent =(String) newValue.get("yml");
PropertyTrigger.change(ymlContent);
return "success";
}
在拿到前端傳過來的yml字元串後,調用PropertyTrigger
的change
方法,實現後續的更改邏輯。
7、PropertyTrigger
在調用change
方法後,主要做的事情有兩件:
- 修改
EnvInitializer
中的環境envMap
,用於前端頁面刷新時返回新的數據,以及下一次屬性改變時進行對比使用 - 修改bean中屬性的值,這也是整個配置中心最重要的功能
先看一下程式碼:
public class PropertyTrigger {
public static void change(String ymlContent) {
Map<String, Object> newMap = YamlConverter.convert(ymlContent);
Map<String, Object> oldMap = EnvInitializer.getEnvMap();
oldMap.keySet().stream()
.filter(key->newMap.containsKey(key))
.filter(key->!newMap.get(key).equals(oldMap.get(key)))
.forEach(key->{
System.out.println(key);
Object newVal = newMap.get(key);
oldMap.put(key, newVal);
doChange(key,newVal);
});
EnvInitializer.setEnvMap(oldMap);
}
private static void doChange(String propertyName, Object newValue) {
System.out.println("newValue:"+newValue);
Map<String, Map<Class, String>> pool = VariablePool.getPool();
Map<Class, String> classProMap = pool.get(propertyName);
classProMap.forEach((clazzName,realPropertyName)->{
try {
Object bean = SpringContextUtil.getBean(clazzName);
Field field = clazzName.getDeclaredField(realPropertyName);
field.setAccessible(true);
field.set(bean, newValue);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
});
}
}
前面鋪墊了那麼多,其實就是為了實現這段程式碼中的功能,具體邏輯如下:
- 調用
YamlConverter
的convert
方法,將前端傳來的yml格式字元串解析封裝成單層Map,數據格式和EnvInitializer
中的envMap
相同 - 遍歷舊的
envMap
,查看其中的key在新的Map中對應的屬性值是否發生了改變,如果沒有改變則不做之後的任何操作 - 如果發生改變,用新的值替換
envMap
中的舊值 - 通過屬性名稱,從
VariablePool
中拿到涉及改變的Class
,以及類中的欄位Field
。並通過後面的SpringContextUtil
中的方法獲取到這個bean的實例對象,再通過反射改變欄位的值 - 將修改後的Map寫回
EnvInitializer
中的envMap
到這裡,就實現了全部的功能。
8、SpringContextUtil
SpringContextUtil
通過實現ApplicationContextAware
介面獲得了spring容器,而通過容器的getBean()
方法就可以容易的拿到spring中的bean,方便進行後續的更改操作。
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> t) {
return applicationContext.getBean(t);
}
}
9、前端程式碼
至於前端程式碼,就是一個非常簡單的表單,程式碼的話可以移步git
查看。
最後
到這裡全部的程式碼介紹完了,最後做一個簡要的總結吧,雖然通過這幾個類能夠實現一個簡易版的配置中心功能,但是還有不少的缺陷,例如:
- 沒有處理
@ConfigurationProperties
註解 - 只處理了yml文件,沒有處理properties文件
- 目前處理的bean都是基於
singleton
模式,如果作用域為prototype
,也會存在問題 - 反射性能低,如果某個屬性涉及的類很多會影響性能
- 目前只能程式碼嵌入到項目中使用,還不支援獨立部署及遠程註冊功能
- ……
總的來說,後續需要完善的點還有不少,真是感覺任重道遠。
最後再聊聊項目的名稱,為什麼取名叫hermit-purple
呢,來源是jojo中二喬的替身隱者之紫,感覺這個替身的能力和配置中心的感知功能還是蠻搭配的,所以就用了這個哈哈。
那麼這次的分享就到這裡,我是Hydra,預祝大家虎年春節快樂,我們下篇再見。
項目git地址:
//github.com/trunks2008/hermit-purple-config
大家如果對程式碼有建議或者好的idea,歡迎在後台留言或加我微信好友一起討論。
作者簡介,碼農參上,一個熱愛分享的公眾號,有趣、深入、直接,與你聊聊技術。個人微信DrHydra9,歡迎添加好友,進一步交流。