下载源码:
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 网关统一调用
通过网关调用
直接调用