Skip to content

Commit

Permalink
Merge pull request #45 from nacos-group/develop
Browse files Browse the repository at this point in the history
Develop to master, version 1.0.2
  • Loading branch information
TTTTTAAAAAKKKKEEEENNNN authored Dec 8, 2020
2 parents 5844974 + 86da87b commit 5c0b75b
Show file tree
Hide file tree
Showing 74 changed files with 1,698 additions and 789 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ a libnacos-cli.so and a nacos-cli.out will be generated

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

**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:
**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 /endpoints/endpoint0
**on path /endpoints/endpoint0**

## Integrate the library into your project

Expand Down Expand Up @@ -403,6 +403,22 @@ int main() {
}
```

### 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:

```C++
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();
......
```

# About Nacos

Nacos (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.
Expand Down
5 changes: 2 additions & 3 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Nacos-sdk-cpp是nacos客戶端的C++版本,它支持服务发现和动态配

运行 `./nacos-cli.out` 以执行客户端的所有testcase

**注意: 你需要在本机运行一个nacos server,监听8848端口以完成所有测试
其中有个测试将会测试端点(endpoint)功能,所以你需要在本机运行一个http服务器,在路径/endpoints/endpoint0提供下述内容:
**注意:** 你需要在本机运行一个nacos server,监听8848端口以完成所有测试
其中有个测试将会测试端点(endpoint)功能,**所以你还需要**在本机运行一个http服务器,在路径/endpoints/endpoint0提供下述内容:

`127.0.0.1:8848`

Expand Down Expand Up @@ -398,7 +398,6 @@ int main() {
}
```


### 启用认证

如果你的服务器启设置了密码,在上述任意一个例子当中加入下述配置,即可启用用户名密码认证:
Expand Down
6 changes: 6 additions & 0 deletions include/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class Constants {

const static NacosString DEFAULT_GROUP;

const static NacosString DEFAULT_CONTEXT_PATH;

const static NacosString PROTOCOL_VERSION;

const static NacosString GET_SERVERS_PATH;

const static NacosString APPNAME;

const static NacosString UNKNOWN_APP;
Expand Down
12 changes: 7 additions & 5 deletions include/DebugAssertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ do \

#define SHOULD_BE_FALSE(assertion, message) SHOULD_BE_TRUE(!(assertion), (message))

#define ReleaseResource(x) \
#ifdef NACOS_AUTH
#define ADD_AUTH_INFO(x) \
do { \
if ((x) != NULL) \
delete (x); \
x = NULL; \
(x)["nacos.auth.username"] = "nacos"; \
(x)["nacos.auth.password"] = "nacos"; \
} while (0)
#else
#define ADD_AUTH_INFO(x)
#endif
}//namespace nacos

