def _create_http_check(options): http_check = Check.http( f'http://{options.consul_check_host}:{options.port}/status', f'{options.consul_http_check_interval_sec}s', timeout=f'{options.consul_http_check_timeout_sec}s' ) return http_check
def register(self, metrics_server, loop): """ :return: A coroutine callable to deregister or ``None``. """ consul = _Consul(token=self.token, loop=loop) if not (yield from consul.agent.service.register( name=self.name, service_id=self.service_id, address=metrics_server.socket.addr, port=metrics_server.socket.port, tags=self.tags, check=Check.http( metrics_server.url, "10s", ))): # pragma: nocover return None @asyncio.coroutine def deregister(): try: if self.deregister is True: yield from consul.agent.service.deregister(self.service_id) finally: consul.close() return deregister
def register_plugin(): """ Registers this plugin with Consul :return: None """ logger.info("Registering plugin with Consul") consul_host = getenv("CONSUL_HOST", "localhost") consul_port = getenv("CONSUL_PORT", 8500) c = Consul(host=consul_host, port=consul_port) hname = socket.gethostname() ipaddr = socket.gethostbyname(hname) health_check = Check.http(url=f'http://{ipaddr}:{port}/config', interval="20s", deregister=True) service_name = config['serviceName'] service_id = f'{service_name}-{str(uuid.uuid4())}' c.agent.service.register(name=service_name, service_id=service_id, address=ipaddr, port=port, tags=['secure=false'], check=health_check) atexit.register(deregister, c, service_id)
def initialize_app(self, app) -> Optional[Future]: if not options.consul_enabled: integrations_logger.info('Consul disabled, skipping') return None host = socket.gethostname() self.consul = Consul(host=options.consul_host, port=options.consul_port) self.service_name = options.app self.service_id = f'{self.service_name}-{options.datacenter}-{host}-{options.port}' http_check = Check.http(f'http://{host}:{options.port}/status', options.consul_http_check_interval_sec, timeout=options.consul_http_check_timeout_sec) # not supported by version 1.1.0 meta = {'serviceVersion': version} return asyncio.ensure_future( self.consul.agent.service.register( self.service_name, service_id=self.service_id, address=host, port=options.port, check=http_check, tags=options.consul_tags, ))
def register(self, metrics_server, loop): """ :return: A coroutine callable to deregister or ``None``. """ consul = _Consul(token=self.token, loop=loop) if not (yield from consul.agent.service.register( name=self.name, service_id=self.service_id, address=metrics_server.socket.addr, port=metrics_server.socket.port, tags=self.tags, check=Check.http( metrics_server.url, "10s", ) )): # pragma: nocover return None @asyncio.coroutine def deregister(): try: if self.deregister is True: yield from consul.agent.service.deregister(self.service_id) finally: consul.close() return deregister
def run(self): all_svc = list(c.agent.services()) st.write(all_svc) if self.register_btn: for v in self.services.split('\n'): arr = v.split('\t') if len(arr) != 2: st.warning("Line not valid: {}".format(v)) name, url = arr if not name or not url: st.warning("Line not valid: {}".format(v)) if name in all_svc and not self.force: st.warning("Ignore: {}".format(name)) continue check = Check.http(url, interval=f'{self.num_interval}s', timeout=f'{self.num_timeout}s') c.agent.service.register(name=name, service_id=name, address=url, check=check) st.success("Registered: {}".format(name))
def main(): c = consul.Consul() check_http = Check.http('http://127.0.0.1:5000/healthcheck', interval='5s', timeout='10s', deregister=True) # registration of checkout service c.agent.service.register('checkout', address=os.getenv("LISTEN_ADDR", "127.0.0.1"), port=5000, check=check_http) app.run(debug=True, host='0.0.0.0', port=5000)
def run(self): if self.register_btn: check = Check.http(self.txt_url, interval=f'{self.num_interval}s', timeout=f'{self.num_timeout}s') c.agent.service.register(name=self.txt_name, service_id=self.txt_name, address=self.txt_url, check=check)
def register(self, host='localhost', port=3000, tags=None): if host is None: host = 'localhost' self.consul.agent.service.register( name=self.service_name, address=host, port=port, tags=tags, check=Check.http(url=f'http://{host}:{port}/check', interval=10))
def register(self, url, timeout: int = TIMEOUT) -> None: self.consul.agent.service.register(self.service_name, tags=self.tags, service_id=self.service_id, address=self.address, port=8156, # timeout=self.timeout, check=Check.http(url=url, interval=self.http_interval))
def register_to_consul(): consul = Consul(host="consul", port=consul_port) agent = consul.agent service = agent.service check = Check.http(f"http://{service_name}:{service_port}/api/ui", interval="10s", timeout="5s", deregister="1s") service.register(service_name, service_id=service_name, port=service_port, check=check)
def register_service(self, app_id: str, host: str, port: int): if not app_id: raise ValueError if not host: raise ValueError if not port: raise ValueError self.consul.agent.service.register(name='pe', service_id=app_id, address=host, port=port, tags=['pe', 'python', app_id], check=Check.http( url='http://' + host + ':' + str(port), interval=self._DEFAULT_CONSUL_CONFIG['HEALTHCHECK_INTERVAL'], header={"Accept": ["application/json"]} ))
def register_to_consul(): consul = Consul(host='consul', port=CONSUL_PORT) agent = consul.agent service = agent.service check = Check.http(f"http://{SERVICE_NAME}:{SERVICE_PORT}/api/social/ui", interval="10s", timeout="5s", deregister="1s") service.register(SERVICE_NAME, service_id=SERVICE_NAME, port=SERVICE_PORT, check=check)
def register_to_consul(): consul = Consul(host="consul", port=consul_port) agent = consul.agent service = agent.service ip = get_ip() check = Check.http(f"http://{ip}:{service_port}/", interval="10s", timeout="5s", deregister="1s") service.register(service_name, service_id=service_name, address=ip, port=service_port, check=check)
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 main(): # consul client create c = consul.Consul() # health check check_http = Check.http('http://127.0.0.1:5001/healthcheck', interval='5s', timeout='10s', deregister=True) # registration of cart service c.agent.service.register('cart', address=os.getenv("LISTEN_ADDR", "127.0.0.1"), port=5001, check=check_http) # discovery services = c.catalog.service('checkout')[1][0] # url for checkout micro-service config[ 'CHECKOUT_URL'] = f"http://{services['ServiceAddress']}:{services['ServicePort']}" app.run(debug=True, host='0.0.0.0', port=5001)
def _register_service(self): logger.info("registering service to consul") client = Consul(host=self.configuration["CONSUL_HOST"], port=self.configuration["CONSUL_PORT"], scheme=self.configuration["CONSUL_SCHEME"], verify=self.configuration["CONSUL_VERIFY_SSL"]) health_address = "http://{host}:{port}/service/health" health_http = Check.http( url=health_address.format(host=self.configuration["HOST"], port=self.configuration["PORT"]), interval=self.configuration["CONSUL_HEALTH_INTERVAL"], timeout=self.configuration["CONSUL_HEALTH_TIMEOUT"]) client.agent.service.register(name=self.configuration["SERVICE_NAME"], service_id=generate_service_id( self.configuration["SERVICE_NAME"], self.configuration["HOST"], self.configuration["PORT"]), address=self.configuration["HOST"], port=self.configuration["PORT"], check=health_http)
payload = f'''{{ "command_id": {sc.command_id}, "command_type": "stop", "body": "" }}''' user_id = command['user_id'] server_id = command['server_id'] command_publish_client.publish(f'/commands/{user_id}/{server_id}', payload) return Response() if __name__ == '__main__': service = agent.service check = Check.http('http://instancemanager:5000/', interval='10s', timeout='5s', deregister='10s') ip = socket.gethostbyname('instancemanager') service.register('instancemanager', service_id='instancemanager', address=ip, port=5000, check=check) print('registered with consul') command_output_client = mqtt.Client() command_output_client.username_pw_set('user_01', 'passwd01') command_output_client.on_message = command_output_on_message command_output_client.connect('emqx') command_output_client.subscribe('/command_output/#') command_output_client.loop_start()
from consul import Consul, Check from lorem.text import TextLorem import time consul_port = 8500 time.sleep(10) models.Base.metadata.create_all(bind=engine) app = FastAPI(docs_url='/docs', openapi_url='/openapi.json') consul = Consul(host='consul', port=8500) agent = consul.agent service = agent.service check = Check.http('http://referral:5050/', interval='10s', timeout='5s', deregister='10s') ip = socket.gethostbyname('referral') service.register('referral', service_id='referral', address=ip, port=5050, check=check) def get_db(): try: db = SessionLocal() yield db finally: db.close()
def _http(cls, service, params, proto='http'): """ Consul HTTP Check This feature is only available when using Consul 0.5 or newer. Containers specifying these extra metadata in labels or environment will be used to register an HTTP health check with the service. SERVICE_80_CHECK_HTTP=/health/endpoint/path SERVICE_80_CHECK_INTERVAL=15s SERVICE_80_CHECK_TIMEOUT=1s # optional, Consul default used otherwise It works for services on any port, not just 80. If its the only service, you can also use SERVICE_CHECK_HTTP. Consul HTTPS Check This feature is only available when using Consul 0.5 or newer. Containers specifying these extra metedata in labels or environment will be used to register an HTTPS health check with the service. SERVICE_443_CHECK_HTTPS=/health/endpoint/path SERVICE_443_CHECK_INTERVAL=15s SERVICE_443_CHECK_TIMEOUT=1s # optional, Consul default used otherwise """ # https://github.com/cablehead/python-consul/blob/53eb41c4760b983aec878ef73e72c11e0af501bb/consul/base.py#L66 # https://github.com/gliderlabs/registrator/blob/master/docs/user/backends.md#consul-http-check # https://github.com/gliderlabs/registrator/blob/4322fe00304d6de661865721b073dc5c7e750bd2/consul/consul.go#L97 # https://github.com/poppyred/python-consul2/blob/b1057552427ccad11c03f7d60743336f77d0f7ea/consul/base.py#L66 # https://www.consul.io/docs/discovery/checks#http-interval path = cls._value(params, proto) if path: # Perform a HTTP GET against *url* every *interval* (e.g. "10s") to perfom # health check with an optional *timeout* and optional *deregister* after # which a failing service will be automatically deregistered. Optional # parameter *header* specifies headers sent in HTTP request. *header* # paramater is in form of map of lists of strings, # e.g. {"x-foo": ["bar", "baz"]}. url = f"{proto}://{service.ip}:{service.port}{path}" timeout = cls._value(params, 'timeout') interval, deregister = cls._common_values(params) tls_skip_verify = cls._bool_value(params, 'tls_skip_verify') header = cls._json_value(params, 'header') ret = Check.http(url, interval, timeout=timeout, deregister=deregister, header=header, tls_skip_verify=tls_skip_verify) method = cls._value(params, proto + '_method') if method: if cls.consul_version <= (0, 8, 5): # method was buggy before that # https://github.com/hashicorp/consul/blob/master/CHANGELOG.md#085-june-27-2017 return None # FIXME: as 2021/01/20, python-consul doesn't support setting method # https://github.com/hashicorp/consul/blob/master/CHANGELOG.md#084-june-9-2017 ret['Method'] = method.upper() body = cls._value(params, 'body') if body: if cls.consul_version < (1, 7, 0): # not implemented before 1.7.0 return None # consul >= 1.7.0 # https://github.com/hashicorp/consul/pull/6602 # https://github.com/hashicorp/consul/blob/master/CHANGELOG.md#170-february-11-2020 ret['Body'] = body return cls._post_process(ret, params) return None