Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Cannot use Token to obtain registered services through consul.agent.services () #277

Open
doubledna opened this issue May 3, 2020 · 1 comment

Comments

@doubledna
Copy link

doubledna commented May 3, 2020

When I use the python-consul package and connect to the consul service with ACL added, I can register the service through the token, but I cannot get the registered service through the token
Registration code:
`
class RegisterService(object):
def init(self, service_name, service_ip, service_port):
self.service_name = service_name
self.service_ip = service_ip
self.service_port = service_port

def register_service(self):
    connect_consul = ConnectConsul().connect_consul().connect_consul()
    check = consul.Check.tcp(self.service_ip, self.service_port, "10s")
    connect_consul.agent.service.register(self.service_name, "%s:%s:%s" % (self.service_name, self.service_ip,
                                                                           self.service_port),
                                          address=self.service_ip, port=self.service_port, check=check)

`

This can register the service normally

Get service code:
`
class DiscoveryService(object):
def init(self, service_name):
self.service_name = service_name

def discovery_service(self):
    connect_consul = ConnectConsul().connect_consul().connect_consul()
    services = connect_consul.agent.services()
    service = services.get(self.service_name)
    if not service:
        return None
    address = "{0}:{1}".format(service['Address'], service['Port'])
    return service, address

`

This return None

When I can get the service through http:
def discovery_service_request_mode(self): host = ConnectConsul().connect_consul().host port = ConnectConsul().connect_consul().port token = ConnectConsul().connect_consul().token url = "http://%s:%s/v1/agent/services" % (host, str(port)) payload = {} headers = { 'Authorization': 'Bearer %s' % token } response = requests.request("GET", url, headers=headers, data=payload) print(json.loads(response.text))

@poppyred
Copy link

@doubledna 老哥来这儿 一起维护 https://github.com/poppyred/python-consul2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants