Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Consul-Register-Service
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
You can’t perform that action at this time.