NotificationControllerV2:notifications/v2:实现长轮循,监听配置变更、返回,结束长轮循

NotificationControllerV2:notifications/v2:实现长轮循,监听配置变更、返回,结束长轮循

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.链接: 参考资料.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Onesignal API的参数和返回值如下: **参数:** - app_id (string, required):OneSignal应用程序的ID。 - contents (object, optional):通知的消息内容,可以是纯文本字符串或包含多个文本字段的对象。例如:`{"en": "English Message", "es": "Mensaje en español"}`。 - headings (object, optional):通知的消息标题,可以是纯文本字符串或包含多个文本字段的对象。例如:`{"en": "English Title", "es": "Título en español"}`。 - subtitle (object, optional):通知的消息副标题,可以是纯文本字符串或包含多个文本字段的对象。例如:`{"en": "English Subtitle", "es": "Subtítulo en español"}`。 - template_id (string, optional):使用OneSignal模板的ID,以自定义通知的外观和功能。 - included_segments (array, optional):要发送通知的用户的段列表。 - excluded_segments (array, optional):不要发送通知的用户的段列表。 - include_player_ids (array, optional):要发送通知的单个用户的ID列表。 - included_ios_badge_count (number, optional):在iOS上显示的徽章计数。 - android_channel_id (string, optional):在Android上使用的通知渠道的ID。 - small_icon (string, optional):在通知中使用的小图标的名称或资源ID。 - large_icon (string, optional):在通知中使用的大图标的名称或资源ID。 - big_picture (string, optional):在通知中使用的大图片的URL地址。 - buttons (array, optional):在通知中显示的按钮列表。每个按钮都包含文本和一个打开URL或执行操作的URL。 - web_buttons (array, optional):在浏览器中打开的按钮列表。每个按钮都包含文本和一个打开URL的URL。 - ios_attachments (object, optional):在iOS上使用的附件。可以是单个文件或多个文件的对象。例如:`{"id1": "https://example.com/image.png", "id2": "https://example.com/audio.mp3"}`。 - android_background_layout (object, optional):在Android上使用的后台布局。可以是纯文本字符串或包含多个文本字段的对象。例如:`{"headings_color": "FFFF0000", "contents_color": "FF00FF00"}`。 - ttl (number, optional):通知的生存时间(以秒为单位)。 - priority (number, optional):通知的优先级。值为-2、-1、0、1或2。 - sound (string, optional):要在通知中播放的声音的名称或资源ID。 - android_led_color (string, optional):在Android上使用的LED颜色。 - android_accent_color (string, optional):在Android上使用的强调颜色。 - android_visibility (number, optional):在Android上通知的可见性。值为0、1或-1。 - ios_sound (string, optional):要在iOS上播放的声音的名称或资源ID。 - ios_badgeType (string, optional):在iOS上使用的徽章类型。值为none、setTo、increment或decrement。 - ios_badgeCount (number, optional):在iOS上显示的徽章计数。 - ios_category (string, optional):在iOS上使用的通知类别。 - content_available (boolean, optional):指示是否应在iOS上启用静默通知。 - mutable_content (boolean, optional):指示是否应在iOS上启用可变通知。 - url (string, optional):在通知中包含的URL地址。 **返回值:** - id (string):通知的唯一ID。 - recipients (number):接收通知的用户数。 - errors (array):发送通知时出现的任何错误。每个错误都是一个字符串,包含出错的用户ID和错误描述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值