diff --git a/cluster/directory/base_directory.go b/cluster/directory/base_directory.go index c64ac3e12e83dfb6e446b7eb05be15425c2d9516..0fadb0983eb26cf228cfbfa1c4217e8b48f5e7d3 100644 --- a/cluster/directory/base_directory.go +++ b/cluster/directory/base_directory.go @@ -23,6 +23,7 @@ import ( import ( "github.com/dubbogo/gost/container/set" + perrors "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -33,6 +34,7 @@ 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() @@ -131,3 +133,17 @@ 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/router/chain/chain.go b/cluster/router/chain/chain.go index 26d52ecf5a2259dfbb6a68b2ebf5b2fd7b8c4785..8358bdffcbbb5617767604132204778ee6cffd57 100644 --- a/cluster/router/chain/chain.go +++ b/cluster/router/chain/chain.go @@ -49,7 +49,7 @@ type RouterChain struct { } // Route Loop routers in RouterChain and call Route method to determine the target invokers list. -func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker { +func (c *RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker { finalInvokers := invoker l := len(c.routers) rs := make([]router.Router, l, int(math.Ceil(float64(l)*1.2))) @@ -67,7 +67,7 @@ func (c RouterChain) Route(invoker []protocol.Invoker, url *common.URL, invocati // New a array add builtinRouters which is not sorted in RouterChain and routers // Sort the array // Replace router array in RouterChain -func (c RouterChain) AddRouters(routers []router.Router) { +func (c *RouterChain) AddRouters(routers []router.Router) { newRouters := make([]router.Router, 0, len(c.builtinRouters)+len(routers)) newRouters = append(newRouters, c.builtinRouters...) newRouters = append(newRouters, routers...) diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 83c46c42740a12368421dc703dc7ed1db8f70ad4..7a89b9e836651f8da35af40c39965723d8aba965 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -29,7 +29,6 @@ import ( import ( "github.com/apache/dubbo-go/cluster/directory" - "github.com/apache/dubbo-go/cluster/router/chain" "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" @@ -178,34 +177,19 @@ func (dir *registryDirectory) toGroupInvokers() []protocol.Invoker { //len is 1 it means no group setting ,so do not need cluster again for _, invokers := range groupInvokersMap { groupInvokersList = invokers - dir.buildRouterChain(invokers) } } else { for _, invokers := range groupInvokersMap { staticDir := directory.NewStaticDirectory(invokers) cluster := extension.GetCluster(dir.GetUrl().SubURL.GetParam(constant.CLUSTER_KEY, constant.DEFAULT_CLUSTER)) + staticDir.BuildRouterChain(invokers) groupInvokersList = append(groupInvokersList, cluster.Join(staticDir)) - - dir.buildRouterChain(invokers) } } return groupInvokersList } -func (dir *registryDirectory) 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 -} - func (dir *registryDirectory) uncacheInvoker(url *common.URL) { logger.Debugf("service will be deleted in cache invokers: invokers key is %s!", url.Key()) dir.cacheInvokersMap.Delete(url.Key())