keijack / python-eureka-client

A eureka client written in python. Support registering your python component to Eureka Server, as well as calling remote services by pulling the the Eureka registry.
MIT License
183 stars 43 forks source link

Unable to catch exceptions thrown during registering process #54

Closed dr4hcu5-jan closed 3 years ago

dr4hcu5-jan commented 3 years ago

Hi there, I currently am trying to dectect if the initial connection to the Eureka Server was successful. I'm currently using the exceptions thrown during the process: socket.timeout. urllib.error.URLError, py_eureka_client.eureka_client.EurekaServerConnectionException with the following code

    try:
        ec.start()
        ec.status_update("UP")
    except (socket.timeout, urllib.error.URLError, py_eureka_client.eureka_client.EurekaServerConnectionException):
        logging.warning("The connection to the Eureka Host could not be opened")

But the Exceptions are not catched and the eureka client prints its own error messages and therefore not letting me inform the user or other depending services.

Have a nice weekend.

Traceback

[2021-05-30 13:05:23]-[eureka_client]-[line:1131] -WARNING: Eureka server [http://192.168.178.74:8761/eureka] is down, use next url to try.
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\urllib\request.py", line 1346, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "C:\Program Files\Python39\lib\http\client.py", line 1253, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1299, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1248, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Program Files\Python39\lib\http\client.py", line 1008, in _send_output
    self.send(msg)
  File "C:\Program Files\Python39\lib\http\client.py", line 948, in send
    self.connect()
  File "C:\Program Files\Python39\lib\http\client.py", line 919, in connect
    self.sock = self._create_connection(
  File "C:\Program Files\Python39\lib\socket.py", line 843, in create_connection
    raise err
  File "C:\Program Files\Python39\lib\socket.py", line 831, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1129, in __try_eureka_servers_in_list
    fun(url)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1268, in do_pull
    self.__applications = get_applications(url, self.__remote_regions)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 404, in get_applications
    return _get_applications_(f"{_format_url(eureka_server)}apps/", regions)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 419, in _get_applications_
    txt = http_client.load(_url, timeout=_DEFAULT_TIME_OUT)[0]
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\http_client.py", line 146, in load
    res = http_cli.urlopen()
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\http_client.py", line 111, in urlopen
    return urllib.request.urlopen(self.request, data=self.data, timeout=self.timeout,
  File "C:\Program Files\Python39\lib\urllib\request.py", line 214, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 517, in open
    response = self._open(req, data)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 534, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Program Files\Python39\lib\urllib\request.py", line 494, in _call_chain
    result = func(*args)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 1375, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Program Files\Python39\lib\urllib\request.py", line 1349, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error timed out>
[2021-05-30 13:05:23]-[eureka_client]-[line:1273] -WARNING: pull full registry from eureka server error!
Traceback (most recent call last):
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1115, in __try_all_eureka_servers
    self.__try_eureka_server_in_zone(fun)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1090, in __try_eureka_server_in_zone
    self.__try_eureka_servers_in_list(fun, self.__eureka_server_conf.servers_in_zone, self.zone)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1140, in __try_eureka_servers_in_list
    raise EurekaServerConnectionException(f"All eureka servers in zone[{_zone}] are down!")
py_eureka_client.eureka_client.EurekaServerConnectionException: <urlopen error All eureka servers in zone[default] are down!>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1271, in __pull_full_registry
    self.__connect_to_eureka_server(do_pull)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1149, in __connect_to_eureka_server
    self.__try_all_eureka_servers(fun)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1117, in __try_all_eureka_servers
    self.__try_eureka_server_not_in_zone(fun)
  File "D:\Arbeit\Repositories\arsu-prognosis-rest\venv\lib\site-packages\py_eureka_client\eureka_client.py", line 1100, in __try_eureka_server_not_in_zone
    raise EurekaServerConnectionException("All eureka servers in all zone are down!")
py_eureka_client.eureka_client.EurekaServerConnectionException: <urlopen error All eureka servers in all zone are down!>
keijack commented 3 years ago

The exceptions are caught and log for the purpose to do the connection retry. However, I add a on_error callback when initializing the eureka client. please check


def on_err(err_type: str, exception: Exception):
    if err_type == eureka_client.ERROR_REGISTER:
        eureka_client.stop()

eureka_client.init(...,
                  on_error=on_err
                  ....)

You should update to version 0.10.0 to use this feature.

keijack commented 3 years ago

I close this, if you have any problem, please re-open it.