refactor: 使用环境变量代替IP哈希生成节点ID
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -18,6 +17,7 @@ import (
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/genv"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
@@ -158,37 +158,18 @@ func catchSQLHook() gdb.HookHandler {
|
||||
}
|
||||
}
|
||||
|
||||
func getNodeIdFromIPPort(ctx context.Context) int64 {
|
||||
// 获取本地IP
|
||||
ip, err := utils.GetLocalIP()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
// 获取端口
|
||||
port := g.Cfg().MustGet(ctx, "server.address").String()
|
||||
// 拼接字符串
|
||||
addr := fmt.Sprintf("%s%s", ip, port)
|
||||
|
||||
// 计算哈希(保证唯一且稳定)
|
||||
h := fnv.New64a()
|
||||
h.Write([]byte(addr))
|
||||
hashVal := h.Sum64()
|
||||
|
||||
// 取模 1024 → 得到 0~1023 的合法 node
|
||||
return int64(hashVal % 1024)
|
||||
}
|
||||
|
||||
// ==================== Insert钩子 ====================
|
||||
|
||||
func insertHook(ctx context.Context, in *gdb.HookInsertInput) (result sql.Result, err error) {
|
||||
userInfo, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodeId := getNodeIdFromIPPort(ctx)
|
||||
if nodeId == 0 {
|
||||
return nil, fmt.Errorf("nodeId cannot be empty")
|
||||
|
||||
nodeId := genv.Get("APP_NODE", "").Int64()
|
||||
if g.IsEmpty(nodeId) {
|
||||
nodeId = 1
|
||||
}
|
||||
|
||||
node, err := snowflake.NewNode(nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -444,61 +444,19 @@ LOOP:
|
||||
goto LOOP
|
||||
}
|
||||
|
||||
// GetLocalIP 获取本地IP ✅ 阿里云 ECS✅ 腾讯云 CVM✅ 华为云✅ 物理机✅ Docker 容器✅ K8s Pod✅ 虚拟机
|
||||
func GetLocalIP() (string, error) {
|
||||
// 先获取所有网卡
|
||||
ifaces, err := net.Interfaces()
|
||||
// IsLocalIP 判断是否是本地IP
|
||||
func IsLocalIP(ip string) bool {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return false
|
||||
}
|
||||
|
||||
// 遍历网卡,找符合条件的
|
||||
for _, iface := range ifaces {
|
||||
// 跳过 禁用、回环、虚拟网卡
|
||||
if iface.Flags&net.FlagUp == 0 || // 网卡未启用
|
||||
iface.Flags&net.FlagLoopback != 0 || // 回环地址
|
||||
strings.Contains(iface.Name, "docker") || // docker 网卡
|
||||
strings.Contains(iface.Name, "veth") || // 容器虚拟网卡
|
||||
strings.Contains(iface.Name, "bridge") || // 网桥
|
||||
strings.Contains(iface.Name, "lo") { // 本地回环
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取网卡地址
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
ipNet, ok := addr.(*net.IPNet)
|
||||
if !ok || ipNet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := ipNet.IP
|
||||
if ip.To4() != nil && isPrivateIP(ip) { // 只取内网 IPv4
|
||||
return ip.String(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("cannot find valid local private IP")
|
||||
}
|
||||
|
||||
// 判断是否内网IP(生产必须)
|
||||
func isPrivateIP(ip net.IP) bool {
|
||||
privateIPBlocks := []string{
|
||||
"10.0.0.0/8",
|
||||
"172.16.0.0/12",
|
||||
"192.168.0.0/16",
|
||||
}
|
||||
|
||||
for _, block := range privateIPBlocks {
|
||||
_, ipNet, err := net.ParseCIDR(block)
|
||||
if err == nil && ipNet.Contains(ip) {
|
||||
if ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
|
||||
if ipNet.IP.String() == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user