# rpc错误：代码=不可用 描述=连接错误：描述="传输：拨号时出错：拨号tcp 127.0.0.1:8080：连接被拒绝"

- **ID:** `go/grpc-unavailable-connection-refused`
- **领域:** go
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

gRPC客户端尝试连接到未运行或端口错误的服务器。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Ensure the gRPC server is running and accessible at the configured address.** (95% 成功率)
   ```
   Check server health by verifying the process is active and listening on the expected port. Use netstat or lsof to confirm.
   ```
2. **Implement a health check and retry logic with circuit breaker.** (85% 成功率)
   ```
   conn, err := grpc.Dial("localhost:8080", grpc.WithInsecure(), grpc.WithBlock(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024*1024*4)))
if err != nil {
    log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
   ```

## 无效尝试

- **Retry the connection with exponential backoff without checking server status.** — If the server is down, retries will keep failing until the server is up. This wastes resources and delays detection. (90% 失败率)
- **Increase the dial timeout in client configuration.** — A longer timeout does not fix the fundamental issue of server unavailability; it just delays the error. (80% 失败率)
