C++ 注册Nacos

下载源码:

git clone GitHub - nacos-group/nacos-sdk-cpp: C++ client for Nacos

编译源码

cd nacos-sdk-cpp

cmake .

make

生成库文件

在nacos-sdk-cpp 下

注册nacos

将include  和libnacos-cli.so libnacos-cli-static.a 放入你的工程

如果Nacos服务地址:192.168.1.124:8848

如果你的c++服务配置为

地址:192.168.1.131

端口:9000

服务名:TestCppService

在你的工程中添加如下代码

注册实例

头文件

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

using namespace std;
using namespace nacos;

Properties configProps;
configProps[PropertyKeyConst::SERVER_ADDR] = "192.168.1.118:8848";
INacosServiceFactory *factory = NacosFactoryFactory::getNacosFactory(configProps);
ResourceGuard <INacosServiceFactory> _guardFactory(factory);
NamingService *namingSvc = factory->CreateNamingService();
ResourceGuard <NamingService> _serviceFactory(namingSvc);
Instance instance;
instance.clusterName = "DefaultCluster";
instance.ip = "192.168.1.131";
instance.port = 9000;
instance.instanceId = "1";
instance.ephemeral = true;

try
{
	 NacosString serviceName = "TestCppService" ;
	 namingSvc->registerInstance(serviceName, instance);
}
catch (NacosException &e)
{
	cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
	return ;
}

注销实例

 try
    {
       NacosString serviceName = "TestCppService" ;
       namingSvc->deregisterInstance(serviceName, "192.168.1.131", 9000);
    }
    catch (NacosException &e)
    {
       cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
       return ;
    }

可以在Nacos中心看到注册服务上线:

可以配合libhv 来做测试

下载libhv

git clone libhv: 🔥 比libevent、libuv更易用的国产网络库,用来开发 TCP/UDP/SSL/HTTP/WebSocket/MQTT 客户端/服务端

编译libhv

mkdir build 

cd build

cmake ..

cmake --build .

生成库文件

编译例子

g++ -std=c++11   -I ./libhv-master/include/hv/ -I  .   http_server.cpp -o http_server  -L  ./lib  -lhv

合并后的完整代码

#include "hv/HttpServer.h"

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

using namespace std;
using namespace nacos;


void HttpRouter(HttpService &router)
{
	// curl -v http://ip:port/
    router.Static("/", "./html");


    // curl -v http://ip:port/ping
    router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
        return resp->String("pong");
    });

    // curl -v http://ip:port/data
    router.GET("/data", [](HttpRequest* req, HttpResponse* resp) {
        static char data[] = "0123456789";
        return resp->Data(data, 10 /*, false */);
    });

    // curl -v http://ip:port/paths
    router.GET("/paths", [&router](HttpRequest* req, HttpResponse* resp) {
        return resp->Json(router.Paths());
    });

    // curl -v http://ip:port/get?env=1
    router.GET("/get", [](HttpRequest* req, HttpResponse* resp) {
        resp->json["origin"] = req->client_addr.ip;
        resp->json["url"] = req->url;
        resp->json["args"] = req->query_params;
        resp->json["headers"] = req->headers;
        return 200;
    });

    // curl -v http://ip:port/echo -d "hello,world!"
    router.POST("/echo", [](const HttpContextPtr& ctx) {
        return ctx->send(ctx->body(), ctx->type());
    });

    // curl -v http://ip:port/user/123
    router.GET("/user/{id}", [](const HttpContextPtr& ctx) {
        hv::Json resp;
        resp["id"] = ctx->param("id");
        return ctx->send(resp.dump(2));
    });
	
}


int main() 
{  
    Properties configProps;
    configProps[PropertyKeyConst::SERVER_ADDR] = "192.168.1.118:8848";
    INacosServiceFactory *factory = NacosFactoryFactory::getNacosFactory(configProps);
    ResourceGuard <INacosServiceFactory> _guardFactory(factory);
    NamingService *namingSvc = factory->CreateNamingService();
    ResourceGuard <NamingService> _serviceFactory(namingSvc);
    Instance instance;
    instance.clusterName = "DefaultCluster";
    instance.ip = "192.168.1.131";
    instance.port = 9000;
    instance.instanceId = "1";
    instance.ephemeral = true;


    try 
	{
		 NacosString serviceName = "TestCppService" ;
         namingSvc->registerInstance(serviceName, instance);
    }
    catch (NacosException &e) {
        cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
    }	
	
    
	HttpService router;
    HttpRouter(router);

    http_server_t server;
    server.port = 8080;
    server.service = &router;
    http_server_run(&server);
	
	
    try 
	{
		NacosString serviceName = "TestCppService" ;
        namingSvc->deregisterInstance(serviceName, "192.168.1.131", 9000);
    }
    catch (NacosException &e) {
        cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
        return -1;
    }
    sleep(5);

    return 0;
}

可以配置通过nacos 网关统一调用

通过网关调用

直接调用

相关推荐

  1. 2、学习 Nacos 注册中心

    2024-04-02 09:44:03       20 阅读

最近更新

  1. leetcode705-Design HashSet

    2024-04-02 09:44:03       5 阅读
  2. Unity发布webgl之后打开streamingAssets中的html文件

    2024-04-02 09:44:03       5 阅读
  3. vue3、vue2中nextTick源码解析

    2024-04-02 09:44:03       6 阅读
  4. 高级IO——React服务器简单实现

    2024-04-02 09:44:03       5 阅读
  5. 将图片数据转换为张量(Go并发处理)

    2024-04-02 09:44:03       4 阅读
  6. go第三方库go.uber.org介绍

    2024-04-02 09:44:03       6 阅读
  7. 前后端AES对称加密 前端TS 后端Go

    2024-04-02 09:44:03       7 阅读

热门阅读

  1. vue2 预览word文件

    2024-04-02 09:44:03       5 阅读
  2. C++宝强越狱1.0.6版本

    2024-04-02 09:44:03       5 阅读
  3. python 批量向MQTT服务器推送数据

    2024-04-02 09:44:03       5 阅读
  4. FastAPI Web框架教程 第13章 WebSocket

    2024-04-02 09:44:03       4 阅读
  5. 设计模式-单例模式(懒汉式)

    2024-04-02 09:44:03       5 阅读
  6. Anaconda conda常用命令

    2024-04-02 09:44:03       7 阅读
  7. ES6+对象常用方法

    2024-04-02 09:44:03       4 阅读
  8. 快速安装sudachipy日语包

    2024-04-02 09:44:03       6 阅读