记录spring、springboot集成apollo配置中心
一, spring集成apollo,前提是apollo配置中心服務端已經在運行中
上面是我在阿里云服務搭建的apollo配置中心服務端,登錄后的樣子。沒有搭建服務端的小伙伴,請先搭建好apollo的服務端
然后點擊‘創建項目’,新建測試用的項目
填寫類容:
在里面新建測試需要的配置,kay-value格式的
創建/x修改好了,需要發布生效
接下來我們在我們的項目里面配置使用apollo注冊中心的里面的配置
在項目的resources里面創建目錄‘META-INF’,然后在目錄下創建配置文件‘app.properties’,
內容如下:
#自定義的appid名稱,區分不同的應用
app.id=20201201
#eureka配置中心地址
apollo.meta=http://139.196.186.76:49006
#啟用apollo配置開關
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled=true
apollo 使用配置的命名空間,多個以逗號分隔
apollo.bootstrap.namespaces = application
然后,在resource文件夾下創建‘apollo-env.properties’配置文件,內容如下:
dev.mate=http://139.196.186.76:49006 #加入apollo在遠程服務器上的話,項目在本地測試的話,ip端口需要換成你的apollo遠程訪問ip+遠程開放端口
apollo.autoUpdateInjectedSpringProperties=true
pom 文件添加:
<!--apollo--><dependency><groupId>com.ctrip.framework.apollo</groupId><artifactId>apollo-client</artifactId><version>1.3.0</version></dependency><!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-core --><dependency><groupId>com.ctrip.framework.apollo</groupId><artifactId>apollo-core</artifactId><version>1.3.0</version></dependency>在spring項目的 application.xml里面配置apollo:
<apollo:config order="2"/><apollo:config namespaces="application"/><bean id="appPropertyPlaceholderConfigurer"class="cn.glor.monopoly.apollo.config.AppPropertyPlaceholderConfigurer"></bean> /*** @描述* @創建人 joy_qiu* @創建時間 2020-12-24-18:19* @修改人和其它信息*/ public class AppPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {@Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {try { //從apollo中獲取所有配置信息Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never nullSet<String> fieldnames = config.getPropertyNames(); //遍歷配置信息for(String fieldname : fieldnames){String attributeName=fieldname;String attributeValue = config.getProperty(fieldname,"");System.out.println("attributeName:"+attributeName + "; attributeValue:" + attributeValue );props.put(attributeName,attributeValue);}} catch (Exception e) {e.printStackTrace();logger.info("獲取apollo配置失敗");}super.processProperties(beanFactoryToProcess, props);} }然后,在本地的環境是Windows的話需要在c://opt/創建settings 文件夾,Linux 環境是在/opt/settings/下面創建,并在settings文件夾下創建文件server.properties文件,注意 參數 ‘idc’ 是指定集群環境的,應為一般項目會有多個環境:dev,uat,pro等等內容如下:
env=DEV
apollo.meta=http://139.196.186.76:49006
apollo.configService=http://139.196.186.76:49006
#配置idc 表示指定集群的配置,不配置使用default集群 的配置
#idc=dev
然后,編寫測試的接口:
然后啟動項目,測試接口:
訪問接口的結果和apollo服務端的一直,然后我們測試下,修改apollo服務端的參數值,看項目是否可以接受到:
記得修改后,要點擊發布才能生效
然后直接訪問接口:
測試成功,如果遠程的apollo服務停止,會讀取本地的緩存配置文件,我們看看配置文件在哪里,apollo默認會在windows環境是在C://opt/data/appid/下面
Linux 環境是在/opt/settings/下面
如果是現有項目要集成apollo的話,可以現有項目的配置文件復制到apollo,如下圖點擊‘文本’,參數格式就和項目里面的格式一樣,直接將原項目的參數復制到文本框里面就好了
點擊右上角的對號,提交修改
添加成功,點擊發布就可以使用了
springboot集成apollo,比spring集成要簡單多了,本地的/opt/settings/下面配置不變,代碼請看:https://github.com/qzy-joe/springboot_apollo.git
總結
以上是生活随笔為你收集整理的记录spring、springboot集成apollo配置中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10天时间做出php,php 最近10天
- 下一篇: 下载 Java 学习的权威文档