1. 程式人生 > >Spring Cloud配置中心獲取不到最新配置資訊的問題

Spring Cloud配置中心獲取不到最新配置資訊的問題

本篇源於Spring Cloud Config的一個問題,但這個問題並非所有人都會遇到。如果您遇到了,那必須得看看這篇,如果沒有遇到您也應該看看,防患於未然!

問題描述

之前有朋友提出Spring Cloud Config的配置中心在執行一段時間之後,發現修改了配置資訊,但是微服務應用並拿不到新的配置內容。同時,發現配置中心儲存配置的目錄 /tmp 的配置內容被清空了。

原因與解決

首先,分析一下上面的問題,其實已經有一定的線索。表面現象是微服務從配置中心獲取配置資訊的時候獲取不到最新的配置,而其根本原因就是在 /tmp目錄下的快取倉庫已經被清空了,所以導致無法正常的通過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引數來指定一個不會被定期清理的目錄。比如,我們可以設定:

  1. spring.cloud.config.server.git.basedir=config-repo

其他問題

這裡需要注意一下,該引數的設定依然有一定的問題。按理解,如上配置的話,應該是在配置中心程式所在的目錄下建立一個config-repo目錄來進行儲存。但是,在測試了Dalston SR1和SR2版本之後,發現該配置只會將內容儲存到配置中心程式的同一級目錄下,並不會建立一個config-repo目錄。

但是,如果我們這樣設定,就可以將配置的快取內容儲存在配置中心所在目錄下的config-repo目錄中了:

  1. spring.cloud.config.server.git.basedir=config-repo/config-repo

推薦閱讀

更多Spring Boot/Cloud乾貨請持續關注我的部落格(didispace.com)和公眾號。

想深入探討的朋友可以掃描下面二維碼加入我的知識星球

0?wx_fmt=jpeg