【问题标题】:azure.servicebus.common.errors.ServiceBusConnectionError: Failed to open handler: Unable to open authentication session on connectionazure.servicebus.common.errors.ServiceBusConnectionError:无法打开处理程序:无法在连接时打开身份验证会话
【发布时间】:2020-09-10 08:50:56
【问题描述】:

我正在使用用于 python 的 Azure 服务总线库从队列中读取消息。 x 一段时间后 我收到以下错误:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/site-packages/uamqp/authentication/cbs_auth.py", line 76, in create_authenticator
    self._connection.container_id)
  File "./src/cbs.pyx", line 73, in uamqp.c_uamqp.CBSTokenAuth.__cinit__
ValueError: Unable to open CBS link.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/site-packages/azure/servicebus/receive_handler.py", line 309, in open
    self._handler.open(connection=self.connection)
  File "/opt/anaconda3/lib/python3.7/site-packages/uamqp/client.py", line 259, in open
    self._build_session()
  File "/opt/anaconda3/lib/python3.7/site-packages/uamqp/client.py", line 214, in _build_session
    on_attach=self._on_attach)
  File "/opt/anaconda3/lib/python3.7/site-packages/uamqp/authentication/cbs_auth.py", line 82, in create_authenticator
    "Please confirm target hostname exists: {}".format(connection.container_id, connection.hostname))
uamqp.errors.AMQPConnectionError: Unable to open authentication session on connection b'SBReceiver-00000000-0000-0000-0000-000000000000'.
Please confirm target hostname exists: b'myhostname.servicebus.windows.net'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/opt/anaconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/path/to/main.py", line 648, in <module>
    main()
  File "/path/to/main.py", line 631, in main
    run_service_bus()
  File "/path/to/main.py", line 482, in run_service_bus
    with my_client.get_receiver() as queue_receiver:
  File "/opt/anaconda3/lib/python3.7/site-packages/azure/servicebus/base_handler.py", line 57, in __enter__
    self.open()
  File "/opt/anaconda3/lib/python3.7/site-packages/azure/servicebus/receive_handler.py", line 318, in open
    self._handle_exception(e)
  File "/opt/anaconda3/lib/python3.7/site-packages/azure/servicebus/base_handler.py", line 131, in _handle_exception
    raise ServiceBusConnectionError(message, exception)
azure.servicebus.common.errors.ServiceBusConnectionError: Failed to open handler: Unable to open authentication session on connection b'SBReceiver-00000000-0000-0000-0000-000000000000'.
Please confirm target hostname exists: b'myhostname.servicebus.windows.net'

我认为这里发生的事情是一段时间后我拥有的令牌过期。处理这个问题的正确方法是什么?

我使用的代码如下:

sb_client = ServiceBusClient.from_connection_string("primary_connection_string_here")
my_client = sb_client.get_queue("queue_name_here")
    with my_client.get_receiver() as queue_receiver:
        messages = queue_receiver.fetch_next(timeout=3)
        for message in messages:
            message.complete()

【问题讨论】:

  • 当您说“经过 x 段时间”时,您的意思是您能够成功处理并完成消息,直到该 x 段时间?查看error,您的连接字符串似乎是错误的,在这种情况下您应该总是看到此错误。在这种情况下,明显的 x 周期将是 SB 客户端的重试连接。
  • 我可以正常处理消息。这 x 周期是几天。
  • 好的。感谢您的确认。您收到的错误是一个短暂的错误,这是正常的,应该在重试时恢复。你能告诉我你的azure-servicebus 版本吗?目前,7.0.0 提供内置重试功能(预览版,即将 GA)
  • 我当前的版本是:0.50.3。好的,据我了解,这将很快推出。您能否根据我的问题提供一个使用示例?谢谢!
  • 详细回答。

标签: python azureservicebus servicebus azure-servicebus-queues


【解决方案1】:

正如在 cmets 中讨论的那样,这种情况下的问题很可能是由于网络瞬态错误造成的,这在分布式环境中是很正常的。大多数情况下的瞬态错误可以通过重试来恢复。不幸的是,在 v0.50.x 的旧版 python 服务总线 SDK 中,没有开箱即用的重试功能。在latest V7 SDK 中添加了指数回退重试(当前处于预览状态,很快将是 GA)。详情可以参考migration guide from v0.50 to v7。下面是一个使用 V7 SDK 的接收器代码示例(注意:同步变体,还有异步支持,您可以在 samples 的广泛列表中查看)。

V7 SDK 现在允许您为客户端传递重试参数。虽然默认值通常就足够了。

retry_total :允许的重试总数。优先于其他计数。默认值为 10。

retry_backoff_factor :第二次尝试后在尝试之间应用的退避因子(大多数错误会立即通过第二次尝试解决,不会有延迟)。在固定模式下,重试策略将始终休眠 {backoff factor}。在“指数”模式下,重试策略将休眠:{backoff factor} * (2 ** ({number of total retries} - 1)) 秒。如果 backoff_factor 为 0.1,则重试将在重试之间休眠 [0.0s, 0.2s, 0.4s, ...]。默认值为 0.8。

retry_backoff_max : 最大回退时间。默认值为 120(以秒为单位)。

servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, retry_total=10, retry_backoff_factor=1, retry_backoff_max=30)

with servicebus_client:
    receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME)
    with receiver:
        received_msgs = receiver.receive_messages(max_message_count=10, max_wait_time=5)
        for msg in received_msgs:
            print(str(msg))
            msg.complete()

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 2011-10-20
    • 2016-01-16
    • 2015-07-09
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多