-
Notifications
You must be signed in to change notification settings - Fork 2
/
consul_functions.py
46 lines (30 loc) · 1.1 KB
/
consul_functions.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
from consul import Consul, Check
import socket
CONSUL_PORT = 8500
SERVICE_NAME = 'locations'
SERVICE_PORT = 5000
def get_host_name_IP():
host_name_ip = ""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
host_name_ip = s.getsockname()[0]
s.close()
# print ("Host ip:", host_name_ip)
return host_name_ip
except:
print("Unable to get Hostname")
def register_to_consul():
consul = Consul(host="consul", port=CONSUL_PORT)
agent = consul.agent
service = agent.service
ip = get_host_name_IP()
check = Check.http(f"http://{ip}:{SERVICE_PORT}/api/{SERVICE_NAME}/ui", interval="10s", timeout="5s",
deregister="1s")
service.register(name=SERVICE_NAME, service_id=SERVICE_NAME, address=ip, port=SERVICE_PORT, check=check)
def get_consul_service(service_id):
consul = Consul(host="consul", port=CONSUL_PORT)
agent = consul.agent
service_list = agent.services()
service_info = service_list[service_id]
return service_info['Address'], service_info['Port']