diff --git a/cluster/directory/base_directory.go b/cluster/directory/base_directory.go index 0fadb0983eb26cf228cfbfa1c4217e8b48f5e7d3..75d9ef26567df0fbd83f5d9f94c8548d1e8e633d 100644 --- a/cluster/directory/base_directory.go +++ b/cluster/directory/base_directory.go @@ -23,7 +23,6 @@ import ( import ( "github.com/dubbogo/gost/container/set" - perrors "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -34,7 +33,6 @@ import ( "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/common/logger" - "github.com/apache/dubbo-go/protocol" ) var routerURLSet = gxset.NewSet() @@ -48,6 +46,15 @@ type BaseDirectory struct { routerChain router.Chain } +// NewBaseDirectory Create BaseDirectory with URL +func NewBaseDirectory(url *common.URL) BaseDirectory { + return BaseDirectory{ + url: url, + destroyed: atomic.NewBool(false), + routerChain: &chain.RouterChain{}, + } +} + // RouterChain Return router chain in directory func (dir *BaseDirectory) RouterChain() router.Chain { return dir.routerChain @@ -60,15 +67,6 @@ func (dir *BaseDirectory) SetRouterChain(routerChain router.Chain) { dir.routerChain = routerChain } -// NewBaseDirectory Create BaseDirectory with URL -func NewBaseDirectory(url *common.URL) BaseDirectory { - return BaseDirectory{ - url: url, - destroyed: atomic.NewBool(false), - routerChain: &chain.RouterChain{}, - } -} - // GetUrl Get URL func (dir *BaseDirectory) GetUrl() common.URL { return *dir.url @@ -133,17 +131,3 @@ func GetRouterURLSet() *gxset.HashSet { func AddRouterURLSet(url *common.URL) { routerURLSet.Add(url) } - -// BuildRouterChain build router chain by invokers -func (dir *staticDirectory) BuildRouterChain(invokers []protocol.Invoker) error { - if len(invokers) == 0 { - return perrors.Errorf("invokers == null") - } - url := invokers[0].GetUrl() - routerChain, e := chain.NewRouterChain(&url) - if e != nil { - return e - } - dir.SetRouterChain(routerChain) - return nil -} diff --git a/cluster/directory/base_directory_test.go b/cluster/directory/base_directory_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d5993959f1d37f343a612e2bee305461d49535d0 --- /dev/null +++ b/cluster/directory/base_directory_test.go @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package directory + +import ( + "encoding/base64" + "fmt" + "testing" +) + +import ( + gxnet "github.com/dubbogo/gost/net" + "github.com/stretchr/testify/assert" +) + +import ( + _ "github.com/apache/dubbo-go/cluster/router/condition" + "github.com/apache/dubbo-go/common" + "github.com/apache/dubbo-go/common/constant" +) + +func TestNewBaseDirectory(t *testing.T) { + url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider")) + directory := NewBaseDirectory(&url) + + assert.NotNil(t, directory) + + assert.Equal(t, url, directory.GetUrl()) + assert.Equal(t, &url, directory.GetDirectoryUrl()) + +} + +func TestBuildRouterChain(t *testing.T) { + url, _ := common.NewURL(fmt.Sprintf("dubbo://192.168.1.1:20000/com.ikurento.user.UserProvider")) + directory := NewBaseDirectory(&url) + + assert.NotNil(t, directory) + + localIP, _ := gxnet.GetLocalIP() + rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP)) + routeURL := getRouteUrl(rule) + routerURLs := make([]*common.URL, 0) + routerURLs = append(routerURLs, routeURL) + directory.SetRouters(routerURLs) + chain := directory.RouterChain() + + assert.NotNil(t, chain) +} + +func getRouteUrl(rule string) *common.URL { + url, _ := common.NewURL("condition://0.0.0.0/com.foo.BarService") + url.AddParam("rule", rule) + url.AddParam("force", "true") + url.AddParam(constant.ROUTER_KEY, "router") + return &url +} diff --git a/cluster/directory/static_directory.go b/cluster/directory/static_directory.go index 032859a278b938025668cad408f1297f2c2f0b02..9f600fedc40cf29a40abca6c11652935f20473b4 100644 --- a/cluster/directory/static_directory.go +++ b/cluster/directory/static_directory.go @@ -18,6 +18,11 @@ package directory import ( + perrors "github.com/pkg/errors" +) + +import ( + "github.com/apache/dubbo-go/cluster/router/chain" "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/protocol" ) @@ -76,3 +81,17 @@ func (dir *staticDirectory) Destroy() { dir.invokers = []protocol.Invoker{} }) } + +// BuildRouterChain build router chain by invokers +func (dir *staticDirectory) BuildRouterChain(invokers []protocol.Invoker) error { + if len(invokers) == 0 { + return perrors.Errorf("invokers == null") + } + url := invokers[0].GetUrl() + routerChain, e := chain.NewRouterChain(&url) + if e != nil { + return e + } + dir.SetRouterChain(routerChain) + return nil +}