Files
cattleData/backend/TEST_CONNECTION.md

134 lines
3.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 连接测试指南
## 当前状态
- ✅ 服务在服务器上正常运行
- ✅ 本地访问成功 (localhost:12240)
- ❌ 外部访问失败 (119.45.30.82:12240)
## 问题原因
外部无法访问通常是以下原因之一:
1. **服务器防火墙未开放端口**(最常见)
2. **云服务器安全组未配置**
3. **网络路由问题**
## 解决步骤
### 步骤1: 配置服务器防火墙
在服务器上执行:
```bash
# 方法1: 使用 firewalld (CentOS 7+/RHEL 7+)
sudo firewall-cmd --add-port=12240/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports # 验证
# 方法2: 使用 iptables
sudo iptables -A INPUT -p tcp --dport 12240 -j ACCEPT
sudo service iptables save
# 或
sudo iptables-save > /etc/sysconfig/iptables
# 方法3: 使用提供的脚本
chmod +x open_firewall.sh
sudo ./open_firewall.sh
```
### 步骤2: 配置云服务器安全组
#### 阿里云
1. 登录阿里云控制台
2. 进入 **ECS****网络与安全****安全组**
3. 选择对应的安全组
4. 点击 **配置规则****添加安全组规则**
5. 配置:
- 规则方向:入方向
- 授权策略:允许
- 协议类型TCP
- 端口范围12240/12240
- 授权对象0.0.0.0/0或指定IP
#### 腾讯云
1. 登录腾讯云控制台
2. 进入 **云服务器****安全组**
3. 选择对应的安全组 → **修改规则**
4. 添加入站规则:
- 类型:自定义
- 来源0.0.0.0/0
- 协议端口TCP:12240
- 策略:允许
#### 其他云服务商
类似操作,在安全组/防火墙规则中添加 TCP 12240 端口的入站规则。
### 步骤3: 验证配置
```bash
# 1. 检查端口监听(应该显示 0.0.0.0:12240
netstat -tlnp | grep 12240
# 或
ss -tlnp | grep 12240
# 2. 检查防火墙规则
firewall-cmd --list-ports # firewalld
# 或
iptables -L INPUT -n | grep 12240 # iptables
# 3. 从服务器测试外部IP
curl http://119.45.30.82:12240/api/cattle-data
# 4. 从外部测试在本地Windows PowerShell
curl http://119.45.30.82:12240/api/cattle-data
```
## 临时测试方案SSH隧道
如果暂时无法配置防火墙,可以使用 SSH 隧道进行测试:
```bash
# 在本地Windows PowerShell中执行
ssh -L 12240:localhost:12240 root@119.45.30.82
# 然后在另一个终端测试
curl http://localhost:12240/api/cattle-data
```
## 诊断工具
使用提供的诊断脚本:
```bash
chmod +x check_connection.sh
./check_connection.sh
```
## 常见问题
### Q: 防火墙已配置但仍无法访问?
A: 检查云服务器安全组,这是最常见的遗漏。
### Q: 如何确认端口是否开放?
A: 使用在线端口检测工具,或从其他服务器测试。
### Q: 服务只监听 localhost
A: Spring Boot 默认监听 0.0.0.0,如需明确指定:
```yaml
server:
address: 0.0.0.0
port: 12240
```
## 测试命令汇总
```bash
# 服务器端
netstat -tlnp | grep 12240 # 检查监听
curl http://localhost:12240/api/cattle-data # 本地测试
firewall-cmd --list-ports # 查看防火墙端口
# 客户端Windows PowerShell
curl http://119.45.30.82:12240/api/cattle-data
Invoke-WebRequest -Uri http://119.45.30.82:12240/api/cattle-data
```