【问题标题】:ServiceUnavailable: 503 DNS resolution failed for service: firestore.googleapis.comServiceUnavailable:503 DNS解析服务失败:firestore.googleapis.com
【发布时间】:2021-09-28 12:34:45
【问题描述】:

我正在尝试使用 Python 代码(在 Jupyter Notebook 中)在我的项目中访问 Google 的 Firestore,但由于某种原因,Python 程序永远不会完成。 还尝试在 Firestore 中创建文档,但随后出现此错误:

ServiceUnavailable: 503 DNS resolution failed for service: firestore.googleapis.com

我在 Firestore 中创建了一个服务帐户(ServiceAccount.json 包含设置)。这是我的代码:

import firebase_admin

from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate('ServiceAccount.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
db.collection('company').add({'name': 'Test'})

只有最后一条语句失败。 这也被返回:

_InactiveRpcError                         Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\google\api_core\grpc_helpers.py in error_remapped_callable(*args, **kwargs)
     65         try:
---> 66             return callable_(*args, **kwargs)
     67         except grpc.RpcError as exc:

C:\ProgramData\Anaconda3\lib\site-packages\grpc\_channel.py in __call__(self, request, timeout, metadata, credentials, wait_for_ready, compression)
    945                                       wait_for_ready, compression)
--> 946         return _end_unary_response_blocking(state, call, False, None)
    947 

C:\ProgramData\Anaconda3\lib\site-packages\grpc\_channel.py in _end_unary_response_blocking(state, call, with_call, deadline)
    848     else:
--> 849         raise _InactiveRpcError(state)
    850 

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "DNS resolution failed for service: firestore.googleapis.com"
    debug_error_string = "{"created":"@1632831473.221000000","description":"Resolver transient failure","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":1356,"referenced_errors":[{"created":"@1632831473.221000000","description":"DNS resolution failed for service: firestore.googleapis.com","file":"src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc","file_line":202,"grpc_status":14,"referenced_errors":[{"created":"@1632831473.221000000","description":"OS Error","file":"src/core/lib/iomgr/resolve_address_windows.cc","file_line":93,"os_error":"The specified class was not found.\r\n","syscall":"getaddrinfo","wsa_error":10109}]}]}"
>

The above exception was the direct cause of the following exception:

ServiceUnavailable                        Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\google\api_core\retry.py in retry_target(target, predicate, sleep_generator, deadline, on_error)
    189         try:
--> 190             return target()
    191
...

【问题讨论】:

  • 如果我尝试检查firestore.googleapis.com(Linux:whois firestore.googleapis.com)的 IP,则表明该地址不存在。也许他们改变了一些东西,它需要更新模块或文件.json。或者也许这只是服务器的暂时问题,您必须等待几天,它会再次工作。
  • 当我 ping firestore.googleapis.com 时,我确实收到了回复:C:\&gt;ping firestore.googleapis.com Pinging firestore.googleapis.com [142.251.36.42] with 32 bytes of data: Reply from 142.251.36.42: bytes=32 time=21ms TTL=117 Reply from 142.251.36.42: bytes=32 time=26ms TTL=117 Reply from 142.251.36.42: bytes=32 time=18ms TTL=117 Reply from 142.251.36.42: bytes=32 time=18ms TTL=117 Ping statistics for 142.251.36.42: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 18ms, Maximum = 26ms, Average = 20ms
  • 但是我的 Python 代码仍然冻结(在 get() 的情况下)或者当我尝试创建数据时返回相同的错误。

标签: python google-cloud-firestore


【解决方案1】:

您使用的是 Windows 吗?如果你是,我相信你已经打了bug。虽然 c-ares DNS 解析器问题已得到修复,但您可能使用的是较旧的软件包。

您可以通过检查已配置的环境变量来解决此问题。如果您在 Jupyter Notebook 单元之一中运行它:

import os
os.environ

您应该得到一长串变量。看看GRPC_DNS_RESOLVER是什么:

environ{'SHELL': '/bin/bash',
        'OS_IMAGE_FAMILY': 'debian-10',
        'CONDA_EXE': '/opt/conda/bin/conda',
        '_CE_M': '',
        'DL_ANACONDA_HOME': '/opt/conda',

根据documentation

GRPC_DNS_RESOLVER 声明使用哪个 DNS 解析器。如果 gRPC 是使用 c-ares 支持构建的,则默认值为 ares。否则,此环境变量的值将被忽略。可用的 DNS 解析器包括:

ares(除 iOS、Android 或 Node 之外的大多数平台上的默认设置)- 基于 c-ares 库的 DNS 解析器

native - 基于 getaddrinfo() 的 DNS 解析器,创建一个新线程来执行名称解析

然后您可以像这样将GRPC_DNS_RESOLVER 设置为原生:

os.environ['GRPC_DNS_RESOLVER'] = 'native'

【讨论】:

  • 谢谢安德鲁,我会测试一下。事实上,它在 Windows 计算机上。同时我也在Mac上测试了我的代码,运行流畅。
  • 这是个好消息。我相信 iOS 无论如何都会使用本机,因为根据文档,ares 不是 Mac 的默认设置。让我知道测试的进展,因为我有兴趣知道结果。
  • 嗨,安德鲁,不走运。首先 os.environ 没有显示 GRPC_DNS_RESOLVER 设置。其次,在执行 os.environ['GRPC_DNS_RESOLVER'] = 'native' 并再次运行我的代码后,用于检索数据的代码仍然永远不会结束并且不会返回任何数据。 (我通过 Anaconda 或 VS Code 使用带或不带 VPN 的工作计算机和 Jupyter Notebook)
  • 真可惜。重启内核后呢? Anaconda 和 Jupyter 笔记本是在本地运行还是在容器中运行?您可以在浏览器中访问developer tools 吗?我想知道您是否可以深入研究网络选项卡下的请求标头并查看 503 错误的更多详细信息。可能值得一看。
  • 重启内核后还是不行。 Anaconda 和 Jupyter Notebook 以及 VS Code 都在我的笔记本电脑上本地运行。会做一些进一步的研究。
猜你喜欢
  • 1970-01-01
  • 2019-02-03
  • 2020-09-18
  • 2020-05-21
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 2015-11-19
相关资源
最近更新 更多