class _AsyncServiceDiscovery:
    def __init__(self, options, hostname, event_loop=None):
        self.options = options
        self.consul = AsyncConsul(host=options.consul_host,
                                  port=options.consul_port,
                                  loop=event_loop)
        self.service_name = options.app
        self.hostname = hostname
        self.service_id = _make_service_id(options,
                                           service_name=self.service_name,
                                           hostname=self.hostname)

    async def register_service(self):
        http_check = _create_http_check(self.options)
        await self.consul.agent.service.register(
            self.service_name,
            service_id=self.service_id,
            address=self.hostname,
            port=self.options.port,
            check=http_check,
            tags=self.options.consul_tags,
        )
        log.info('Successfully registered service %s', self.service_id)

    async def deregister_service_and_close(self):
        if await self.consul.agent.service.deregister(self.service_id):
            log.info('Successfully deregistered service %s', self.service_id)
        else:
            log.info('Failed to deregister service %s normally',
                     self.service_id)
        self.consul.close()
示例#2
0
class _AsyncServiceDiscovery:
    def __init__(self, options, event_loop=None):
        self.options = options
        self.consul = AsyncConsul(host=options.consul_host, port=options.consul_port, loop=event_loop)
        self.service_name = options.app
        self.hostname = _get_hostname_or_raise(options.node_name)
        self.service_id = _make_service_id(options, service_name=self.service_name, hostname=self.hostname)
        self.consul_weight_watch_seconds = f'{options.consul_weight_watch_seconds}s'
        self.consul_weight_total_timeout_sec = options.consul_weight_total_timeout_sec
        self.consul_weight_consistency_mode = options.consul_weight_consistency_mode.lower()

    async def register_service(self):
        http_check = _create_http_check(self.options)
        index = None
        old_weight = None
        while True:
            index, value = await self.consul.kv.get(
                f'host/{self.hostname}/weight',
                index=index,
                wait=self.consul_weight_watch_seconds,
                total_timeout=self.consul_weight_total_timeout_sec,
                consistency=self.consul_weight_consistency_mode,
            )
            weight = _get_weight_or_default(value)
            if old_weight != weight:
                old_weight = weight
                register_params = {
                    'service_id': self.service_id,
                    'port': self.options.port,
                    'check': http_check,
                    'tags': self.options.consul_tags,
                    'weights': Weight.weights(weight, 0)
                }
                if await self.consul.agent.service.register(self.service_name, **register_params):
                    log.info('Successfully registered service %s', register_params)
                else:
                    raise Exception(f'Failed to register {self.service_id}')

    async def deregister_service_and_close(self):
        if await self.consul.agent.service.deregister(self.service_id):
            log.info('Successfully deregistered service %s', self.service_id)
        else:
            log.info('Failed to deregister service %s normally', self.service_id)
        self.consul.close()