为了账号安全,请及时绑定邮箱和手机立即绑定

azure.servicebus.common.errors.ServiceBus

azure.servicebus.common.errors.ServiceBus

米琪卡哇伊 2023-07-27 09:46:22
我正在使用适用于 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:我认为这里发生的事情是在一段时间后我拥有的令牌过期了。处理这个问题的正确方法是什么?
查看完整描述

1 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

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

V7 SDK 现在允许您为客户端传递重试参数。虽然默认值一般来说应该足够了。

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

retry_backoff_factor:在第二次尝试之后在尝试之间应用的退避因子(大多数错误可以通过第二次尝试立即解决,没有延迟)。在固定模式下,重试策略将始终休眠 {backoff Factor}。在“指数”模式下,重试策略将休眠:{退避因子} * (2 ** ({总重试次数} - 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()


查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信