diff --git a/common/extension/router_factory.go b/common/extension/router_factory.go index d88c26ff827145f22214aa14645f782d22a80b45..6f27aafaebf87147116e74272cc229657f436201 100644 --- a/common/extension/router_factory.go +++ b/common/extension/router_factory.go @@ -36,12 +36,3 @@ func GetRouterFactory(name string) cluster.RouterFactory { return routers[name]() } -func GetRouterFactories() []cluster.RouterFactory { - var factorys []cluster.RouterFactory - - for _, value := range routers { - factorys = append(factorys, value()) - } - return factorys - -} diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 044e4ea4da21d0aab6805a3cb992639c593e33de..8af9f582a08919bb7578a00fc64f8c8bde99d5b5 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -18,7 +18,9 @@ package directory import ( + gxset "github.com/dubbogo/gost/container/set" "reflect" + "strings" "sync" "time" ) @@ -127,23 +129,23 @@ func (dir *registryDirectory) refreshInvokers(res *registry.ServiceEvent) { } switch res.Action { case remoting.EventTypeAdd: - //dirUrl := &res.Service - //var urls []*common.URL - // - //for _, v := range dirUrl.GetBackupUrls() { - // p := v.Protocol - // category := v.GetParam(constant.CATEGORY_KEY, constant.PROVIDERS_CATEGORY) - // if strings.EqualFold(category, constant.ROUTERS_CATEGORY) || strings.EqualFold(constant.ROUTE_PROTOCOL, p) { - // urls = append(urls, v) - // } - //} - // - //if len(urls) > 0 { - // routers := toRouters(urls) - // if len(routers) > 0 { - // dir.SetRouters(routers) - // } - //} + dirUrl := &res.Service + var urls []*common.URL + + for _, v := range dirUrl.GetBackupUrls() { + p := v.Protocol + category := v.GetParam(constant.CATEGORY_KEY, constant.PROVIDERS_CATEGORY) + if strings.EqualFold(category, constant.ROUTERS_CATEGORY) || strings.EqualFold(constant.ROUTE_PROTOCOL, p) { + urls = append(urls, v) + } + } + + if len(urls) > 0 { + routers := toRouters(urls) + if len(routers) > 0 { + dir.SetRouters(routers) + } + } //dir.cacheService.EventTypeAdd(res.Path, dir.serviceTTL) dir.cacheInvoker(url) @@ -163,7 +165,34 @@ func (dir *registryDirectory) refreshInvokers(res *registry.ServiceEvent) { } func toRouters(urls []*common.URL) []cluster.Router { - return nil + if urls == nil || len(urls) == 0 { + return nil + } + + routerMap := gxset.NewSet() + for _, url := range urls { + if url.Protocol == constant.EMPTY_PROTOCOL { + continue + } + routerKey := url.GetParam(constant.ROUTER_KEY, "") + if routerKey == "" { + continue + } + url.Protocol = routerKey + factory := extension.GetRouterFactory(url.GetParam(constant.ROUTER_KEY, "condition")) + router, e := factory.Router(url) + if e != nil { + logger.Error("factory.Router(url){%s} , error : %s", url, e) + } + routerMap.Add(router) + } + + routers := make([]cluster.Router, 0) + for _, v := range routerMap.Values() { + routers = append(routers, v.(cluster.Router)) + } + + return routers } func (dir *registryDirectory) toGroupInvokers() []protocol.Invoker { diff --git a/registry/directory/directory_test.go b/registry/directory/directory_test.go index b3c1d35aaa66b3437ff89807fba2df0a383921cb..ef22c537a0ee68dc59b3e2088d6fa350da41596d 100644 --- a/registry/directory/directory_test.go +++ b/registry/directory/directory_test.go @@ -19,6 +19,8 @@ package directory import ( "context" + "encoding/base64" + gxnet "github.com/dubbogo/gost/net" "net/url" "strconv" "testing" @@ -31,6 +33,7 @@ import ( import ( "github.com/apache/dubbo-go/cluster/cluster_impl" + _ "github.com/apache/dubbo-go/cluster/router" "github.com/apache/dubbo-go/common" "github.com/apache/dubbo-go/common/constant" "github.com/apache/dubbo-go/common/extension" @@ -201,3 +204,20 @@ func normalRegistryDir(noMockEvent ...bool) (*registryDirectory, *registry.MockR } return registryDirectory, mockRegistry.(*registry.MockRegistry) } + +func TestToRouter(t *testing.T) { + localIP, _ := gxnet.GetLocalIP() + rule := base64.URLEncoding.EncodeToString([]byte("host = " + localIP + " => " + " host = 10.20.3.3")) + url, _ := common.NewURL( + context.TODO(), + "dubbo://0.0.0.0/com.foo.BarService", + common.WithParamsValue(constant.RULE_KEY, rule), + common.WithParamsValue(constant.ROUTER_KEY, "condition"), + ) + urls := make([]*common.URL, 0) + urls = append(urls, &url) + routers := toRouters(urls) + assert.Equal(t, 1, len(routers)) + router := routers[0] + assert.Equal(t, "condition", router.Url().Protocol) +}