edit-icon download-icon

ACM C++ SDK

Last Updated: Jul 16, 2018

This topic explains how to use ACM C++ SDK.

Note: ACM C++ SDK only supports Linux platform.

Install ACM C++ SDK

  1. Download SDK dependency packages: ACM C++ SDK
  2. Extract the downloaded package to create the following directory structure:

    • example/
    • include/
    • lib/

    The preceding directories and files serve the following purposes:

    1. example: acm.cpp demonstrates how to use SDK. Makefile compiles and manages the example directory.
    2. include: the header files to be included in your own program.
    3. lib: The directory contains the 64-bit static library and dynamic library.
  3. The environment variable LD_LIBRARY_PATH specifies the ACM dynamic library search path, which is also the path for installation.

    1. export LD_LIBRARY_PATH=/usr/lib64:/usr/lib:/lib:/lib64:../lib
  4. Go to the example directory and modify the start parameter of the acm.cpp file and the dataId and group parameters. Run the make command for compiling. Run the following sample code:

    1. cd example //Go to the example directory
    2. vim acm.cpp //Modify the acm.cpp file
    3. make //Compile the sample code
    4. ./acm //Run the sample code

Sample code

Run the following code in your project for configuration management:

  1. #include "ACM.h"
  2. using namespace std;
  3. using namespace acm;
  4. //Define the listener
  5. class MyListener : public ManagerListener
  6. {
  7. public:
  8. MyListener(const std::string& data_id, const std::string& group):data_id_(data_id),group_(group){}
  9. virtual ~MyListener()
  10. {}
  11. virtual void getExecutor()
  12. {
  13. printf("data_id:%s group:%s getExecutor\n",data_id_.c_str(), group_.c_str());
  14. }
  15. //Callback during configuration modification.
  16. virtual void receiveConfigInfo( std::string &configInfo)
  17. {
  18. printf("data_id:%s group:%s configInfo:\n%s\n", data_id_.c_str(), group_.c_str(), configInfo.c_str());
  19. config_ = configInfo;
  20. }
  21. private:
  22. std::string data_id_;
  23. std::string group_;
  24. std::string config_;
  25. };
  26. int main() {
  27. ACM::init("acm.aliyun.com", // endpoint: Can be obtained from the ACM console.
  28. "***************", // namespace: Can be obtained from the ACM console.
  29. "***************", // accessKey: Can be obtained from the ACM console.
  30. "***************"); // secretKey: Can be obtained from the ACM console.
  31. //The dataId and group of the configuration that is listened for
  32. std::string dataId = "${dataId}";
  33. std::string group = "${group}";
  34. std::string content;
  35. //Actively pull the configuration
  36. ACM::getConfig(dataId, group, 5000, content);
  37. printf("get ok config %s\n", content.c_str());
  38. MyListener* listener = new MyListener(dataId, group);
  39. //Listen for configuration changes
  40. ACM::addListener(dataId, group, listener);
  41. printf("add listener ok %s %s\n", dataId.c_str(), group.c_str());
  42. do {
  43. printf("input q to quit\n");
  44. } while (getchar() != 'q');
  45. return 0;
  46. }

Interface description

Initialization setting interface

Set endpoint, nameSpace, accessKey, and secretKey.

  1. static void init(const char* endpoint,
  2. const char* nameSpace,
  3. char* accessKey,
  4. char* secretKey);

Parameters are described as follows:

Parameter Parameter type Description
endpoint const char* ACM domain name, which is obtained from the console
nameSpace const char* Namespace, which is obtained from the console
accessKey char* accessKey, which is obtained from the console
secretKey char* secretKey, which is obtained from the console

Interface for getting configurations

Obtains configurations from ACM when the service starts.

  1. static bool getConfig(const std::string &dataId,
  2. const std::string &group,
  3. int timeoutMs,
  4. std::string &content);

Parameters are described as follows:

Parameter Parameter type Description
dataId const string Configuration ID. Use a naming rule like package.class (for example, com.taobao.tc.refund.log.level) to ensure global uniqueness. It is recommended to indicate business meaning of the configuration in the “class” section. All characters must be in lower case. Only English characters and four special characters (“.” , “:” , “-“ , and “_”) are allowed. It must not exceed 256 bytes.
group const string Configuration group. We recommend that you use “product name: module name” (for example ACM: Test) to guarantee the uniqueness. Only English characters and four special characters (“.” , “:” , “-“ , and “_”) are allowed. It must not exceed 128 bytes.
timeoutMs int Time-out period
content string Returned configuration content

Listening to configuration interface

If you want ACM to push configuration changes, you can use the ACM dynamic configuration listening interface.

  1. static void addListener(const std::string &dataId,
  2. const std::string &group,
  3. ManagerListener* listener);

Parameters are described as follows:

Parameter Parameter type Description
dataId const string dataId
group const string group
listener ManagerListener Listener. The listener callback function will be run when configuration is modified.

Cancel configuration listening interface

Cancels configuration listening when the service starts.

  1. static void removeListener(const std::string &dataId,
  2. const std::string &group,
  3. ManagerListener* listener);

Parameters are described as follows:

Parameter Parameter type Description
dataId const string dataId
group const string group
listener ManagerListener Listener to be canceled
Thank you! We've received your feedback.