forked from bigdatacesga/service-discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consulsd.py
130 lines (106 loc) · 4.11 KB
/
consulsd.py
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
"""Consul Service Discovery API
Examples:
import consul
service = consul.Client()
service.register(id='flexlm1', name='flexlm',
address='10.112.0.211', port=28518,
tags=('flexlm1', light', 'v1'),
check={'id': 'flexlm', 'name': 'flexlm on port 28518',
'tcp': '10.112.0.211:28518',
'Interval': '30s', 'timeout': '2s'})
service.deregister(id='flexlm1')
service.list()
service.info(name='flexlm')
The registration/deregistration is done using the consul agent API:
http://localhost:8500
PUT /v1/agent/service/register
PUT /v1/agent/service/deregister/<serviceId>
To query the information the catalog API is used:
http://localhost:8500
GET /v1/catalog/services
GET /v1/catalog/service/bigdata
GET /v1/catalog/nodes
GET /v1/catalog/node/c13-9
The payload for the registration request has the following format:
{
"ID": "flexlm1",
"Name": "flexlm",
"Tags": ["flexlm1", light", "v1"],
"Address": "10.112.0.211",
"Port": 28518,
"Check": {
"id": "flexlm",
"name": "flexlm on port 28518",
"tcp": "10.112.0.211:28518",
"Interval": "30s",
"timeout": "2s"
}
}
"""
import requests
class Client(object):
"""Service Discovery Client to perform basic operations
Examples:
import consul
service = consul.Client()
service.register(id='flexlm1', name='flexlm',
address='10.112.0.211', port=28518,
tags=('flexlm1', light', 'v1'))
service.deregister(id='flexlm1')
service.list()
service.info(name='flexlm')
The service can also be registered with an associated check:
service.register(id='flexlm1', name='flexlm',
address='10.112.0.211', port=28518,
tags=('flexlm1', light', 'v1'),
check={'id': 'flexlm', 'name': 'flexlm on port 28518',
'tcp': '10.112.0.211:28518',
'Interval': '30s', 'timeout': '2s'})
The registration/deregistration is done using the consul agent API:
PUT /v1/agent/service/register
PUT /v1/agent/service/deregister/<serviceId>
"""
def __init__(self, endpoint='http://localhost:8500'):
self.endpoint = endpoint
self.url_register = '{}/{}'.format(self.endpoint, 'v1/agent/service/register')
self.url_deregister = '{}/{}'.format(self.endpoint, 'v1/agent/service/deregister')
self.url_services = '{}/{}'.format(self.endpoint, '/v1/catalog/services')
self.url_service = '{}/{}'.format(self.endpoint, '/v1/catalog/service')
self.url_nodes = '{}/{}'.format(self.endpoint, '/v1/catalog/nodes')
self.url_node = '{}/{}'.format(self.endpoint, '/v1/catalog/node')
def register(self, id, name, address, port=None, tags=None, check=None):
"""Register a new service with the local consul agent"""
service = {}
service['ID'] = id
service['Name'] = name
service['Address'] = address
if port:
service['Port'] = int(port)
if tags:
service['Tags'] = tags
if check:
service['Check'] = check
r = requests.put(self.url_register, json=service)
if r.status_code != 200:
raise consulRegistrationError(
'PUT returned {}'.format(r.status_code))
return r
def deregister(self, id):
"""Deregister a service with the local consul agent"""
r = requests.put('{}/{}'.format(self.url_deregister, id))
if r.status_code != 200:
raise consulDeregistrationError(
'PUT returned {}'.format(r.status_code))
return r
def list(self):
"""List all services that have been registered"""
r = requests.get(self.url_services)
return r.json()
def info(self, name):
"""Info about a given service"""
r = requests.get('{}/{}'.format(self.url_service, name))
return r.json()
class consulRegistrationError(Exception):
pass
class consulDeregistrationError(Exception):
pass