From 592470b9f5b68f936f34b1efc5d3f875d78c91ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=98=8E=E5=BF=97?= Date: Wed, 18 Mar 2026 10:50:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=AE=A2=E9=98=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89LocalIP(#872)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clients/config_client/config_client.go | 4 ++++ common/constant/client_config_options.go | 8 ++++++++ common/constant/config.go | 1 + util/common.go | 14 ++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/clients/config_client/config_client.go b/clients/config_client/config_client.go index fa64b6df..5242e5f0 100644 --- a/clients/config_client/config_client.go +++ b/clients/config_client/config_client.go @@ -111,6 +111,10 @@ func NewConfigClientWithRamCredentialProvider(nc nacos_client.INacosClient, prov if err != nil { return nil, err } + // Set custom client IP if configured + if clientConfig.ClientIP != "" { + util.SetCustomClientIP(clientConfig.ClientIP) + } serverConfig, err := nc.GetServerConfig() if err != nil { return nil, err diff --git a/common/constant/client_config_options.go b/common/constant/client_config_options.go index c302d46a..1f8fba65 100644 --- a/common/constant/client_config_options.go +++ b/common/constant/client_config_options.go @@ -244,3 +244,11 @@ func WithAppConnLabels(appConnLabels map[string]string) ClientOption { config.AppConnLabels = appConnLabels } } + +// WithClientIP sets custom client IP for gRPC communication. +// This is useful in containerized environments where the auto-detected IP might be incorrect. +func WithClientIP(clientIP string) ClientOption { + return func(config *ClientConfig) { + config.ClientIP = clientIP + } +} diff --git a/common/constant/config.go b/common/constant/config.go index 6af6da95..af5d5ee7 100644 --- a/common/constant/config.go +++ b/common/constant/config.go @@ -61,6 +61,7 @@ type ClientConfig struct { EndpointQueryParams string // the address server endpoint query params ClusterName string // the address server clusterName AppConnLabels map[string]string // app conn labels + ClientIP string // the custom client ip, if not set, will use local ip auto detected } type ClientLogSamplingConfig struct { diff --git a/util/common.go b/util/common.go index 2097dd1e..036b5ae2 100644 --- a/util/common.go +++ b/util/common.go @@ -67,8 +67,22 @@ func GetConfigCacheKey(dataId string, group string, tenant string) string { } var localIP = "" +var customClientIP = "" + +// SetCustomClientIP sets a custom client IP to be used in gRPC communication. +// This will override the auto-detected local IP. +// Useful for containerized environments where the auto-detected IP might be incorrect. +func SetCustomClientIP(ip string) { + customClientIP = ip + if len(customClientIP) > 0 { + logger.Infof("Custom Client IP set to: %s", customClientIP) + } +} func LocalIP() string { + if customClientIP != "" { + return customClientIP + } if localIP == "" { netInterfaces, err := net.Interfaces() if err != nil {