Created
March 2, 2019 21:56
-
-
Save yildirima/e8975bc6d26dacabb6d7e8cd09befb7f to your computer and use it in GitHub Desktop.
Consul-Register-Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import consul | |
def consul_conn_check(host): | |
c = consul.Consul(host=host) | |
state = False | |
try: | |
c.catalog.nodes() | |
state = True | |
except ConnectionError: | |
pass | |
finally: | |
return state | |
#print(consul_conn_check("127.0.0.1")) | |
def register_consul(host, port, service_id=str): | |
service_address = "127.0.0.1" | |
conn = consul_conn_check(host) | |
if conn: | |
c = consul.Consul(host=host) | |
result = c.agent.service.register( | |
'My_Apache_Service_80', | |
service_id=service_id, | |
address=service_address, | |
port=port, | |
check={ | |
"DeregisterCriticalServiceAfter": "3m", | |
"http": "http://{}:{}/".format(service_address, port), | |
"interval": "5s", | |
"timeout": "1s" | |
} | |
) | |
return result | |
else: | |
return False | |
print(register_consul('127.0.0.1', 80, "ApacheID_80")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment