Wii
Wii
发布于 2025-08-26 / 12 阅读
0
0

Apollo Client (C++)

背景

来到易点之后,并没有什么 C++ 的语言环境,配置中心依赖 Apollo,又苦于没有合适的 Apollo Client 可用,故周末抽出一些时间,写了一个简单的 C++ 版本的 Apollo Client

功能

目前只实现了简单的读和订阅功能。

class ApolloClient {
public:
  struct Meta {
    SubscribeMeta meta;
    bool is_running{true};
    std::thread td;
  };
  ApolloClient(const ApolloClientOptions &options); //< Construct
  ~ApolloClient();                                  //< Destructor

  /**
   * @brief Get all properties of the namespace (with cache)
   * @param [in] nmspace namespace
   * @param [in] ttl_s WANR: NOT IMPLEMENT FOR NOW. maximum allowable delay
   * time in seconds, the value 0 means query
   * @return the properties of the namespace
   * immediately
   */
  Properties GetProperties(const std::string &nmspace, int ttl_s = 60);

  Properties GetPropertiesFromCache(const std::string &nmspace);

  /**
   * @brief Get all properties without cache
   * @param [in] nmspace namespace
   * @return the properties of the namespace
   */
  Properties GetPropertiesDirectly(const std::string &nmspace);

  /**
   * @brief subscribe a namespace, and callback function will be called when
   * namespace's properties changed
   * @param [in] subscribe meta data
   * @param [in] callback function. function definaltion see @ref
   * NotifyFunction
   * @return subscribe id
   */
  int Subscribe(SubscribeMeta &&meta, NotifyFunction &&callback);

  /**
   * @brief unscbscribe
   * @param subscribe_id subscribe id
   */
  void Unsubscribe(int subscribe_id);
};

应用

目前在生产环境用起来没发现什么问题,订阅用了 long polling 代替轮询。


评论