diff --git a/cluster/directory/base_directory.go b/cluster/directory/base_directory.go index e93efbac7c62e267937152f7ba798a35018b6dd6..0e79cc40112c48288c7eb549d0fc5532cf0926e6 100644 --- a/cluster/directory/base_directory.go +++ b/cluster/directory/base_directory.go @@ -49,6 +49,10 @@ func (dir *BaseDirectory) RouterChain() router.Chain { return dir.routerChain } +func (dir *BaseDirectory) SetRouterChain(routerChain router.Chain) { + dir.routerChain = routerChain +} + func GetRouterURLSet() *gxset.HashSet { return routerURLSet } diff --git a/cluster/router/chain/factory.go b/cluster/router/chain/factory.go deleted file mode 100644 index 1f24e6df7d3e1615960638a0d021a772229096f3..0000000000000000000000000000000000000000 --- a/cluster/router/chain/factory.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 chain - -import ( - "github.com/apache/dubbo-go/cluster/router" - "github.com/apache/dubbo-go/common" - "github.com/apache/dubbo-go/common/extension" -) - -func init() { - extension.SetRouterChainsFactory("chain", NewRouterChainFactory) -} - -type RouterChainFactory struct{} - -func (c RouterChainFactory) Router(url *common.URL) (router.Chain, error) { - return NewRouterChain(url) -} - -func NewRouterChainFactory() router.ChainFactory { - return RouterChainFactory{} -} diff --git a/cluster/router/router.go b/cluster/router/router.go index 6c833cab8483e9a1168a2f63112001060d71e723..48c88a95d259b0b1a8e7355f6641d16bfda715b3 100644 --- a/cluster/router/router.go +++ b/cluster/router/router.go @@ -29,11 +29,6 @@ type RouterFactory interface { Router(*common.URL) (Router, error) } -// ChainFactory Extension - Router Chain -type ChainFactory interface { - Router(*common.URL) (Chain, error) -} - type Router interface { Route([]protocol.Invoker, *common.URL, protocol.Invocation) []protocol.Invoker Priority() int64 diff --git a/common/extension/router_factory.go b/common/extension/router_factory.go index e6a1cec1ab8f52a612a686546281dc46d242c36b..8f916339e2d1ad059d58a82825026d3bd6ad68a9 100644 --- a/common/extension/router_factory.go +++ b/common/extension/router_factory.go @@ -22,8 +22,7 @@ import ( ) var ( - routers = make(map[string]func() router.RouterFactory) - routerChains = make(map[string]func() router.ChainFactory) + routers = make(map[string]func() router.RouterFactory) ) func SetRouterFactory(name string, fun func() router.RouterFactory) { @@ -37,17 +36,6 @@ func GetRouterFactory(name string) router.RouterFactory { return routers[name]() } -func SetRouterChainsFactory(name string, fun func() router.ChainFactory) { - routerChains[name] = fun -} - -func GetRouterChainsFactory(name string) router.ChainFactory { - if routers[name] == nil { - panic("router_chain_factory for " + name + " is not existing, make sure you have import the package.") - } - return routerChains[name]() -} - func GetRouters() map[string]func() router.RouterFactory { return routers } diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 73adf2d92ebcf21f133ca1f71c9e90b7f592f13e..2001cb163be575df41cb54babb05c41a723369ba 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -31,6 +31,7 @@ import ( import ( "github.com/apache/dubbo-go/cluster/directory" "github.com/apache/dubbo-go/cluster/router" + "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" @@ -180,7 +181,7 @@ func toRouters(urls []*common.URL) []router.Router { continue } url.Protocol = routerKey - factory := extension.GetRouterFactory(url.GetParam(constant.ROUTER_KEY, "condition")) + factory := extension.GetRouterFactory(url.GetParam(constant.ROUTER_KEY, routerKey)) router, e := factory.Router(url) if e != nil { logger.Error("factory.Router(url){%s} , error : %s", url, e) @@ -219,18 +220,34 @@ 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)) 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())