Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when it need local ip, it will get it every time. We can get local ip once, and reused it. #807

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cluster/cluster_impl/base_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/cluster_impl/failover_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{
Expand Down
5 changes: 2 additions & 3 deletions cluster/directory/base_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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")
Expand All @@ -79,7 +78,7 @@ func TestIsProperRouter(t *testing.T) {
regURL := url
regURL.AddParam(constant.APPLICATION_KEY, "mock-app")
d := NewBaseDirectory(&regURL)
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")
Expand Down
21 changes: 10 additions & 11 deletions cluster/router/condition/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

import (
"github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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"))
Expand All @@ -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{}
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions cluster/router/condition/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions common/host_util.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions common/host_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetLocalIp(t *testing.T) {
assert.NotNil(t, GetLocalIp())
}
3 changes: 1 addition & 2 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand Down Expand Up @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions config/service_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
})
Expand All @@ -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))
Expand Down
5 changes: 2 additions & 3 deletions config_center/configurator/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

import (
gxset "github.com/dubbogo/gost/container/set"
gxnet "github.com/dubbogo/gost/net"
)

import (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions registry/base_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions registry/consul/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
consul "github.com/hashicorp/consul/api"
perrors "github.com/pkg/errors"
)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions registry/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions registry/kubernetes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -54,7 +53,7 @@ const (

func init() {
processID = fmt.Sprintf("%d", os.Getpid())
localIP, _ = gxnet.GetLocalIP()
localIP = common.GetLocalIp()
extension.SetRegistry(Name, newKubernetesRegistry)
}

Expand Down
3 changes: 1 addition & 2 deletions registry/nacos/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -52,7 +51,7 @@ const (
)

func init() {
localIP, _ = gxnet.GetLocalIP()
localIP = common.GetLocalIp()
extension.SetRegistry(constant.NACOS_KEY, newNacosRegistry)
}

Expand Down