#endif
18 changes: 7 additions & 11 deletions include/NacosExceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace nacos{
class NacosException : public std::exception {
private:
protected:
int _errcode;
NacosString _errmsg;
public:
Expand Down Expand Up @@ -75,6 +75,9 @@ class NacosException : public std::exception {
static const int INVALID_FACTORY_CONFIG = 1003;
static const int ALL_SERVERS_TRIED_AND_FAILED = 1004;
static const int NO_SERVER_AVAILABLE = 1005;
static const int INVALID_LOGIN_CREDENTIAL = 1006;
static const int UNABLE_TO_OPEN_FILE = 1007;

};

class NetworkException : public std::exception {
Expand All @@ -93,20 +96,13 @@ class NetworkException : public std::exception {
const int errorcode() const throw() { return _curlerrcode; };
};

class IOException : public std::exception {
private:
int _errcode;
NacosString _errmsg;
class IOException : public NacosException {
public:
IOException(int errorcode, const char *errormsg) throw(): _errcode(errorcode), _errmsg(errormsg) {};
IOException(int errorcode, const char *errormsg) throw() : NacosException(errorcode, errormsg) {};

IOException(int errorcode, const NacosString &errormsg) throw(): _errcode(errorcode), _errmsg(errormsg) {};
IOException(int errorcode, const NacosString &errormsg) throw() : NacosException(errorcode, errormsg) {};

~IOException() throw() {};

const char *what() const throw() { return _errmsg.c_str(); };

const int errorcode() const throw() { return _errcode; };
};

class MalformedConfigException : public NacosException {
Expand Down
4 changes: 4 additions & 0 deletions include/NacosString.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ NacosString NacosStringOps::valueOf(T val) {

return NULLSTR;
}

template<>
NacosString NacosStringOps::valueOf<bool>(bool val);

}//namespace nacos

#endif
6 changes: 0 additions & 6 deletions include/Parameters.h

This file was deleted.

12 changes: 7 additions & 5 deletions include/PropertyKeyConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,26 @@ class PropertyKeyConst {

static const NacosString NAMING_POLLING_THREAD_COUNT;

static const NacosString HTTP_REQ_TIMEOUT;

static const NacosString SRVLISTMGR_REFRESH_INTERVAL;

static const NacosString SRVLISTMGR_READ_TIMEOUT;
static const NacosString SERVER_REQ_TIMEOUT;

static const NacosString TCP_NAMING_POLL_INTERVAL;

static const NacosString CONFIG_LONGPULLLING_TIMEOUT;

static const NacosString CONFIG_GET_TIMEOUT;

static const NacosString HB_FAIL_WAIT_TIME;

static const NacosString NACOS_SNAPSHOT_PATH;

static const NacosString NACOS_LOG_PATH;

static const NacosString CLIENT_NAME;
static const NacosString AUTH_USERNAME;
static const NacosString AUTH_PASSWORD;

static const int NACOS_DEFAULT_PORT = 8848;

/*public static class SystemEnv {
static const NacosString ALIBABA_ALIWARE_ENDPOINT_PORT = "ALIBABA_ALIWARE_ENDPOINT_PORT";
Expand Down
19 changes: 18 additions & 1 deletion include/utils/ParamUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace nacos{
class ParamUtils {
public:
template<typename T>
static T getNthElem(const std::list <T> &parm, size_t i) {
static const T &getNthElem(const std::list <T> &parm, size_t i) {
assert(parm.size() > i);
typename std::list<T>::const_iterator it = parm.begin();
for (size_t skipper = 0; skipper < i; skipper++) {
Expand Down Expand Up @@ -192,6 +192,23 @@ class ParamUtils {
list.push_back(key);
list.push_back(value);
}

static NacosString toLower(const NacosString &str) {
NacosString lowerCaseString;
for (NacosString::const_iterator it = str.begin(); it != str.end(); it++) {
lowerCaseString.push_back(tolower(*it));
}

return lowerCaseString;
}

static bool equals_ic(const NacosString &str1, const NacosString &str2) {

NacosString lcase_str1 = toLower(str1);
NacosString lcase_str2 = toLower(str2);

return lcase_str1 == lcase_str2;
}
};
}//namespace nacos

Expand Down
3 changes: 3 additions & 0 deletions src/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const NacosString Constants::CLIENT_VERSION = "3.0.0";
const int Constants::DATA_IN_BODY_VERSION = 204;

const NacosString Constants::DEFAULT_GROUP = "DEFAULT_GROUP";
const NacosString Constants::DEFAULT_CONTEXT_PATH = "nacos";
const NacosString Constants::PROTOCOL_VERSION = "v1";
const NacosString Constants::GET_SERVERS_PATH = "ns/operator/servers";

const NacosString Constants::APPNAME = "AppName";

Expand Down
9 changes: 4 additions & 5 deletions src/PropertyKeyConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ const NacosString PropertyKeyConst::NAMING_CLIENT_BEAT_THREAD_COUNT = "namingCli

const NacosString PropertyKeyConst::NAMING_POLLING_THREAD_COUNT = "namingPollingThreadCount";

const NacosString PropertyKeyConst::HTTP_REQ_TIMEOUT = "httpReqTimeout";

const NacosString PropertyKeyConst::SRVLISTMGR_REFRESH_INTERVAL = "serverListMgr.refreshInterval";

const NacosString PropertyKeyConst::SRVLISTMGR_READ_TIMEOUT = "serverListMgr.readTimeout";
const NacosString PropertyKeyConst::SERVER_REQ_TIMEOUT = "nacos.server.reqtimeout";

const NacosString PropertyKeyConst::TCP_NAMING_POLL_INTERVAL = "naming.poller.interval";

const NacosString PropertyKeyConst::CONFIG_LONGPULLLING_TIMEOUT = "config.longpulling.timeout";

const NacosString PropertyKeyConst::CONFIG_GET_TIMEOUT = "config.get.timeout";

const NacosString PropertyKeyConst::HB_FAIL_WAIT_TIME = "naming.heartbeat.failwait";
const NacosString PropertyKeyConst::NACOS_SNAPSHOT_PATH = "nacos.snapshot.path";
const NacosString PropertyKeyConst::NACOS_LOG_PATH = "nacos.log.path";
const NacosString PropertyKeyConst::CLIENT_NAME = "nacos.client.name";
const NacosString PropertyKeyConst::AUTH_USERNAME = "nacos.auth.username";
const NacosString PropertyKeyConst::AUTH_PASSWORD = "nacos.auth.password";
}//namespace nacos
44 changes: 28 additions & 16 deletions src/config/AppConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
#include "PropertyKeyConst.h"
#include "IOUtils.h"
#include "NacosExceptions.h"
#include "Parameters.h"
#include "utils/DirUtils.h"
#include <vector>
#include <stdlib.h>

using namespace std;

namespace nacos{
NacosString AppConfigManager::LINE_SEPARATOR = "\n";
NacosString AppConfigManager::KV_SEPARATOR = "=";


bool AppConfigManager::nacosAuthEnabled() {
if (contains(PropertyKeyConst::AUTH_USERNAME) && contains(PropertyKeyConst::AUTH_PASSWORD)) {
return true;
} else {
return false;
}
}

Properties AppConfigManager::parseConfigFile(const NacosString &file) {
Properties parsedConfig;
NacosString confContent = IOUtils::readStringFromFile(file, NULLSTR);//TODO: add encoding support
Expand Down Expand Up @@ -71,21 +80,24 @@ void AppConfigManager::clearConfig() {
appConfig.clear();
}

NacosString AppConfigManager::get(const NacosString &key) const {
const NacosString &AppConfigManager::get(const NacosString &key) {
if (appConfig.count(key) == 0) {
return NULLSTR;
}

Properties copyProps = appConfig;
if (key.compare(PropertyKeyConst::NAMESPACE) == 0
&& copyProps[PropertyKeyConst::NAMESPACE].compare("Public") == 0)
&& appConfig[PropertyKeyConst::NAMESPACE].compare("Public") == 0)
{
return NULLSTR;
}
return copyProps[key];
return appConfig[key];
}

void AppConfigManager::set(const NacosString &key, const NacosString &value) {
//Special case handle
if (key.compare(PropertyKeyConst::SERVER_REQ_TIMEOUT) == 0) {
_serverReqTimeout = atoi(value.c_str());
}
appConfig[key] = value;
}

Expand All @@ -107,19 +119,18 @@ AppConfigManager::AppConfigManager(const NacosString &_configFile) {
void AppConfigManager::initDefaults() {
appConfig.clear();
//appConfig[PropertyKeyConst::NAMESPACE] = "public";
appConfig[PropertyKeyConst::HTTP_REQ_TIMEOUT] = "50000";
appConfig[PropertyKeyConst::SRVLISTMGR_REFRESH_INTERVAL] = "30000";
appConfig[PropertyKeyConst::SRVLISTMGR_READ_TIMEOUT] = "3000";
appConfig[PropertyKeyConst::CONTEXT_PATH] = DEFAULT_CONTEXT_PATH;
appConfig[PropertyKeyConst::TCP_NAMING_POLL_INTERVAL] = "30000";//30 secs by default
appConfig[PropertyKeyConst::CONFIG_LONGPULLLING_TIMEOUT] = "30000";//ms
appConfig[PropertyKeyConst::CONFIG_GET_TIMEOUT] = "3000";//ms
appConfig[PropertyKeyConst::HB_FAIL_WAIT_TIME] = "20000";//ms
set(PropertyKeyConst::SRVLISTMGR_REFRESH_INTERVAL, "30000");
set(PropertyKeyConst::SERVER_REQ_TIMEOUT, "3000");
set(PropertyKeyConst::CONTEXT_PATH, Constants::DEFAULT_CONTEXT_PATH);
set(PropertyKeyConst::TCP_NAMING_POLL_INTERVAL, "30000");//30 secs by default
set(PropertyKeyConst::CONFIG_LONGPULLLING_TIMEOUT, "30000");//ms
set(PropertyKeyConst::HB_FAIL_WAIT_TIME, "20000");//ms
set(PropertyKeyConst::CLIENT_NAME, "default");

NacosString homedir = DirUtils::getHome();

appConfig[PropertyKeyConst::NACOS_LOG_PATH] = homedir + Constants::FILE_SEPARATOR + "nacos" + Constants::FILE_SEPARATOR + "log";
appConfig[PropertyKeyConst::NACOS_SNAPSHOT_PATH] = homedir + Constants::FILE_SEPARATOR + "nacos" + Constants::FILE_SEPARATOR + "snapshot";
set(PropertyKeyConst::NACOS_LOG_PATH, homedir + Constants::FILE_SEPARATOR + "nacos" + Constants::FILE_SEPARATOR + "log");
set(PropertyKeyConst::NACOS_SNAPSHOT_PATH, homedir + Constants::FILE_SEPARATOR + "nacos" + Constants::FILE_SEPARATOR + "snapshot");
log_info("DEFAULT_LOG_PATH:%s\n", appConfig[PropertyKeyConst::NACOS_LOG_PATH].c_str());
log_info("DEFAULT_SNAPSHOT_PATH:%s\n", appConfig[PropertyKeyConst::NACOS_SNAPSHOT_PATH].c_str());
}
Expand All @@ -128,7 +139,8 @@ void AppConfigManager::initDefaults() {
void AppConfigManager::applyConfig(Properties &rhs) {
for (map<NacosString, NacosString>::iterator it = rhs.begin();
it != rhs.end(); it++) {
appConfig[it->first] = it->second;
set(it->first, it->second);
}
}

}//namespace nacos
7 changes: 6 additions & 1 deletion src/config/AppConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppConfigManager {

static NacosString LINE_SEPARATOR;
static NacosString KV_SEPARATOR;
volatile long _serverReqTimeout;

AppConfigManager();

Expand All @@ -26,6 +27,10 @@ class AppConfigManager {
void applyConfig(Properties &rhs);

public:
bool nacosAuthEnabled();

long getServeReqTimeout() const { return _serverReqTimeout; };

bool isReloadable() const { return reloadable; };

AppConfigManager(Properties &props);
Expand All @@ -38,7 +43,7 @@ class AppConfigManager {

void clearConfig();

NacosString get(const NacosString &key) const;
const NacosString &get(const NacosString &key);

bool contains(const NacosString &key) const;

Expand Down
Loading

0 comments on commit 5c0b75b

Please sign in to comment.