# gRPC 错误：UNAVAILABLE：目标 dns:///my-service:50051 名称解析失败

- **ID:** `api/grpc-unavailable-service-name`
- **领域:** api
- **类别:** network_error
- **错误码:** `UNAVAILABLE`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

DNS 解析器无法将服务主机名解析为 IP 地址，或者解析后的地址不可达。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC v1.50+ | active | — | — |
| gRPC-Go v1.50+ | active | — | — |
| gRPC-Java v1.50+ | active | — | — |

## 解决方案

1. ```
   使用 nslookup 或 dig 检查 DNS 解析：nslookup my-service。确保主机名正确且 DNS 服务器可达。如果使用 Kubernetes，验证服务名称和命名空间：kubectl get svc my-service -n your-namespace
   ```
2. ```
   直接在目标 URI 中使用 IP 地址以绕过 DNS：target ipv4:///10.0.0.1:50051，或在 gRPC 选项中配置自定义解析器。
   ```

## 无效尝试

- **** — Restarting the gRPC client without fixing DNS configuration does not resolve the underlying resolution failure. (60% 失败率)
- **** — Increasing the timeout for gRPC calls will not help if the hostname cannot be resolved at all. (30% 失败率)
