All Projects → nacos-group → nacos-sdk-cpp

nacos-group / nacos-sdk-cpp

Licence: Apache-2.0 License
C++ client for Nacos

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to nacos-sdk-cpp

xmutca-rpc
Xmutca-rpc是一个基于netty开发的分布式服务框架,提供稳定高性能的RPC远程服务调用功能,支持注册中心,服务治理,负载均衡等特性,开箱即用。
Stars: ✭ 18 (-70%)
Mutual labels:  nacos
DiscoveryPlatform
☀️ Nepxion DiscoveryPlatform is a platform for Nepxion Discovery with service governance, release orchestration, flow inspection, instance blacklist, gateway route 服务治理、蓝绿灰度编排、流量侦测、实例摘除、网关路由的平台
Stars: ✭ 63 (+5%)
Mutual labels:  nacos
java-best-practice
Java学习例子,最佳实践
Stars: ✭ 19 (-68.33%)
Mutual labels:  nacos
mall4cloud
⭐️⭐️⭐️ Springcloud商城 O2O商城 小程序商城 PC商城 H5商城 APP商城 Java商城 分销商城 多用户商城 uniapp商城 微服务商城
Stars: ✭ 3,915 (+6425%)
Mutual labels:  nacos
midwayjs-crud
基于 Typescript+MidwayJs+Nacos 的微服务开发架构
Stars: ✭ 45 (-25%)
Mutual labels:  nacos
docker-case
这个项目主要是为了快速拉起docker服务
Stars: ✭ 31 (-48.33%)
Mutual labels:  nacos
sparkzxl-framework
sparkzxl 基于spring boot的组件库
Stars: ✭ 20 (-66.67%)
Mutual labels:  nacos
piccolo
Netty4长连接网关
Stars: ✭ 19 (-68.33%)
Mutual labels:  nacos
php-nacos
阿里巴巴nacos配置中心-PHP客户端
Stars: ✭ 167 (+178.33%)
Mutual labels:  nacos
CoSky
High-performance, low-cost microservice governance platform. Service Discovery and Configuration Service | 高性能、低成本微服务治理平台
Stars: ✭ 57 (-5%)
Mutual labels:  nacos
spring-cloud
🔥 Develop distributed application services based on SpringCloud architecture model and components
Stars: ✭ 36 (-40%)
Mutual labels:  nacos
skycloud-base
🔥springcloud脚手架,配置中心(apollo/nacos) 注册中心(consul/nacos) 分布式事物(seata) 调用链(skywalking) 日志(ELK)监控(prometheus与grafana) 等,适合学习与快速开发使用
Stars: ✭ 80 (+33.33%)
Mutual labels:  nacos
taotao-cloud-project
微服务开发脚手架,包括大数据模块、微服务模块、前端模块。基于Spring Cloud Alibaba的微服务架构。提供技术框架的基础能力的封装,减少开发工作,只关注业务,包含了工作以来的工作总结和技术沉淀
Stars: ✭ 76 (+26.67%)
Mutual labels:  nacos
learn
一个学习使用的综合项目。实现方案为spring cloud alibaba
Stars: ✭ 38 (-36.67%)
Mutual labels:  nacos
yiying-parent
在线电影,基于分布式微服务架构,技术架构有SpringBoot、SpringCoud、nacos、dubbo、mybatis-plus、Druid,采用前后端分离方式进行开发,实现自定义视频上传、解码、存储、点播
Stars: ✭ 48 (-20%)
Mutual labels:  nacos
dnmp
docker-compose部署LNMP环境 Nginx/Openresty、MySQL(5.7、8.0、8.1)、PHP7.4(8.0、5.6)、Redis5.0、PHPMyAdmin、Xdebug、RabbitMQ、Nacos
Stars: ✭ 138 (+130%)
Mutual labels:  nacos
spring-cloud-alibaba-component
Sample of Spring Cloud Alibaba component
Stars: ✭ 50 (-16.67%)
Mutual labels:  nacos
Java Note
后端研发——笔记
Stars: ✭ 54 (-10%)
Mutual labels:  nacos
sca-best-practice
Provide microservice API for HanLP
Stars: ✭ 13 (-78.33%)
Mutual labels:  nacos
nacos-python-sdk
python使用nacos的微服务sdk,以及python的熔断器和限流器
Stars: ✭ 28 (-53.33%)
Mutual labels:  nacos

中文版本说明请点这里

Nacos-sdk-cpp

Nacos-sdk-cpp for c++ client allows users to access Nacos service, it supports service discovery and dynamic configuration.

Gitter License

Quick Examples

Setup project

Download the source and run the following command in bash:

cd nacos-sdk-cpp
cmake .
make

a libnacos-cli.so and a nacos-cli.out will be generated

run ./nacos-cli.out to perform test on the library

run make install to install the libnacos-cli to your system library path

Note: You need to run a nacos server on your local machine listening on port 8848 to go through the whole test One 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:

127.0.0.1:8848

on path /nacos/endpoint0

All these examples could be found in nacos-sdk-cpp/examples/

Integrate the library into your project

