{"id":15296120,"url":"https://github.com/nacos-group/nacos-sdk-cpp","last_synced_at":"2025-04-13T09:51:15.247Z","repository":{"id":42204141,"uuid":"280442616","full_name":"nacos-group/nacos-sdk-cpp","owner":"nacos-group","description":"C++ client for Nacos","archived":false,"fork":false,"pushed_at":"2024-01-16T09:05:17.000Z","size":849,"stargazers_count":134,"open_issues_count":33,"forks_count":58,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-27T01:12:28.585Z","etag":null,"topics":["cplusplus","cpp","libnacos-cli","nacos","nacos-sdk","nacos-sdk-cpp"],"latest_commit_sha":null,"homepage":"https://nacos.io","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nacos-group.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-17T14:16:15.000Z","updated_at":"2025-03-19T06:56:12.000Z","dependencies_parsed_at":"2024-06-19T11:25:54.907Z","dependency_job_id":"36d30be0-8f46-437d-b687-0b8314881c0c","html_url":"https://github.com/nacos-group/nacos-sdk-cpp","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacos-group%2Fnacos-sdk-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacos-group%2Fnacos-sdk-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacos-group%2Fnacos-sdk-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nacos-group%2Fnacos-sdk-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nacos-group","download_url":"https://codeload.github.com/nacos-group/nacos-sdk-cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248695300,"owners_count":21146952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cplusplus","cpp","libnacos-cli","nacos","nacos-sdk","nacos-sdk-cpp"],"created_at":"2024-09-30T18:09:27.715Z","updated_at":"2025-04-13T09:51:15.228Z","avatar_url":"https://github.com/nacos-group.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"https://github.com/alibaba/nacos/blob/develop/doc/Nacos_Logo.png\" width=\"50%\" height=\"50%\" /\u003e \n\n[中文版本说明请点这里](https://github.com/nacos-group/nacos-sdk-cpp/blob/master/README_zh_CN.md)\n\n# Nacos-sdk-cpp\n\nNacos-sdk-cpp for c++ client allows users to access Nacos service, it supports service discovery and dynamic configuration.\n\n\n[![Gitter](https://badges.gitter.im/alibaba/nacos.svg)](https://gitter.im/alibaba/nacos?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)   [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n\n\n# Quick Examples\n## Setup project\nDownload the source and run the following command in bash:\n\n```\ncd nacos-sdk-cpp\ncmake .\nmake\n```\n\na libnacos-cli.so and a nacos-cli.out will be generated\n\nrun `./nacos-cli.out` to perform test on the library\n\nrun `make install` to install the libnacos-cli to your system library path\n\n**Note:** You need to run a nacos server on your local machine listening on port 8848 to go through the whole test\nOne of the testcases will test endpoint functionality, so **you also need** to run a simple http server on port 80 which provides the following content:\n\n`127.0.0.1:8848`\n\n**on path /nacos/endpoint0**\n\n**All these examples could be found in nacos-sdk-cpp/examples/**\n\n## Integrate the library into your project\n\nHere is an example showing how to integrate the library(.so) into your project:\n\nIntegratingIntoYourProject.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nint main() {\n    Properties props;\n    props[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1:8848\";//Server address\n    NacosServiceFactory *factory = new NacosServiceFactory(props);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    ResourceGuard \u003cConfigService\u003e _serviceFactory(n);\n    NacosString ss = \"\";\n    try {\n        ss = n-\u003egetConfig(\"k\", NULLSTR, 1000);\n    }\n    catch (NacosException \u0026e) {\n        cout \u003c\u003c\n             \"Request failed with curl code:\" \u003c\u003c e.errorcode() \u003c\u003c endl \u003c\u003c\n             \"Reason:\" \u003c\u003c e.what() \u003c\u003c endl;\n        return -1;\n    }\n    cout \u003c\u003c ss \u003c\u003c endl;\n\n    return 0;\n}\n\n```\n\n`g++ -I/usr/local/include/nacos/ IntegratingIntoYourProject.cpp -lnacos-cli -o integrated.out`\n\nStart a nacos on your localmachine listening on port 8848, and run `./integrated.out`\n\nThen you'll see:\n\n`SuccessfullyIntegrated`\n\n## If you are using a static lib(.a):\nAssume that the file you are compiling resides in the same directory as the .a library, please use the following command:\n\n`g++ -I/usr/local/include/nacos/ IntegratingIntoYourProject.cpp -lcurl -lz -L. -lnacos-cli-static -o integrated.out`\n\n-lcurl -lz Specifies the curl and lz library used by libnacos\n\n-L. -lnacos-cli-static links the static libnacos library resides in the same directory as IntegratingIntoYourProject.cpp\n\n## Configuration\n\n### Get Config\n\ngetConfig.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nint main() {\n    Properties props;\n    props[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1:8848\";//Server address\n    NacosServiceFactory *factory = new NacosServiceFactory(props);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    ResourceGuard \u003cConfigService\u003e _serviceFactory(n);\n    NacosString ss = \"\";\n    try {\n        ss = n-\u003egetConfig(\"k\", NULLSTR, 1000);\n    }\n    catch (NacosException \u0026e) {\n        cout \u003c\u003c\n             \"Request failed with curl code:\" \u003c\u003c e.errorcode() \u003c\u003c endl \u003c\u003c\n             \"Reason:\" \u003c\u003c e.what() \u003c\u003c endl;\n        return -1;\n    }\n    cout \u003c\u003c ss \u003c\u003c endl;\n\n    return 0;\n}\n``` \n\n### Publish Config\n\nsetConfig.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nint main() {\n    Properties props;\n    props[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1:8848\";//server address\n    NacosServiceFactory *factory = new NacosServiceFactory(props);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    ResourceGuard \u003cConfigService\u003e _serviceFactory(n);\n    bool bSucc = false;\n    NacosString ss = \"\";\n\n    try {\n        bSucc = n-\u003epublishConfig(\"Cfg_key\", NULLSTR, \"Cfg_val\");\n        int retry = 0;\n        ss = n-\u003egetConfig(\"Cfg_key\", NULLSTR, 1000);\n        while (!(ss == \"Cfg_val\") \u0026\u0026 retry++ \u003c 10) {\n            ss = n-\u003egetConfig(\"Cfg_key\", NULLSTR, 1000);\n        }\n\n        if (!(ss == \"Cfg_val\")) {\n            throw NacosException(0, \"getConfig() failed.\");\n        }\n    }\n    catch (NacosException \u0026e) {\n        cout \u003c\u003c\n             \"Request failed with curl code:\" \u003c\u003c e.errorcode() \u003c\u003c endl \u003c\u003c\n             \"Reason:\" \u003c\u003c e.what() \u003c\u003c endl;\n\n        return -1;\n    }\n    cout \u003c\u003c \"Publishing Key:Cfg_key with value:Cfg_val result:\" \u003c\u003c bSucc \u003c\u003c endl;\n\n    return 0;\n}\n``` \n\n### Listen to key change \u0026 Cancel listening\n\nlistenToKeys.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nclass MyListener : public Listener {\nprivate:\n    int num;\npublic:\n    MyListener(int num) {\n        this-\u003enum = num;\n    }\n\n    void receiveConfigInfo(const NacosString \u0026configInfo) {\n        cout \u003c\u003c \"===================================\" \u003c\u003c endl;\n        cout \u003c\u003c \"Watcher\" \u003c\u003c num \u003c\u003c endl;\n        cout \u003c\u003c \"Watched Key UPDATED:\" \u003c\u003c configInfo \u003c\u003c endl;\n        cout \u003c\u003c \"===================================\" \u003c\u003c endl;\n    }\n};\n\nint main() {\n    Properties props;\n    props[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1:8848\";\n    NacosServiceFactory *factory = new NacosServiceFactory(props);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    ResourceGuard \u003cConfigService\u003e _serviceFactory(n);\n\n    MyListener *theListener = new MyListener(1);//You don't need to free it, since it will be deleted by the function removeListener\n    n-\u003eaddListener(\"dqid\", NULLSTR, theListener);//All changes on the key dqid will be received by MyListener\n\n    cout \u003c\u003c \"Input a character to continue\" \u003c\u003c endl;\n    getchar();\n    cout \u003c\u003c \"remove listener\" \u003c\u003c endl;\n    n-\u003eremoveListener(\"dqid\", NULLSTR, theListener);//Cancel listening\n    getchar();\n\n    return 0;\n}\n```\n\n## Naming\n\n### Register Instance \u0026 Unregister Instance\n\nregisterInstances.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \u003cunistd.h\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nint main() {\n    Properties configProps;\n    configProps[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1\";\n    NacosServiceFactory *factory = new NacosServiceFactory(configProps);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    NamingService *namingSvc = factory-\u003eCreateNamingService();\n    ResourceGuard \u003cNamingService\u003e _serviceFactory(namingSvc);\n    Instance instance;\n    instance.clusterName = \"DefaultCluster\";\n    instance.ip = \"127.0.0.1\";\n    instance.port = 2333;\n    instance.instanceId = \"1\";\n    instance.ephemeral = true;\n\n    //Registers 5 services named TestNamingService1...5\n    try {\n        for (int i = 0; i \u003c 5; i++) {\n            NacosString serviceName = \"TestNamingService\" + NacosStringOps::valueOf(i);\n            instance.port = 2000 + i;\n            namingSvc-\u003eregisterInstance(serviceName, instance);\n        }\n    }\n    catch (NacosException \u0026e) {\n        cout \u003c\u003c \"encounter exception while registering service instance, raison:\" \u003c\u003c e.what() \u003c\u003c endl;\n        return -1;\n    }\n    sleep(30);\n    try {\n        for (int i = 0; i \u003c 5; i++) {\n            NacosString serviceName = \"TestNamingService\" + NacosStringOps::valueOf(i);\n\n            namingSvc-\u003ederegisterInstance(serviceName, \"127.0.0.1\", 2000 + i);\n            sleep(1);\n        }\n    }\n    catch (NacosException \u0026e) {\n        cout \u003c\u003c \"encounter exception while registering service instance, raison:\" \u003c\u003c e.what() \u003c\u003c endl;\n        return -1;\n    }\n    sleep(30);\n\n    return 0;\n}\n```\n\n### Subscribe \u0026 Unsubscribe\n\nsubscribeServices.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nclass MyServiceListener : public EventListener {\nprivate:\n    int num;\npublic:\n    MyServiceListener(int num) {\n        this-\u003enum = num;\n    }\n\n    void receiveNamingInfo(const ServiceInfo \u0026serviceInfo){\n        cout \u003c\u003c \"===================================\" \u003c\u003c endl;\n        cout \u003c\u003c \"Watcher: \" \u003c\u003c num \u003c\u003c endl;\n        cout \u003c\u003c \"Watched service UPDATED: \" \u003c\u003c serviceInfo.toInstanceString() \u003c\u003c endl;\n        cout \u003c\u003c \"===================================\" \u003c\u003c endl;\n\n    }\n};\n\nint main() {\n    Properties props;\n    props[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1:8848\";\n    //Interval for poller to check the status of subscribed services(unit:Ms), 30000 by default\n    //Here we set it to 5000 to see the output more quick\n    props[PropertyKeyConst::SUBSCRIPTION_POLL_INTERVAL] = \"5000\";\n    NacosServiceFactory *factory = new NacosServiceFactory(props);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    NamingService *n = factory-\u003eCreateNamingService();\n    ResourceGuard \u003cNamingService\u003e _serviceFactory(n);\n\n    n-\u003esubscribe(\"ss\", new MyServiceListener(1));\n    cout \u003c\u003c \"Press any key to register services\" \u003c\u003c endl;\n    getchar();\n\n    n-\u003eregisterInstance(\"ss\", \"127.0.0.1\", 33);\n    n-\u003eregisterInstance(\"ss\", \"127.0.0.1\", 34);\n    cout \u003c\u003c \"Press any key to deregister services\" \u003c\u003c endl;\n    getchar();\n\n    n-\u003ederegisterInstance(\"ss\", \"127.0.0.1\", 33);\n    n-\u003ederegisterInstance(\"ss\", \"127.0.0.1\", 34);\n    cout \u003c\u003c \"All instances Unregistered, press any key to finish testing\" \u003c\u003c endl;\n    getchar();\n\n    return 0;\n}\n```\n\n### Get all instances of a service\n\ngetAllInstances.cpp:\n```C++\n#include \u003ciostream\u003e\n#include \u003clist\u003e\n#include \"Nacos.h\"\n\nusing namespace std;\nusing namespace nacos;\n\nint main() {\n    Properties configProps;\n    configProps[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1\";\n    NacosServiceFactory *factory = new NacosServiceFactory(configProps);\n    ResourceGuard \u003cNacosServiceFactory\u003e _guardFactory(factory);\n    NamingService *namingSvc = factory-\u003eCreateNamingService();\n    ResourceGuard \u003cNamingService\u003e _guardService(namingSvc);\n\n    list \u003cInstance\u003e instances = namingSvc-\u003egetAllInstances(\"TestNamingService1\");\n    cout \u003c\u003c \"getAllInstances from server:\" \u003c\u003c endl;\n    for (list\u003cInstance\u003e::iterator it = instances.begin();\n         it != instances.end(); it++) {\n        cout \u003c\u003c \"Instance:\" \u003c\u003c it-\u003etoString() \u003c\u003c endl;\n    }\n\n    return 0;\n}\n```\n\n### Enabling Authentication\n\nIf your Nacos server is secured with password, you can add the following snippet to any of the examples above to enable authentication:\n\n```C++\nusing namespace nacos;\n......\n    configProps[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1\";\n    configProps[PropertyKeyConst::AUTH_USERNAME] = \"username\";\n    configProps[PropertyKeyConst::AUTH_PASSWORD] = \"password\";\n    NacosServiceFactory *factory = new NacosServiceFactory(configProps);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    NamingService *namingSvc = factory-\u003eCreateNamingService();\n......\n```\n\n### Enabling SPAS Authentication\n\n```C++\nusing namespace nacos;\n......\n    configProps[PropertyKeyConst::SERVER_ADDR] = \"127.0.0.1\";\n    configProps[PropertyKeyConst::ACCESS_KEY] = \"accessKey\";\n    configProps[PropertyKeyConst::SECRET_KEY] = \"secretKey\";\n    NacosServiceFactory *factory = new NacosServiceFactory(configProps);\n    ConfigService *n = factory-\u003eCreateConfigService();\n    NamingService *namingSvc = factory-\u003eCreateNamingService();\n......\n```\n\n# Supported System/Compilers\n\n| OS/Environment | Compilers | Tested version |\n| ---- | ---- | ---- |\n|MacOS Darwin 19.6.0 x86_64|Clang|Apple clang version 12.0.0 (clang-1200.0.26.2)|\n|Windows 10 WSL|GCC|version 4.8.4|\n|Windows 10 CYGWIN_NT-10.0 x86_64|GCC|version 10.2.0 (GCC)|\n|Ubuntu1~16.04.12|GCC|version 5.4.0|\n|CentOS|GCC||\n|Windows|Visual C++|To be done|\n\n\n# About Nacos\n\nNacos (official site: [http://nacos.io](http://nacos.io)) is an easy-to-use platform designed for dynamic service discovery and configuration and service management. It helps you to build cloud native applications and microservices platform easily.\n\nService is a first-class citizen in Nacos. Nacos supports almost all type of services, for example: [Dubbo/gRPC service](https://nacos.io/en-us/docs/use-nacos-with-dubbo.html), [Spring Cloud RESTFul service](https://nacos.io/en-us/docs/use-nacos-with-springcloud.html) and [Kubernetes service](https://nacos.io/en-us/docs/use-nacos-with-kubernetes.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacos-group%2Fnacos-sdk-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnacos-group%2Fnacos-sdk-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnacos-group%2Fnacos-sdk-cpp/lists"}