ip获取逻辑

This commit is contained in:
2025-11-21 17:57:56 +08:00
parent 27e27acd70
commit deee867ff1

View File

@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"net"
"net/http" "net/http"
"time" "time"
@@ -56,12 +55,10 @@ func (r *Reporter) Stop() {
} }
func (r *Reporter) sendHeartbeat() { func (r *Reporter) sendHeartbeat() {
// 获取本机IP // 新节点不发送IP让后端服务器从请求中获取
localIP := getLocalIP()
// 发送心跳使用Form格式兼容旧接口 // 发送心跳使用Form格式兼容旧接口
url := fmt.Sprintf("%s/api/node/heartbeat", r.cfg.Backend.URL) url := fmt.Sprintf("%s/api/node/heartbeat", r.cfg.Backend.URL)
req, err := http.NewRequest("POST", url, bytes.NewBufferString(fmt.Sprintf("realNodeIP=%s&type=pingServer", localIP))) req, err := http.NewRequest("POST", url, bytes.NewBufferString("type=pingServer"))
if err != nil { if err != nil {
r.logger.Error("创建心跳请求失败", zap.Error(err)) r.logger.Error("创建心跳请求失败", zap.Error(err))
return return
@@ -77,27 +74,9 @@ func (r *Reporter) sendHeartbeat() {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
r.logger.Debug("心跳发送成功", zap.String("ip", localIP)) r.logger.Debug("心跳发送成功后端将从请求中获取节点IP")
} else { } else {
r.logger.Warn("心跳发送失败", zap.Int("status", resp.StatusCode)) r.logger.Warn("心跳发送失败", zap.Int("status", resp.StatusCode))
} }
} }
func getLocalIP() string {
// 获取第一个非回环IP
addrs, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
}
return "127.0.0.1"
}