diff --git a/cluster/cluster_impl/base_cluster_invoker.go b/cluster/cluster_impl/base_cluster_invoker.go index ced5b15cb9..d3552c9476 100644 --- a/cluster/cluster_impl/base_cluster_invoker.go +++ b/cluster/cluster_impl/base_cluster_invoker.go @@ -22,7 +22,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -72,7 +71,7 @@ func (invoker *baseClusterInvoker) IsAvailable() bool { //check invokers availables func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, invocation protocol.Invocation) error { if len(invokers) == 0 { - ip, _ := gxnet.GetLocalIP() + ip := common.GetLocalIp() return perrors.Errorf("Failed to invoke the method %v. No provider available for the service %v from "+ "registry %v on the consumer %v using the dubbo version %v .Please check if the providers have been started and registered.", invocation.MethodName(), invoker.directory.GetUrl().SubURL.Key(), invoker.directory.GetUrl().String(), ip, constant.Version) @@ -84,7 +83,7 @@ func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, in //check cluster invoker is destroyed or not func (invoker *baseClusterInvoker) checkWhetherDestroyed() error { if invoker.destroyed.Load() { - ip, _ := gxnet.GetLocalIP() + ip := common.GetLocalIp() return perrors.Errorf("Rpc cluster invoker for %v on consumer %v use dubbo version %v is now destroyed! can not invoke any more. ", invoker.directory.GetUrl().Service(), ip, constant.Version) } diff --git a/cluster/cluster_impl/failover_cluster_invoker.go b/cluster/cluster_impl/failover_cluster_invoker.go index 4260a9324d..7376249134 100644 --- a/cluster/cluster_impl/failover_cluster_invoker.go +++ b/cluster/cluster_impl/failover_cluster_invoker.go @@ -20,11 +20,11 @@ package cluster_impl import ( "context" "fmt" + "github.com/apache/dubbo-go/common" "strconv" ) import ( - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" ) @@ -89,7 +89,7 @@ func (invoker *failoverClusterInvoker) Invoke(ctx context.Context, invocation pr return result } - ip, _ := gxnet.GetLocalIP() + ip := common.GetLocalIp() invokerSvc := invoker.GetUrl().Service() invokerUrl := invoker.directory.GetUrl() return &protocol.RPCResult{ diff --git a/cluster/directory/base_directory_test.go b/cluster/directory/base_directory_test.go index a2b62dfa00..e51e533f2e 100644 --- a/cluster/directory/base_directory_test.go +++ b/cluster/directory/base_directory_test.go @@ -24,7 +24,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" "github.com/stretchr/testify/assert" ) @@ -55,7 +54,7 @@ func TestBuildRouterChain(t *testing.T) { assert.NotNil(t, directory) - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP)) routeURL := getRouteURL(rule, anyURL) routeURL.AddParam(constant.INTERFACE_KEY, "mock-app") @@ -79,7 +78,7 @@ func TestIsProperRouter(t *testing.T) { regURL := url regURL.AddParam(constant.APPLICATION_KEY, "mock-app") d := NewBaseDirectory(®URL) - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP)) routeURL := getRouteURL(rule, anyURL) routeURL.AddParam(constant.APPLICATION_KEY, "mock-app") diff --git a/cluster/router/condition/factory_test.go b/cluster/router/condition/factory_test.go index b31f6c7a82..8fdfae7404 100644 --- a/cluster/router/condition/factory_test.go +++ b/cluster/router/condition/factory_test.go @@ -26,7 +26,6 @@ import ( ) import ( - "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -164,7 +163,7 @@ func TestRoute_matchWhen(t *testing.T) { } func TestRoute_matchFilter(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() t.Logf("The local ip is %s", localIP) url1, _ := common.NewURL("dubbo://10.20.3.3:20880/com.foo.BarService?default.serialization=fastjson") url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -223,7 +222,7 @@ func TestRoute_methodRoute(t *testing.T) { func TestRoute_ReturnFalse(t *testing.T) { url, _ := common.NewURL("") - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() invokers := []protocol.Invoker{NewMockInvoker(url, 1), NewMockInvoker(url, 2), NewMockInvoker(url, 3)} inv := &invocation.RPCInvocation{} rule := base64.URLEncoding.EncodeToString([]byte("host = " + localIP + " => false")) @@ -234,7 +233,7 @@ func TestRoute_ReturnFalse(t *testing.T) { } func TestRoute_ReturnEmpty(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url, _ := common.NewURL("") invokers := []protocol.Invoker{NewMockInvoker(url, 1), NewMockInvoker(url, 2), NewMockInvoker(url, 3)} inv := &invocation.RPCInvocation{} @@ -246,7 +245,7 @@ func TestRoute_ReturnEmpty(t *testing.T) { } func TestRoute_ReturnAll(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() urlString := "dubbo://" + localIP + "/com.foo.BarService" dubboURL, _ := common.NewURL(urlString) mockInvoker1 := NewMockInvoker(dubboURL, 1) @@ -262,7 +261,7 @@ func TestRoute_ReturnAll(t *testing.T) { } func TestRoute_HostFilter(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -281,7 +280,7 @@ func TestRoute_HostFilter(t *testing.T) { } func TestRoute_Empty_HostFilter(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -300,7 +299,7 @@ func TestRoute_Empty_HostFilter(t *testing.T) { } func TestRoute_False_HostFilter(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -319,7 +318,7 @@ func TestRoute_False_HostFilter(t *testing.T) { } func TestRoute_Placeholder(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -338,7 +337,7 @@ func TestRoute_Placeholder(t *testing.T) { } func TestRoute_NoForce(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) @@ -355,7 +354,7 @@ func TestRoute_NoForce(t *testing.T) { } func TestRoute_Force(t *testing.T) { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() url1, _ := common.NewURL(factory333URL) url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP)) diff --git a/cluster/router/condition/router.go b/cluster/router/condition/router.go index 4267f5b405..48f2aae133 100644 --- a/cluster/router/condition/router.go +++ b/cluster/router/condition/router.go @@ -25,7 +25,6 @@ import ( import ( "github.com/RoaringBitmap/roaring" "github.com/dubbogo/gost/container/set" - "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" ) @@ -184,7 +183,7 @@ func (c *ConditionRouter) Route(invokers *roaring.Bitmap, cache router.Cache, ur return result } else if c.Force { rule, _ := url.GetParamAndDecoded(constant.RULE_KEY) - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() logger.Warnf("The route result is empty and force execute. consumer: %s, service: %s, router: %s", localIP, url.Service(), rule) return result } diff --git a/common/host_util.go b/common/host_util.go new file mode 100644 index 0000000000..1f0a15bf2b --- /dev/null +++ b/common/host_util.go @@ -0,0 +1,13 @@ +package common + +import gxnet "github.com/dubbogo/gost/net" + +var localIp string + +func GetLocalIp() string { + if len(localIp) != 0 { + return localIp + } + localIp, _ = gxnet.GetLocalIP() + return localIp +} diff --git a/common/host_util_test.go b/common/host_util_test.go new file mode 100644 index 0000000000..c253372c0e --- /dev/null +++ b/common/host_util_test.go @@ -0,0 +1,10 @@ +package common + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestGetLocalIp(t *testing.T) { + assert.NotNil(t, GetLocalIp()) +} diff --git a/config/config_loader.go b/config/config_loader.go index c66e526921..f2102249c6 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -28,7 +28,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" ) @@ -250,7 +249,7 @@ func createInstance(url common.URL) (registry.ServiceInstance, error) { host := url.Ip if len(host) == 0 { - host, err = gxnet.GetLocalIP() + host = common.GetLocalIp() if err != nil { return nil, perrors.WithMessage(err, "could not get the local Ip") } diff --git a/config/service_config_test.go b/config/service_config_test.go index 4d4122ee70..61c8864b6a 100644 --- a/config/service_config_test.go +++ b/config/service_config_test.go @@ -18,11 +18,11 @@ package config import ( + "github.com/apache/dubbo-go/common" "testing" ) import ( - gxnet "github.com/dubbogo/gost/net" "github.com/stretchr/testify/assert" "go.uber.org/atomic" ) @@ -184,7 +184,7 @@ func TestExport(t *testing.T) { func TestGetRandomPort(t *testing.T) { protocolConfigs := make([]*ProtocolConfig, 0, 3) - ip, err := gxnet.GetLocalIP() + ip := common.GetLocalIp() protocolConfigs = append(protocolConfigs, &ProtocolConfig{ Ip: ip, }) @@ -194,7 +194,7 @@ func TestGetRandomPort(t *testing.T) { protocolConfigs = append(protocolConfigs, &ProtocolConfig{ Ip: ip, }) - assert.NoError(t, err) + //assert.NoError(t, err) ports := getRandomPort(protocolConfigs) assert.Equal(t, ports.Len(), len(protocolConfigs)) diff --git a/config_center/configurator/override.go b/config_center/configurator/override.go index ec4e606e0d..7f5abb6175 100644 --- a/config_center/configurator/override.go +++ b/config_center/configurator/override.go @@ -23,7 +23,6 @@ import ( import ( gxset "github.com/dubbogo/gost/container/set" - gxnet "github.com/dubbogo/gost/net" ) import ( @@ -61,7 +60,7 @@ func (c *overrideConfigurator) Configure(url *common.URL) { currentSide := url.GetParam(constant.SIDE_KEY, "") configuratorSide := c.configuratorUrl.GetParam(constant.SIDE_KEY, "") if currentSide == configuratorSide && common.DubboRole[common.CONSUMER] == currentSide && c.configuratorUrl.Port == "0" { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() c.configureIfMatch(localIP, url) } else if currentSide == configuratorSide && common.DubboRole[common.PROVIDER] == currentSide && c.configuratorUrl.Port == url.Port { c.configureIfMatch(url.Ip, url) @@ -127,7 +126,7 @@ func (c *overrideConfigurator) configureDeprecated(url *common.URL) { // 1.If it is a consumer ip address, the intention is to control a specific consumer instance, it must takes effect at the consumer side, any provider received this override url should ignore; // 2.If the ip is 0.0.0.0, this override url can be used on consumer, and also can be used on provider if url.GetParam(constant.SIDE_KEY, "") == common.DubboRole[common.CONSUMER] { - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() c.configureIfMatch(localIP, url) } else { c.configureIfMatch(constant.ANYHOST_VALUE, url) diff --git a/registry/base_registry.go b/registry/base_registry.go index e7d38df067..fe61ab173e 100644 --- a/registry/base_registry.go +++ b/registry/base_registry.go @@ -29,7 +29,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" ) @@ -53,7 +52,7 @@ var ( func init() { processID = fmt.Sprintf("%d", os.Getpid()) - localIP, _ = gxnet.GetLocalIP() + localIP = common.GetLocalIp() } type createPathFunc func(dubboPath string) error diff --git a/registry/consul/utils.go b/registry/consul/utils.go index 05ac5e76e2..f5babf69d5 100644 --- a/registry/consul/utils.go +++ b/registry/consul/utils.go @@ -25,7 +25,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" consul "github.com/hashicorp/consul/api" perrors "github.com/pkg/errors" ) @@ -47,7 +46,7 @@ func buildService(url common.URL) (*consul.AgentServiceRegistration, error) { // address if url.Ip == "" { - url.Ip, _ = gxnet.GetLocalIP() + url.Ip = common.GetLocalIp() } // port diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 5d4a890c6e..6f9c4fc889 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -25,7 +25,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -365,7 +364,7 @@ func (dir *RegistryDirectory) overrideUrl(targetUrl *common.URL) { func (dir *RegistryDirectory) getConsumerUrl(c *common.URL) *common.URL { processID := fmt.Sprintf("%d", os.Getpid()) - localIP, _ := gxnet.GetLocalIP() + localIP := common.GetLocalIp() params := url.Values{} c.RangeParams(func(key, value string) bool { diff --git a/registry/kubernetes/registry.go b/registry/kubernetes/registry.go index 8889585568..c1e559e48d 100644 --- a/registry/kubernetes/registry.go +++ b/registry/kubernetes/registry.go @@ -27,7 +27,6 @@ import ( import ( "github.com/apache/dubbo-getty" - "github.com/dubbogo/gost/net" perrors "github.com/pkg/errors" v1 "k8s.io/api/core/v1" ) @@ -54,7 +53,7 @@ const ( func init() { processID = fmt.Sprintf("%d", os.Getpid()) - localIP, _ = gxnet.GetLocalIP() + localIP = common.GetLocalIp() extension.SetRegistry(Name, newKubernetesRegistry) } diff --git a/registry/nacos/registry.go b/registry/nacos/registry.go index 757474455b..51428864fb 100644 --- a/registry/nacos/registry.go +++ b/registry/nacos/registry.go @@ -26,7 +26,6 @@ import ( ) import ( - gxnet "github.com/dubbogo/gost/net" "github.com/nacos-group/nacos-sdk-go/clients" "github.com/nacos-group/nacos-sdk-go/clients/naming_client" nacosConstant "github.com/nacos-group/nacos-sdk-go/common/constant" @@ -52,7 +51,7 @@ const ( ) func init() { - localIP, _ = gxnet.GetLocalIP() + localIP = common.GetLocalIp() extension.SetRegistry(constant.NACOS_KEY, newNacosRegistry) }