Spring Cloud Config的配置中心獲取不到最新配置信息的問題
本篇源于Spring Cloud Config的一個問題,但這個問題并非所有人都會遇到。如果您遇到了,那必須得看看這篇,如果沒有遇到您也應該看看,防患于未然!
問題描述
之前有朋友提出Spring Cloud Config的配置中心在運行一段時間之后,發(fā)現修改了配置信息,但是微服務應用并拿不到新的配置內容。同時,發(fā)現配置中心存儲配置的目錄/tmp的配置內容被清空了。
原因與解決
首先,分析一下上面的問題,其實已經有一定的線索。表面現象是微服務從配置中心獲取配置信息的時候獲取不到***的配置,而其根本原因就是在/tmp目錄下的緩存?zhèn)}庫已經被清空了,所以導致無法正常的通過Git獲取到***配置,那么自然各個微服務應用就無法獲取***配置了。
其實該問題在Spring Cloud的官方文檔中也有對應的說明,原文如下:
With VCS based backends (git, svn) files are checked out or cloned to the local filesystem. By default they are put in the system temporary directory with a prefix of config-repo-. On linux, for example it could be /tmp/config-repo-<randomid>. Some operating systems routinely clean out temporary directories. This can lead to unexpected behaviour such as missing properties. To avoid this problem, change the directory Config Server uses, by setting spring.cloud.config.server.git.basedir or spring.cloud.config.server.svn.basedir to a directory that does not reside in the system temp structure.
根據上面的內容,我們可以知道在某些系統中,對于/tmp目錄進行周期性的清理,所以也就有了上面所說的問題。
從文檔中我們也已經知道如果去解決該問題,無非就是通過spring.cloud.config.server.git.basedir或spring.cloud.config.server.svn.basedir參數來指定一個不會被定期清理的目錄。比如,我們可以設置:
- spring.cloud.config.server.git.basedir=config-repo
其他問題
這里需要注意一下,該參數的設置依然有一定的問題。按理解,如上配置的話,應該是在配置中心程序所在的目錄下創(chuàng)建一個config-repo目錄來進行存儲。但是,在測試了Dalston SR1和SR2版本之后,發(fā)現該配置只會將內容存儲到配置中心程序的同一級目錄下,并不會創(chuàng)建一個config-repo目錄。
但是,如果我們這樣設置,就可以將配置的緩存內容存儲在配置中心所在目錄下的config-repo目錄中了:
- spring.cloud.config.server.git.basedir=config-repo/config-repo
【本文為51CTO專欄作者“翟永超”的原創(chuàng)稿件,轉載請通過51CTO聯系作者獲取授權】