forked from chinuno-usami/Consulpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
executable file
·117 lines (104 loc) · 3.02 KB
/
test.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
#include <iostream>
#include "consulpp.hpp"
void show_menu()
{
std::cout << "1.Register service" << std::endl;
std::cout << "2.delete service" << std::endl;
std::cout << "3.set value" << std::endl;
std::cout << "4.delete value" << std::endl;
std::cout << "5.get value" << std::endl;
std::cout << "6.health check" << std::endl;
std::cout << "0.exit" << std::endl;
}
void register_service(consulpp::Consulpp& ctx)
{
consulpp::ConsulService service;
service.SetAddress("127.0.0.1");
service.SetId("test_service_id");
service.SetMeta("version", "1.2.3");
service.SetMeta("test_meta", "test_meta_value");
service.SetName("client_test_service");
service.SetPort(22);
service.SetTag("client");
service.SetTag("test");
consulpp::ConsulCheck check;
check.SetId("client_test_check");
check.SetInterval("5s");
check.SetName("Client test check");
check.SetTcp("127.0.0.1:22");
check.SetTimeout("1s");
check.SetNote("test note");
service.SetCheck(check);
ctx.RegisterService(service);
}
void delete_service(consulpp::Consulpp& ctx)
{
ctx.Deregister("test_service_id");
}
void set_value(consulpp::Consulpp& ctx)
{
ctx.SetValue("client/client_test", "test");
}
void delete_value(consulpp::Consulpp& ctx)
{
ctx.DeleteValue("client/client_test");
}
void get_value(consulpp::Consulpp& ctx)
{
std::cout << ctx.GetValue("client/client_test") << std::endl;
}
void health_check(consulpp::Consulpp& ctx, const std::string& service_name, const std::string& tag_filter)
{
consulpp::ConsulServiceSet health_services;
if (ctx.HealthCheck(service_name, tag_filter, health_services))
{
std::cout << "print all services -------------" << std::endl;
for (auto iter : health_services)
{
std::cout << "id = " << iter.GetId() << std::endl;
std::cout << "name = " << iter.GetName() << std::endl;
std::cout << "ip = " << iter.GetAddress() << std::endl;
std::cout << "port = " << iter.GetPort() << std::endl;
}
std::cout << "--------------------------------" << std::endl;
}
else
{
std::cout << "health check failed, please test it" << std::endl;
}
}
int main()
{
consulpp::Consulpp ctx("127.0.0.1", 8500);
while (true)
{
show_menu();
switch (getchar())
{
case '1':
register_service(ctx);
break;
case '2':
delete_service(ctx);
break;
case '3':
set_value(ctx);
break;
case '4':
delete_value(ctx);
break;
case '5':
get_value(ctx);
break;
case '6':
health_check(ctx, "client_test_service", "test");
break;
case '0':
exit(0);
break;
default:
break;
}
}
return 0;
}