Quarkus集成apollo配置中心
前言
Quarkus默認的配置文件和spring boot 一樣,默認讀取application.properties文件。
apollo是一個配置集中管理的開源項目,已被廣泛應(yīng)用。
下面我們就分析下Quarkus的配置加載結(jié)構(gòu),將apollo集成進來。
Eclipse MicroProfile Config:https://github.com/eclipse/microprofile-config/
smallrye-config:https://github.com/smallrye/smallrye-config
Quarkus的config構(gòu)成
Quarkus的配置功能是基于Eclipse MicroProfile Config配置規(guī)范而來的,MicroProfile Config本身不提供配置功能的實現(xiàn),只提供了基礎(chǔ)的配置api抽象,smallrye-config是這個api的其中一個實現(xiàn),Quarkus里用的就是smallrye-config。
microProfile config設(shè)計
1、可以通過ConfigProvider#getConfig()訪問應(yīng)用程序的當前配置。
2、一個配置包括從org.eclipse.microprofile.config.spi.ConfigSource接口收集的列表。
這些ConfigSource根據(jù)其序號進行排序。這樣,可以從外部以較低的重要性覆蓋配置。
默認情況下,有3個默認的ConfigSources:
- System.getProperties() (ordinlal =400)
- System.getenv()(ordinal =300)
- ClassPath上的所有META-INF / microprofile-config.properties文件。(默認ordinal = 100,可通過每個文件內(nèi)的config_ordinal屬性分別配置)
因此,可以在與應(yīng)用程序打包在一起的上述文件中指定默認值,以后可以為每個部署覆蓋默認值。較高的序數(shù)優(yōu)先于較低的序數(shù)。
從microProfile config設(shè)計來看,配置文件應(yīng)該是META-INF / microprofile-config.properties文件才對,但是除了上面默認的三個配置源外,Quarkus也提供了一個,代碼見:
io.quarkus.runtime.configuration.ApplicationPropertiesConfigSource,
如:
public abstract class ApplicationPropertiesConfigSource extends PropertiesConfigSource { private static final long serialVersionUID = -4694780118527396798L; static final String APPLICATION_PROPERTIES = "application.properties"; static final String MP_PROPERTIES = "META-INF/microprofile-config.properties"; ApplicationPropertiesConfigSource(InputStream is, int ordinal) { super(readProperties(is), APPLICATION_PROPERTIES, ordinal); } ApplicationPropertiesConfigSource(InputStream is, String nm, int ordinal) { super(readProperties(is), nm, ordinal); } private static Map readProperties(final InputStream is) { if (is == null) { return Collections.emptyMap(); } try (Closeable ignored = is) { try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8)) { try (BufferedReader br = new BufferedReader(isr)) { final Properties properties = new Properties(); properties.load(br); return (Map) (Map) properties; } } } catch (IOException e) { throw new IOError(e); } } }
集成apollo
綜上,集成apollo就變得異常簡單了,首先實現(xiàn)ConfigSource接口,我的實現(xiàn)如下:
/** * @author kl : http://kailing.pub * @version 1.0 * @date 2020/7/8 11:15 */ public class ApolloConfigSource implements ConfigSource { private final static String CONFIG_SOURCE_NAME = "Apollo"; private final Config config = ConfigService.getAppConfig(); @Override public SetgetPropertyNames() { return config.getPropertyNames(); } @Override public int getOrdinal() { return 600; } @Override public Map getProperties() { Map properties = new HashMap<>(6); for (String key : getPropertyNames()) { properties.put(key, config.getProperty(key, null)); } return properties; } @Override public String getValue(String propertyName) { return config.getProperty(propertyName, null); } @Override public String getName() { return CONFIG_SOURCE_NAME; } }
第二步,在META-INF/services下,創(chuàng)建文件
org.eclipse.microprofile.config.spi.ConfigSource
將你的實現(xiàn)全路徑名稱寫入這個文件,聲明配置源即可,如下圖所示:
現(xiàn)在,你可以將你的application.properties中的所有配置全部復(fù)制到apollo中了,然后刪除這個文件,重新啟動項目,你會發(fā)現(xiàn)一起運行正常
以上就是Quarkus集成apollo配置中心的詳細內(nèi)容,更多關(guān)于Quarkus集成apollo配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springcloud gateway網(wǎng)關(guān)服務(wù)啟動報錯的解決
這篇文章主要介紹了springcloud gateway網(wǎng)關(guān)服務(wù)啟動報錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03java設(shè)置session過期時間的實現(xiàn)方法
這篇文章主要介紹了java設(shè)置session過期時間的實現(xiàn)方法,以實例形式詳細講述了具體實現(xiàn)過程,非常具有參考借鑒價值,需要的朋友可以參考下2014-10-10Springboot視圖解析器ViewResolver使用實例
這篇文章主要介紹了Springboot視圖解析器ViewResolver使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04