Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nacos2.1.1版本配置监控无效的问题 #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/listen/ClientWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ NacosString ClientWorker::checkListenedKeys() NACOS_THROW(NetworkException,Nacos
paramValues.push_back(ConfigConstant::PROBE_MODIFY_REQUEST);
paramValues.push_back(postData);
log_debug("[ClientWorker]-checkListenedKeys:Assembled postData:%s\n", postData.c_str());
paramValues.push_back("tenant");
paramValues.push_back(_objectConfigData->_serverListManager->getNamespace());

//Get the request url
//TODO:move /listener to constant
Expand Down
107 changes: 107 additions & 0 deletions testfawsw/IntegratingIntoYourProject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "factory/NacosServiceFactory.h"

#include <iostream>
#include <unistd.h>
#include "Nacos.h"
#include "log/Logger.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() {
Logger::setLogLevel(DEBUG);
Properties configProps;
// configProps[PropertyKeyConst::SERVER_ADDR] = "";
// configProps[PropertyKeyConst::AUTH_USERNAME] = "test";
// configProps[PropertyKeyConst::AUTH_PASSWORD] = "test";
// configProps[PropertyKeyConst::NAMESPACE] = "dev";
// configProps[PropertyKeyConst::SERVER_ADDR] = "";
// configProps[PropertyKeyConst::AUTH_USERNAME] = "hqzl3";
// configProps[PropertyKeyConst::AUTH_PASSWORD] = "sitHqzl3nacos2022";
// configProps[PropertyKeyConst::NAMESPACE] = "fc2-faw-szhb-hqzl3";
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;

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("tts-script.yaml", "nlu", 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("tts-script.yaml", "nlu", theListener);//Cancel listening
getchar();

return 0;

// NacosString ss = "";
// try {
// ss = n->getConfig("tts-script.yaml", "nlu", 10000);
// }
// catch (NacosException &e) {
// cout <<
// "Request failed with curl code:" << e.errorcode() << endl <<
// "Reason:" << e.what() << endl;
// return -1;
// }
// cout << ss << endl;

// return 0;
}
20 changes: 20 additions & 0 deletions testfawsw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```
cd nacos-sdk-cpp
cmake .
make
```

```
NacosServiceFactory.h
Mutex.h
Tid.h
TimeUtils.h
以上4个头文件需要从src源码中拷贝到`usr/local/include/nacos/`目录,
拷贝位置要和src保持一致,
同时修改以上头文件的的`include`使用相对路径
```

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