Apollo-ServerHttpインターフェースのドキュメント
https://ctripcorp.github.io/apollo/#/zh/usage/other-language-client-user-guide
Apolloから構成を読み取るためのキャッシュを備えたConfigFileControllerHttpインターフェース
インターフェイスはキャッシュから構成を取得します。これは、30秒ごとの単純なポーリング構成など、高頻度の構成プル要求に適しています。
// 使用 Guava 对配置信息进行缓存
private Cache<String, String> localCache;
localCache = CacheBuilder.newBuilder()
.expireAfterWrite(EXPIRE_AFTER_WRITE, TimeUnit.MINUTES)
.weigher((Weigher<String, String>) (key, value) -> value == null ? 0 : value.length())
.maximumWeight(MAX_CACHE_SIZE)
.removalListener(notification -> {
String cacheKey = notification.getKey();
logger.debug("removing cache key: {}", cacheKey);
if (!cacheKey2WatchedKeys.containsKey(cacheKey)) {
return;
}
//create a new list to avoid ConcurrentModificationException
List<String> watchedKeys = new ArrayList<>(cacheKey2WatchedKeys.get(cacheKey));
for (String watchedKey : watchedKeys) {
watchedKeys2CacheKey.remove(watchedKey, cacheKey);
}
cacheKey2WatchedKeys.removeAll(cacheKey);
logger.debug("removed cache key: {}", cacheKey);
})
.build();
// outputFormat 默认 properties
String cacheKey = assembleCacheKey(outputFormat, appId, clusterName, namespace, dataCenter);
String result = localCache.getIfPresent(cacheKey);
- ip = {clientIp}パラメーターはオプションであり、グレースケール公開を実現するために使用されます
com.ctrip.framework.apollo.configservice.controller.ConfigFileController
URL: {
config_server_url}/configfiles/json/{
appId}/{
clusterName}/{
namespaceName}?ip={
clientIp}
Method: GET
// Http Response 返回结果
{
"portal.elastic.document.type":"biz",
"portal.elastic.cluster.name":"hermes-es-fws"
}
キャッシュのないConfigControllerHttpインターフェースは、Apolloから構成を読み取ります
インターフェースはデータベースから直接構成を取得します。これを構成プッシュ通知とともに使用して、構成をリアルタイムで更新できます。
-
releaseKey = {releaseKey}:最後に返されたオブジェクトにreleaseKeyを渡すだけで、バージョンをサーバーと比較するために使用されます。バージョンが変更されない場合、サーバーはトラフィックと計算を節約するために直接304を返します。
-
ip = {clientIp}パラメーターはオプションであり、グレースケール公開を実現するために使用されます
com.ctrip.framework.apollo.configservice.controller.ConfigController
URL: {
config_server_url}/configs/{
appId}/{
clusterName}/{
namespaceName}?releaseKey={
releaseKey}&ip={
clientIp}
Method: GET
// Http Response 返回结果
{
"appId": "100004458",
"cluster": "default",
"namespaceName": "application",
"configurations": {
"portal.elastic.document.type":"biz",
"portal.elastic.cluster.name":"hermes-es-fws"
},
"releaseKey": "20170430092936-dee2d58e74515ff3"
}
NotificationControllerV2アプリケーション対応の構成の更新
Apolloは、Httpロングポーリングに基づいて構成更新のプッシュ通知を提供します。サードパーティのクライアントは、実際のニーズに応じてこの機能を使用する必要があるかどうかを判断できます。
com.ctrip.framework.apollo.configservice.controller.NotificationControllerV2
URL: {
config_server_url}/notifications/v2?appId={
appId}&cluster={
clusterName}¬ifications={
notifications}
Method: GET
// Http Response 返回结果
[
{
"namespaceName": "application",
"notificationId": 101
}
]
// 如果返回的HttpStatus是304,说明配置没有变化
// 如果返回的HttpStauts是200,说明配置有变化,针对变化的namespace重新去服务端拉取配置