阿波羅使用注意事項

1.引入客戶端依賴

<code><dependency>/<code>
<code>\t\t  <groupid>com.ctrip.framework.apollo/<groupid>/<code>
<code>\t\t  <artifactid>apollo-client/<artifactid>/<code>
<code>\t\t  <version>1.5.1/<version>/<code>
<code>

注:

如果是有內部多個應用同時要使用Apollo,可以將Apollo封裝到一個單獨的Jar中,供這些應用引入。且可以將各環境(DEV、UAT、TEST、PRD)Meta Server寫在名為apollo-env.properties配置文件中,如下所示:

<code>dev.meta=http://1.1.1.1:8080/<code>
<code>fat.meta=http://apollo.fat.xxx.com/<code>
<code>uat.meta=http://apollo.uat.xxx.com/<code>
<code>pro.meta=http://apollo.xxx.com/<code>

並將其放到classpath下,這些Meta Server就是各環境的默認值,後續也可以通過在java的啟動參數-Dapollo.meta=metaServerHost,或者spring boot的配置文件application.properties通過指定apollo.meta=metaServerHost等方式,進行修改。

然後在java啟動參數只需要通過-Denv=DEV等參數,apollo就會選擇特定環境的Meta Server。

詳見:apollo meta server的配置

2、配置中引入其它變量的參數熱更新配置

Apollo本身對參數具有熱更新的功能,但是Apollo中配置Key一定要是application.properties或application.yml中的key,而不是引入了其它配置文件中的key,否則熱更新不會起效,以下舉示例說明。

項目中包括了兩個配置文件:

<code>application-config.properties/<code>
<code>application.properties/<code>

內容分別如下:

application-config.properties

<code>TEST_KEY=It's a test value/<code>

application.properties

<code>spring.profile.active = config/<code>
<code>test.key=${TEST_KEY:default_value}/<code>

如果在apollo中配置項是

<code>TEST_KEY=new value/<code>

那修改該配置項TEST_KEY,應用雖然可以收到配置項修改的通知,但是引入test.key的對象中的屬性值不會發生變化,只有重啟應用才會生效。

為了讓配置即時生效,Apollo中的配置項應該是application.properties中的test.key,修改配置項test.key的值,則引入test.key的對象中的屬性值就會即時生效。

3、Apollo 關於@ConfigurationProperties 的動態刷新

針對使用了註解@ConfigurationProperties的配置注入方式,如下示例類:

<code>@ConfigurationProperties(prefix = "redis.cache")/<code>
<code>public class SampleRedisConfig {/<code>
<code>  private int expireSeconds;/<code>
<code>  private int commandTimeout;/<code>
<code> /<code>
<code>  public void setExpireSeconds(int expireSeconds) {/<code>
<code>    this.expireSeconds = expireSeconds;/<code>
<code>  }/<code>
<code>  public void setCommandTimeout(int commandTimeout) {/<code>
<code>    this.commandTimeout = commandTimeout;/<code>
<code>  }/<code>
<code>}/<code>

針對這種Bean,需要使用spring cloud的EnvironmentChangeEvent和RefreshScope,實現方式如下:

1)依賴spring cloud context,如:

<code><dependency>/<code>
<code>\t\t    <groupid>org.springframework.cloud/<groupid>/<code>
<code>\t\t    <artifactid>spring-cloud-context/<artifactid>/<code>
<code>\t\t    <version>2.0.4.RELEASE/<version>/<code>
<code>

2)在spring boot的配置文件中增加如下配置項:

<code>spring.boot.enableautoconfiguration=true/<code>

3)實現ApplicationContextAware

<code>import com.ctrip.framework.apollo.model.ConfigChangeEvent;

import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.context.environment.EnvironmentChangeEvent;

import org.springframework.cloud.context.scope.refresh.RefreshScope;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;



/**

* apollo 自動刷新

*

* @author qubianzhong

* @Date 20:32 2019/11/11

*/

@Component

@Slf4j

public class ApolloRefreshConfig implements ApplicationContextAware {



ApplicationContext applicationContext;



@Autowired

RefreshScope refreshScope;



\t//這裡指定Apollo的namespace,非常重要,如果不指定,默認只使用application

@ApolloConfigChangeListener(value = {ConfigConsts.NAMESPACE_APPLICATION,"business","everything"})

public void onChange(ConfigChangeEvent changeEvent) {

for (String changedKey : changeEvent.changedKeys()) {

log.info("apollo changed namespace:{} Key:{} value:{}", changeEvent.getNamespace(), changedKey, changeEvent.getChange(changedKey));

}

refreshProperties(changeEvent);

}



public void refreshProperties(ConfigChangeEvent changeEvent) {

this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));

refreshScope.refreshAll();

}



@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}

}/<code>

4、服務器上的項目啟動腳本boot.sh裡面增加啟動參數

<code> /<code>

apollo.autoUpdateInjectedSpringProperties:

是否自動更新配置,如果不想自動更新則將該值設置為false,默認值為true

app.id:項目的AppId

env:環境設置,測試環境DEV,預發佈環境UAT,生產環境PRO,可以通三種方式(啟動參數、操作系統環境變量以及配置文件)指定,詳見文檔

apollo.meta:apollo服務地址,例如測試環境是http://192.168.10.235:8071

apollo.bootstrap.enabled:啟動階段是否注入配置

apollo.bootstrap.eagerLoad.enabled:是否將apollo的加載放到日誌系統的加載之前,這樣就可以通過apollo來管理日誌相關的配置,如日誌級別的設置等,如果為true,則會在這個階段apollo不會有日誌輸出

apollo.bootstrap.namespaces:配置項,這裡要和阿波羅裡面配置的properties文件一一對應,例如阿波羅配置了application.properties、db.properties和dubbo.properties三個配置文件,這裡就是設值為application,db,dubbo

apollo.cacheDir:

自定義緩存路徑

腳本中namespaces的默認值為application,所以默認讀取的是阿波羅上application.properties配置文件。

如果參數特別多,或者其他需求,需要添加多個配置文件的話,需要聯繫運維同學配合修改項目的jenkins配置,修改namespaces參數。正常情況建議只用一個application.properties配置文件

詳情參看Apollo Java使用的官方文檔:Apollo Java客戶端使用指南


分享到:


相關文章: