Debian Codegraph @

third-party/libhv-v1.3.3/examples/consul/consul.cpp

raw
1 #include "consul.h"
2
3 #include "HttpClient.h"
4 using namespace hv;
5
6 #include "json.hpp"
7 using json = nlohmann::json;
8
9 #define PROTOCOL "http://"
10 #define API_VERSION "v1"
11
12 static const char url_register[] = "/agent/service/register";
13 static const char url_deregister[] = "/agent/service/deregister";
14 static const char url_discover[] = "/catalog/service";
15
16 static std::string make_url(const char* ip, int port, const char* url) {
17 return asprintf(PROTOCOL "%s:%d/" API_VERSION "%s", ip, port, url);
18 }
19
20 static std::string make_ServiceID(consul_service_t* service) {
21 return asprintf("%s-%s:%d", service->name, service->ip, service->port);
22 }
23
24 /*
25 {
26 "ID": "redis1",
27 "Name": "redis",
28 "Tags": [
29 "primary",
30 "v1"
31 ],
32 "Address": "127.0.0.1",
33 "Port": 8000,
34 "Meta": {
35 "redis_version": "4.0"
36 },
37 "EnableTagOverride": false,
38 "Check": {
39 "DeregisterCriticalServiceAfter": "90m",
40 "Args": ["/usr/local/bin/check_redis.py"],
41 "HTTP": "http://localhost:5000/health",
42 "Interval": "10s",
43 "TTL": "15s"
44 },
45 "Weights": {
46 "Passing": 10,
47 "Warning": 1
48 }
49 }
50 */
51 int register_service(consul_node_t* node, consul_service_t* service, consul_health_t* health) {
52 HttpRequest req;
53 req.method = HTTP_PUT;
54 req.url = make_url(node->ip, node->port, url_register);
55 req.content_type = APPLICATION_JSON;
56
57 json jservice;
58 jservice["Name"] = service->name;
59 if (*service->ip) {
60 jservice["Address"] = service->ip;
61 }
62 jservice["Port"] = service->port;
63 jservice["ID"] = make_ServiceID(service);
64
65 json jcheck;
66 if (*health->url == '\0') {
67 snprintf(health->url, sizeof(health->url), "%s:%d", service->ip, service->port);
68 }
69 jcheck[health->protocol] = health->url;
70 jcheck["Interval"] = asprintf("%dms", health->interval);
71 jcheck["DeregisterCriticalServiceAfter"] = asprintf("%dms", health->interval * 3);
72 jservice["Check"] = jcheck;
73
74 req.body = jservice.dump();
75 printd("PUT %s\n", req.url.c_str());
76 printd("%s\n", req.body.c_str());
77
78 HttpResponse res;
79 int ret = http_client_send(&req, &res);
80 printd("%s\n", res.body.c_str());
81 return ret;
82 }
83
84 int deregister_service(consul_node_t* node, consul_service_t* service) {
85 std::string url = make_url(node->ip, node->port, url_deregister);
86 url += '/';
87 url += make_ServiceID(service);
88
89 HttpRequest req;
90 req.method = HTTP_PUT;
91 req.url = url;
92 req.content_type = APPLICATION_JSON;
93 printd("PUT %s\n", req.url.c_str());
94
95 HttpResponse res;
96 int ret = http_client_send(&req, &res);
97 printd("%s\n", res.body.c_str());
98 return ret;
99 }
100
101 int discover_services(consul_node_t* node, const char* service_name, std::vector<consul_service_t>& services) {
102 std::string url = make_url(node->ip, node->port, url_discover);
103 url += '/';
104 url += service_name;
105
106 HttpRequest req;
107 req.method = HTTP_GET;
108 req.url = url;
109
110 HttpResponse res;
111 printd("GET %s\n", req.url.c_str());
112 int ret = http_client_send(&req, &res);
113 if (ret != 0) return ret;
114 printd("%s\n", res.body.c_str());
115
116 json jroot = json::parse(res.body);
117 if (!jroot.is_array()) return -1;
118 if (jroot.size() == 0) return 0;
119
120 consul_service_t service;
121 std::string name, ip;
122 services.clear();
123 for (size_t i = 0; i < jroot.size(); ++i) {
124 auto jservice = jroot[i];
125 name = jservice["ServiceName"];
126 if (jservice.contains("Address")) {
127 ip = jservice["Address"];
128 } else if (jservice.contains("ServiceAddress")) {
129 ip = jservice["ServiceAddress"];
130 } else if (jservice.contains("ServiceAddress6")) {
131 ip = jservice["ServiceAddress6"];
132 } else {
133 continue;
134 }
135 int port = jservice["ServicePort"];
136
137 strncpy(service.name, name.c_str(), sizeof(service.name));
138 strncpy(service.ip, ip.c_str(), sizeof(service.ip));
139 service.port = port;
140 services.emplace_back(service);
141 }
142
143 return 0;
144 }