Here is an example showing how to integrate the library(.so) into your project:

IntegratingIntoYourProject.cpp:

#include <iostream>
#include "Nacos.h"

using namespace std;
using namespace nacos;

int main() {
    Properties props;
    props[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1:8848";//Server address
    NacosServiceFactory *factory = new NacosServiceFactory(props);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    ConfigService *n = factory->CreateConfigService();
    ResourceGuard <ConfigService> _serviceFactory(n);
    NacosString ss = "";
    try {
        ss = n->getConfig("k", NULLSTR, 1000);
    }
    catch (NacosException &e) {
        cout <<
             "Request failed with curl code:" << e.errorcode() << endl <<
             "Reason:" << e.what() << endl;
        return -1;
    }
    cout << ss << endl;

    return 0;
}

g++ -I/usr/local/include/nacos/ IntegratingIntoYourProject.cpp -lnacos-cli -o integrated.out

Start a nacos on your localmachine listening on port 8848, and run ./integrated.out

Then you'll see:

SuccessfullyIntegrated

If you are using a static lib(.a):

Assume that the file you are compiling resides in the same directory as the .a library, please use the following command:

g++ -I/usr/local/include/nacos/ IntegratingIntoYourProject.cpp -lcurl -lz -L. -lnacos-cli-static -o integrated.out

-lcurl -lz Specifies the curl and lz library used by libnacos

-L. -lnacos-cli-static links the static libnacos library resides in the same directory as IntegratingIntoYourProject.cpp

Configuration

Get Config

getConfig.cpp:

#include <iostream>
#include "Nacos.h"

using namespace std;
using namespace nacos;

int main() {
    Properties props;
    props[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1:8848";//Server address
    NacosServiceFactory *factory = new NacosServiceFactory(props);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    ConfigService *n = factory->CreateConfigService();
    ResourceGuard <ConfigService> _serviceFactory(n);
    NacosString ss = "";
    try {
        ss = n->getConfig("k", NULLSTR, 1000);
    }
    catch (NacosException &e) {
        cout <<
             "Request failed with curl code:" << e.errorcode() << endl <<
             "Reason:" << e.what() << endl;
        return -1;
    }
    cout << ss << endl;

    return 0;
}

Publish Config

setConfig.cpp:

#include <iostream>
#include "Nacos.h"

using namespace std;
using namespace nacos;

int main() {
    Properties props;
    props[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1:8848";//server address
    NacosServiceFactory *factory = new NacosServiceFactory(props);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    ConfigService *n = factory->CreateConfigService();
    ResourceGuard <ConfigService> _serviceFactory(n);
    bool bSucc = false;
    NacosString ss = "";

    try {
        bSucc = n->publishConfig("Cfg_key", NULLSTR, "Cfg_val");
        int retry = 0;
        ss = n->getConfig("Cfg_key", NULLSTR, 1000);
        while (!(ss == "Cfg_val") && retry++ < 10) {
            ss = n->getConfig("Cfg_key", NULLSTR, 1000);
        }

        if (!(ss == "Cfg_val")) {
            throw NacosException(0, "getConfig() failed.");
        }
    }
    catch (NacosException &e) {
        cout <<
             "Request failed with curl code:" << e.errorcode() << endl <<
             "Reason:" << e.what() << endl;

        return -1;
    }
    cout << "Publishing Key:Cfg_key with value:Cfg_val result:" << bSucc << endl;

    return 0;
}

Listen to key change & Cancel listening

listenToKeys.cpp:

#include <iostream>
#include "Nacos.h"

using namespace std;
using namespace nacos;

class MyListener : public Listener {
private:
    int num;
public:
    MyListener(int num) {
        this->num = num;
    }

    void receiveConfigInfo(const NacosString &configInfo) {
        cout << "===================================" << endl;
        cout << "Watcher" << num << endl;
        cout << "Watched Key UPDATED:" << configInfo << endl;
        cout << "===================================" << endl;
    }
};

int main() {
    Properties props;
    props[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1:8848";
    NacosServiceFactory *factory = new NacosServiceFactory(props);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    ConfigService *n = factory->CreateConfigService();
    ResourceGuard <ConfigService> _serviceFactory(n);

    MyListener *theListener = new MyListener(1);//You don't need to free it, since it will be deleted by the function removeListener
    n->addListener("dqid", NULLSTR, theListener);//All changes on the key dqid will be received by MyListener

    cout << "Input a character to continue" << endl;
    getchar();
    cout << "remove listener" << endl;
    n->removeListener("dqid", NULLSTR, theListener);//Cancel listening
    getchar();

    return 0;
}

Naming

Register Instance & Unregister Instance

registerInstances.cpp:

#include <iostream>
#include <unistd.h>
#include "Nacos.h"

using namespace std;
using namespace nacos;

int main() {
    Properties configProps;
    configProps[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1";
    NacosServiceFactory *factory = new NacosServiceFactory(configProps);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    NamingService *namingSvc = factory->CreateNamingService();
    ResourceGuard <NamingService> _serviceFactory(namingSvc);
    Instance instance;
    instance.clusterName = "DefaultCluster";
    instance.ip = "127.0.0.1";
    instance.port = 2333;
    instance.instanceId = "1";
    instance.ephemeral = true;

    //Registers 5 services named TestNamingService1...5
    try {
        for (int i = 0; i < 5; i++) {
            NacosString serviceName = "TestNamingService" + NacosStringOps::valueOf(i);
            instance.port = 2000 + i;
            namingSvc->registerInstance(serviceName, instance);
        }
    }
    catch (NacosException &e) {
        cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
        return -1;
    }
    sleep(30);
    try {
        for (int i = 0; i < 5; i++) {
            NacosString serviceName = "TestNamingService" + NacosStringOps::valueOf(i);

            namingSvc->deregisterInstance(serviceName, "127.0.0.1", 2000 + i);
            sleep(1);
        }
    }
    catch (NacosException &e) {
        cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
        return -1;
    }
    sleep(30);

    return 0;
}

Subscribe & Unsubscribe

subscribeServices.cpp:

#include <iostream>
#include "Nacos.h"

using namespace std;
using namespace nacos;

class MyServiceListener : public EventListener {
private:
    int num;
public:
    MyServiceListener(int num) {
        this->num = num;
    }

    void receiveNamingInfo(const ServiceInfo &serviceInfo){
        cout << "===================================" << endl;
        cout << "Watcher: " << num << endl;
        cout << "Watched service UPDATED: " << serviceInfo.toInstanceString() << endl;
        cout << "===================================" << endl;

    }
};

int main() {
    Properties props;
    props[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1:8848";
    //Interval for poller to check the status of subscribed services(unit:Ms), 30000 by default
    //Here we set it to 5000 to see the output more quick
    props[PropertyKeyConst::SUBSCRIPTION_POLL_INTERVAL] = "5000";
    NacosServiceFactory *factory = new NacosServiceFactory(props);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    NamingService *n = factory->CreateNamingService();
    ResourceGuard <NamingService> _serviceFactory(n);

    n->subscribe("ss", new MyServiceListener(1));
    cout << "Press any key to register services" << endl;
    getchar();

    n->registerInstance("ss", "127.0.0.1", 33);
    n->registerInstance("ss", "127.0.0.1", 34);
    cout << "Press any key to deregister services" << endl;
    getchar();

    n->deregisterInstance("ss", "127.0.0.1", 33);
    n->deregisterInstance("ss", "127.0.0.1", 34);
    cout << "All instances Unregistered, press any key to finish testing" << endl;
    getchar();

    return 0;
}

Get all instances of a service

getAllInstances.cpp:

#include <iostream>
#include <list>
#include "Nacos.h"

using namespace std;
using namespace nacos;

int main() {
    Properties configProps;
    configProps[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1";
    NacosServiceFactory *factory = new NacosServiceFactory(configProps);
    ResourceGuard <NacosServiceFactory> _guardFactory(factory);
    NamingService *namingSvc = factory->CreateNamingService();
    ResourceGuard <NamingService> _guardService(namingSvc);

    list <Instance> instances = namingSvc->getAllInstances("TestNamingService1");
    cout << "getAllInstances from server:" << endl;
    for (list<Instance>::iterator it = instances.begin();
         it != instances.end(); it++) {
        cout << "Instance:" << it->toString() << endl;
    }

    return 0;
}

Enabling Authentication

If your Nacos server is secured with password, you can add the following snippet to any of the examples above to enable authentication:

using namespace nacos;
......
    configProps[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1";
    configProps[PropertyKeyConst::AUTH_USERNAME] = "username";
    configProps[PropertyKeyConst::AUTH_PASSWORD] = "password";
    NacosServiceFactory *factory = new NacosServiceFactory(configProps);
    ConfigService *n = factory->CreateConfigService();
    NamingService *namingSvc = factory->CreateNamingService();
......

Enabling SPAS Authentication

using namespace nacos;
......
    configProps[PropertyKeyConst::SERVER_ADDR] = "127.0.0.1";
    configProps[PropertyKeyConst::ACCESS_KEY] = "accessKey";
    configProps[PropertyKeyConst::SECRET_KEY] = "secretKey";
    NacosServiceFactory *factory = new NacosServiceFactory(configProps);
    ConfigService *n = factory->CreateConfigService();
    NamingService *namingSvc = factory->CreateNamingService();
......

Supported System/Compilers

OS/Environment Compilers Tested version
MacOS Darwin 19.6.0 x86_64 Clang Apple clang version 12.0.0 (clang-1200.0.26.2)
Windows 10 WSL GCC version 4.8.4
Windows 10 CYGWIN_NT-10.0 x86_64 GCC version 10.2.0 (GCC)
Ubuntu1~16.04.12 GCC version 5.4.0
CentOS GCC
Windows Visual C++ To be done

About Nacos

Nacos (official site: 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.

Service is a first-class citizen in Nacos. Nacos supports almost all type of services, for example: Dubbo/gRPC service, Spring Cloud RESTFul service and Kubernetes service.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].