def test_session(self): """ Ensures that the session has the expected connectivity """ httpretty.enable() httpretty.register_uri( httpretty.GET, "http://consul.internal:8501/v1/status/leader", body="localhost:8300", ) app = self.create_app() consul = Consul(app, consul_host="consul.internal", consul_port="8501", test_connection=True) self.assertIsNotNone(consul) httpretty.disable() httpretty.reset() app = self.create_app() self.assertRaises( ConsulConnectionError, lambda: Consul(app, consul_host="consul.internal", consul_port="8501", test_connection=True), )
def test_extension_registration(self): """ Tests that the consul extension has been registered and initialized """ app = self.create_app() consul = Consul(app) self.assertIn('consul', app.extensions) self.assertEqual(consul, app.extensions['consul']) app = self.create_app() consul = Consul() consul.init_app(app) self.assertIn('consul', app.extensions) self.assertEqual(consul, app.extensions['consul'])
def test_config_with_defaults(self): """ Ensures that application config is loaded by consul using the default namespace and host """ with MockConsulKV(): app = self.create_app() consul = Consul(app) consul.apply_remote_config() self.assertEqual(app.config['cfg_1'], 'consul_1') self.assertEqual(app.config['cfg_3'], 'consul_3') self.assertEqual(app.config['cfg_4'], {'inner': 'value'})
def main(): consul = Consul(app=app) #consul.apply_remote_config(namespace='mynamespace/') consul.register_service( name='my-web-app', interval='10s', tags=[ 'webserver', ], httpcheck='http://127.0.0.1:12345/healthcheck', address='127.0.0.1', port=12345, ) app.run(host='0.0.0.0', port=12345)
def test_register_service(self): """ Service registration should call the underlying consulate register api """ httpretty.enable() httpretty.register_uri( httpretty.GET, 'http://consul.internal:8500/v1/status/leader', body="localhost:8300", ) app = self.create_app() with mock.patch('consulate.Session') as mocked: consul = Consul(app) consul.register_service() self.assertEqual(mocked.return_value.agent.service.register.call_count, 1) httpretty.disable() httpretty.reset()
def test_register_service(self): """ Service registration should call the underlying consulate register api """ httpretty.enable() httpretty.register_uri( httpretty.GET, "http://consul.internal:8500/v1/status/leader", body="localhost:8300", ) app = self.create_app() with mock.patch('consulate.Session') as mocked: consul = Consul(app) consul.register_service() self.assertEqual( mocked.return_value.agent.service.register.call_count, 1) httpretty.disable() httpretty.reset()
def test_config_with_environ(self): """ Ensures that application config is loaded by consul using host and namespace defined by environmental variables """ os.environ['CONSUL_HOST'] = "consul.adsabs" os.environ['SERVICE'] = "sample_application" os.environ['ENVIRONMENT'] = "testing" with MockConsulKV(): app = self.create_app() consul = Consul(app) consul.apply_remote_config() del os.environ['CONSUL_HOST'] del os.environ['SERVICE'] del os.environ['ENVIRONMENT'] self.assertEqual(app.config['cfg_1'], 'consul_1') self.assertEqual(app.config['cfg_3'], 'consul_3')
def initialize_consul_client(app): server_port = app.config['SERVER_PORT'] app_name = app.config['APP_NAME'] profile = app.config['SPRING_PROFILES_ACTIVE'] hostname = app.config['HOSTNAME'] # Consul # This extension should be the first one if enabled: consul = Consul(app=app) # Fetch the configuration: consul.apply_remote_config(namespace=f'config/application,{profile}/data') # Register Consul service: consul.register_service(name=app_name, interval='30s', tags=[app_name], port=server_port, httpcheck=hostname + ":" + str(server_port) + "/actuator/health") if profile != 'prod': jwt_secret = "" for data in yaml.load_all(app.config[''], Loader=yaml.BaseLoader): try: jwt_secret = data['com']['microservice']['authentication'][ 'jwt']['keyValue'] break except Exception: log.warning("Not found jwt_secret") if jwt_secret == "": raise Exception("jwt_secret not found") log.debug('Jwt Secret: %s', jwt_secret) app.config['JWT_SECRET_KEY'] = jwt_secret else: app.config['JWT_PUBLIC_KEY'] = open(app.config['JWT_PUBLIC_KEY'], "r").read() log.debug('Config environment: %s', app.config) # provide app's version and deploy environment/config name to set a gauge metric register_metrics(app, app_version="v0.1.2", app_config="staging")
def register_consul(app): if 'CONSUL_SERVER_HOST' not in app.config: return app host_ip = app.config['SERVER_HOST'] or get_ip_address( app.config['HOST_ADAPTER']) consul = Consul(app=app, consul_host=app.config['CONSUL_SERVER_HOST'], consul_port=app.config['CONSUL_SERVER_PORT']) consul.apply_remote_config(namespace=app.config['CONSUL_NAMESPACE']) consul.register_service(name=app.config['CONSUL_SERVICE_NAME'], interval=app.config['CONSUL_REGISTER_INTERVAL'], tags=app.config['CONSUL_REGISTER_TAGS'], port=app.config['SERVER_PORT'], address=host_ip, httpcheck='http://{host}:{port}/health'.format( host=host_ip, port=app.config['SERVER_PORT'])) register_health_check_api(app) return app
from logging.handlers import TimedRotatingFileHandler import datetime import atexit import json import os import vk app = Flask(__name__) CORS(app, supports_credentials=True, resources=r'/api/*') FlaskDynaconf(app) consul = Consul(app=app) # конфигурация для Consul consul.register_service(name=app.config.consul.name, interval=app.config.consul.interval, tags=app.config.consul.tags, port=app.config.consul.port, httpcheck=app.config.consul.httpcheck) base_path = os.path.dirname(os.path.abspath(__file__)) api = Api(app, version='0.0.1', title='Metrikano Classification API', description='Классификация данных в сервисе Metrikano', doc=app.config.docs,
@app.route('/') def home(): """ This function is used to say current status to the Consul. Format: https://www.consul.io/docs/agent/checks.html :return: Empty response with status 200, 429 or 500 """ # TODO: implement any other checking logic. return 'Works', 200 # Consul # This extension should be the first one if enabled: consul = Consul(app=app, consul_host='consul', consul_port=8500) # Fetch the conviguration: # consul.apply_remote_config(namespace='mynamespace/') # Register Consul service: consul.register_service(name='account', interval='10s', tags=[ 'webserver', ], address=ip, port=80, httpcheck='http://{}/healthcheck'.format(ip)) app.run(host='0.0.0.0', port=80)
from flask_consulate import Consul import socket consul = Consul() def register_services(app): consul.init_app(app) host_name = socket.gethostname() consul.register_service(service_id=socket.gethostname(), name='dialogs-app', address=app.config.get('APP_HOST'), interval='10s', port=int(app.config.get('APP_PORT')), httpcheck=f'http://{host_name}:8000/health')
app = Flask(__name__) @app.route('/healthcheck') def health_check(): """ This function is used to say current status to the Consul. Format: https://www.consul.io/docs/agent/checks.html :return: Empty response with status 200, 429 or 500 """ # TODO: implement any other checking logic. return '', 200 # Consul # This extension should be the first one if enabled: consul = Consul(app=app) # Fetch the conviguration: consul.apply_remote_config(namespace='mynamespace/') # Register Consul service: consul.register_service(name='my-web-app', interval='10s', tags=[ 'webserver', ], port=5000, httpcheck='http://localhost:5000/healthcheck') if __name__ == '__main__': app.run()
class BLConsul: __instance = None consul = None @staticmethod def get_instance(): if BLConsul.__instance is None: BLConsul() return BLConsul.__instance def __init__(self): """ Virtually private constructor. """ if BLConsul.__instance is not None: raise Exception("This class is a singleton!") else: BLConsul.__instance = self def init_and_register(self, app): self.consul = Consul(app=app) self.register_service() def register_service(self): print("Service registered with {}:{} {}:{}".format( config.SERVICE_ID, config.SERVICE_NAME, config.IP, config.PORT)) self.consul.register_service( service_id=config.SERVICE_ID, name=config.SERVICE_NAME, interval='10s', tags=['flask', 'microservice', 'aas'], port=config.PORT, address=config.IP, httpcheck='http://{host}:{port}/{service_name}/health'.format( host=config.IP, port=config.PORT, service_name=config.SERVICE_NAME)) # This could be an alternative using Consul's REST API # def get_service(self, service_name): # ret_service = None # self.get_service_url(service_name) # healthy_services = self.consul.session.health.state("passing") # services = self.consul.session.agent.services() # for healthy_service in healthy_services: # if service_name == healthy_service['ServiceName']: # service = services[0][healthy_service['ServiceID']] # if service['Service'] == service_name: # ret_service = service # break # return ret_service def get_service(self, service_name): ret = {"Address": None, "Port": None} try: srv_results = consul_resolver.query( "{}.service.consul".format(service_name), "srv") # SRV DNS query srv_list = srv_results.response.answer # PORT - target_name relation a_list = srv_results.response.additional # IP - target_name relation print(" " * 50) print(a_list) print(" " * 50) # DNS returns a list of replicas, supposedly sorted using Round Robin. We always get the 1st element: [0] srv_replica = srv_list[0][0] port = srv_replica.port target_name = srv_replica.target # From all the IPs, get the one with the chosen target_name for a in a_list: if a.name == target_name: ret['Address'] = a[0] ret['Port'] = port break except dns.exception.DNSException as e: print("Could not get service url: {}".format(e)) return ret # Get Key Values from Consul's key store def get_key_value_items(self): return self.consul.session.kv.items() def get_service_catalog(self): return self.consul.session.catalog.services() def get_service_replicas(self): return self.consul.session.agent.services()
def init_and_register(self, app): self.consul = Consul(app=app) self.register_service()