LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code and KPI, Keep progress,make a better result.
Survive during the day and develop at night。
目录
概 述
NotificationControllerV2实现了ReleaseMessageListener,具体什么时候注册的监听器请参考:ConfigServiceAutoConfiguration
/**
* 监听器注册,namespaces变更后接收消息
* @return
*/
@Bean
public ReleaseMessageScanner releaseMessageScanner() {
ReleaseMessageScanner releaseMessageScanner = new ReleaseMessageScanner();
//0. handle release message cache
releaseMessageScanner.addMessageListener(releaseMessageServiceWithCache);
//1. handle gray release rule
releaseMessageScanner.addMessageListener(grayReleaseRulesHolder);
//2. handle server cache
releaseMessageScanner.addMessageListener(configService);
releaseMessageScanner.addMessageListener(configFileController);
//3. notify clients
releaseMessageScanner.addMessageListener(notificationControllerV2);
releaseMessageScanner.addMessageListener(notificationController);
return releaseMessageScanner;
}
进入主流程2.1客户端请求,ConfigSerivce通知配置变更,这里是利用Spring DeferredResult实现长轮循
@GetMapping
public DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> pollNotification(
@RequestParam(value = "appId") String appId,
@RequestParam(value = "cluster") String cluster,
@RequestParam(value = "notifications") String notificationsAsString,
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "ip", required = false) String clientIp) {
List<ApolloConfigNotification> notifications = null;
try {
//因为一个客户端可以订阅过个namespace所以是list
notifications =
gson.fromJson(notificationsAsString, notificationsTypeReference);
} catch (Throwable ex) {
Tracer.logError(ex);
}
if (CollectionUtils.isEmpty(notifications)) {
throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
}
DeferredResultWrapper deferredResultWrapper = new DeferredResultWrapper(bizConfig.longPollingTimeoutInMilli());
//namespaces集合
Set<String> namespaces = Sets.newHashSet();
//客户端通知的map,key:namespaces名;value:通知编号
Map<String, Long> clientSideNotifications = Maps.newHashMap();
//key:namespaces名
//过滤ApolloConfigNotification
Map<String, ApolloConfigNotification> filteredNotifications = filterNotifications(appId, notifications);
for (Map.Entry<String, ApolloConfigNotification> notificationEntry : filteredNotifications.entrySet()) {
String normalizedNamespace = notificationEntry.getKey();
ApolloConfigNotification notification = notificationEntry.getValue();
//添加到namespaces里
namespaces.add(normalizedNamespace);
//添加通知客户端map
clientSideNotifications.put(normalizedNamespace, notification.getNotificationId());
// 记录名字被归一化的 Namespace 。因为,最终返回给客户端,使用原始的 Namespace 名字,否则客户端无法识别。
if (!Objects.equals(notification.getNamespaceName(), normalizedNamespace)) {
deferredResultWrapper.recordNamespaceNameNormalizedResult(notification.getNamespaceName(), normalizedNamespace);
}
}
if (CollectionUtils.isEmpty(namespaces)) {
throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
}
//组装Watch key集合
Multimap<String, String> watchedKeysMap =
watchKeysUtil.assembleAllWatchKeys(appId, cluster, namespaces, dataCenter);
//生成watch key 集合
Set<String> watchedKeys = Sets.newHashSet(watchedKeysMap.values());
deferredResultWrapper
.onTimeout(() -> logWatchedKeys(watchedKeys, "Apollo.LongPoll.TimeOutKeys"));
//注册结束事件
deferredResultWrapper.onCompletion(() -> {
//unregister all keys
//移除watch key+deferredResultWrapper出deferredResults
for (String key : watchedKeys) {
deferredResults.remove(key, deferredResultWrapper);
}
logWatchedKeys(watchedKeys, "Apollo.LongPoll.CompletedKeys");
});
for (String key : watchedKeys) {
this.deferredResults.put(key, deferredResultWrapper);
}
entityManagerUtil.closeEntityManager();
//获取最新的ApolloConfigNotification集合
List<ApolloConfigNotification> newNotifications =
getApolloConfigNotifications(namespaces, clientSideNotifications, watchedKeysMap,
latestReleaseMessages);
// 若有更新直接设置结果
if (!CollectionUtils.isEmpty(newNotifications)) {
deferredResultWrapper.setResult(newNotifications);
}
return deferredResultWrapper.getResult();
}
``
我们重点看一下getApolloConfigNotifications():获取最新的ApolloConfigNotification集合:
`
小结
NotificationControllerV2:notifications/v2:实现长轮循,监听配置变更、返回,结束长轮循
参考资料和推荐阅读
1.链接: 参考资料.