-
Notifications
You must be signed in to change notification settings - Fork 3
/
Consulpp.cpp
executable file
·221 lines (195 loc) · 6.11 KB
/
Consulpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "Consulpp.h"
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <functional>
#include <cpprest/http_client.h>
#include "Check.h"
using namespace web;
using namespace web::http;
using namespace web::http::client;
static const std::string REGISTER_API("/v1/agent/service/register");
static const std::string DEREGISTER_API("/v1/agent/service/deregister/");
static const std::string KV_API("/v1/kv/");
class CConsulpp::CImpl{
public:
CImpl() = delete;
CImpl( std::string strHost, unsigned int uiPort );
std::string m_strHost;
unsigned int m_uiPort;
std::string m_strUriBase;
};
CConsulpp::CImpl::CImpl(std::string strHost, unsigned int uiPort)
{
m_strHost = strHost;
m_uiPort = uiPort;
m_strUriBase = "http://" + strHost + ":" + std::to_string(uiPort);
}
static std::function<bool(http_response)> NoReturnProcessor()
{
return [=](http_response resp) {
std::cout << "status:" << resp.status_code()<<std::endl;
if (resp.status_code() == status_codes::OK)
{
return true;
}
std::string strError("Register resopnsed body:");
strError += resp.extract_string(true).get();
std::cout << strError << std::endl;
return false;
//throw std::exception("register request failed");
// return pplx::task_from_result(json::value());
};
}
CConsulpp::CConsulpp( std::string strHost, unsigned int uiPort ):m_pData(new CImpl(strHost, uiPort))
{
}
CConsulpp::~CConsulpp()
{
}
// 向consul注册服务
bool CConsulpp::RegisterService( const CConsulService &service )
{
json::value jsonReqData;
jsonReqData["ID"] = json::value::string(service.GetId());
jsonReqData["Name"] = json::value::string(service.GetName());
jsonReqData["Port"] = json::value::number(service.GetPort());
// 添加Metas
json::value jsonMetas;
const auto& mapMetas = service.GetMeta();
std::for_each(std::begin(mapMetas), std::end(mapMetas),
[&jsonMetas](std::pair<std::string, std::string> item) {
jsonMetas[item.first] = json::value::string(item.second);
});
jsonReqData["Meta"] = jsonMetas;
// 添加tags
json::value jsonTags;
const auto& setTags = service.GetTags();
size_t nTagsCount = 0;
std::for_each(std::begin(setTags), std::end(setTags),
[&jsonTags, &nTagsCount](std::string strTag) {
jsonTags[nTagsCount++] = json::value::string(strTag);
});
// 添加checks
json::value jsonChecks;
// 构造单个check的json对象
auto ParseCheck = [](const CCheck& check) {
json::value jsonCheck;
jsonCheck["ID"] = json::value::string(check.GetId());
jsonCheck["Name"] = json::value::string(check.GetName());
jsonCheck["TCP"] = json::value::string(check.GetTcp());
jsonCheck["Interval"] = json::value::string(check.GetInterval());
jsonCheck["Timeout"] = json::value::string(check.GetTimeout());
jsonCheck["Notes"] = json::value::string(check.GetTimeout());
return jsonCheck;
};
const auto& setChecks = service.GetChecks();
size_t nChecksCount = 0;
std::for_each(std::begin(setChecks), std::end(setChecks),
[&jsonChecks, &nChecksCount, &ParseCheck](CCheck check) {
jsonChecks[nChecksCount++] = ParseCheck(check);
});
jsonReqData["Tags"] = jsonTags;
jsonReqData["Checks"] = jsonChecks;
// 请求consul api
http_client client(U(m_pData->m_strUriBase));
uri_builder builder(U(REGISTER_API));
auto resp = client.request(methods::PUT, builder.to_string(), jsonReqData).
then(NoReturnProcessor());
try
{
return resp.get();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
}
// 向consul注销服务
bool CConsulpp::Deregister( std::string strId )
{
// 请求consul api
http_client client(U(m_pData->m_strUriBase));
uri_builder builder(U(DEREGISTER_API));
builder.append_path(strId);
auto resp = client.request(methods::PUT, builder.to_string()).
then(NoReturnProcessor());
try
{
return resp.get();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
}
std::string CConsulpp::GetValue(std::string strKey)
{
// 请求consul api
http_client client(U(m_pData->m_strUriBase));
uri_builder builder(U(KV_API));
builder.append_path(strKey);
// 直接返回原始值,不编码
builder.append_query(U("raw"));
auto resp = client.request(methods::GET, builder.to_string()).
then([](http_response resp) {
std::cout << "status:" << resp.status_code() << std::endl;
if (resp.status_code() == status_codes::OK)
{
return resp.extract_string(true);
}
std::string strError("Register resopnsed body:");
strError += resp.extract_string(true).get();
std::cout << strError << std::endl;
return pplx::task_from_result(std::string());
});
try
{
return resp.get();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return std::string();
}
}
bool CConsulpp::SetValue(std::string strKey, std::string strValue)
{
// 请求consul api
http_client client(U(m_pData->m_strUriBase));
uri_builder builder(U(KV_API));
builder.append_path(strKey);
auto resp = client.request(methods::PUT, builder.to_string(), strValue).
then(NoReturnProcessor());
try
{
return resp.get();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
}
bool CConsulpp::DeleteValue(std::string strKey)
{
// 请求consul api
http_client client(U(m_pData->m_strUriBase));
uri_builder builder(U(KV_API));
builder.append_path(strKey);
auto resp = client.request(methods::DEL, builder.to_string()).
then(NoReturnProcessor());
try
{
return resp.get();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
return false;
}
}