From dabfdedb25dd18cd102416524df1c3741608a435 Mon Sep 17 00:00:00 2001 From: renzhiyuan <renzhiyuan@wecash.net> Date: Tue, 6 Aug 2019 22:26:04 +0800 Subject: [PATCH] add GetBackupUrls method --- cluster/router_chain.go | 32 ++++++++++++++++---------------- common/constant/key.go | 1 + common/url.go | 22 ++++++++++++++++++++++ registry/directory/directory.go | 9 +++++++-- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/cluster/router_chain.go b/cluster/router_chain.go index 8d7cab21a..d1b876d0c 100644 --- a/cluster/router_chain.go +++ b/cluster/router_chain.go @@ -2,7 +2,6 @@ package cluster import ( "github.com/apache/dubbo-go/common" - "github.com/apache/dubbo-go/common/extension" "github.com/apache/dubbo-go/protocol" "sort" ) @@ -14,21 +13,22 @@ type RouterChain struct { } func NewRouterChain(url common.URL) *RouterChain { - var builtinRouters []Router - factories := extension.GetRouterFactorys() - for _, factory := range factories { - router, _ := factory.Router(&url) - builtinRouters = append(builtinRouters, router) - } - var routers []Router - copy(routers, builtinRouters) - sort.Slice(routers, func(i, j int) bool { - return routers[i].Priority() < routers[j].Priority() - }) - return &RouterChain{ - builtinRouters: routers, - routers: routers, - } + //var builtinRouters []Router + //factories := extension.GetRouterFactories() + //for _, factory := range factories { + // router, _ := factory.Router(&url) + // builtinRouters = append(builtinRouters, router) + //} + //var routers []Router + //copy(routers, builtinRouters) + //sort.Slice(routers, func(i, j int) bool { + // return routers[i].Priority() < routers[j].Priority() + //}) + //return &RouterChain{ + // builtinRouters: routers, + // routers: routers, + //} + return nil } func (r RouterChain) AddRouters(routers []Router) { diff --git a/common/constant/key.go b/common/constant/key.go index af7700af2..f2e9c222d 100644 --- a/common/constant/key.go +++ b/common/constant/key.go @@ -74,6 +74,7 @@ const ( METHOD_KEYS = "methods" RULE_KEY = "rule" RUNTIME_KEY = "runtime" + BACKUP_KEY = "backup" ) const ( diff --git a/common/url.go b/common/url.go index 2c82082b9..7c38f8869 100644 --- a/common/url.go +++ b/common/url.go @@ -242,6 +242,28 @@ func (c URL) Key() string { //return c.ServiceKey() } +//todo +func (c URL) GetBackupUrls() []URL { + var urls []URL + var host string + urls = append(urls, c) + backups := strings.Split(c.GetParam(constant.BACKUP_KEY, ""), "") + for _, address := range backups { + index := strings.LastIndex(address, ":") + port := c.Port + if index > 0 { + host = address[:index] + port = address[index+1:] + } else { + host = string(append([]byte(host), []byte(port)...)) + } + //todo + newURL, _ := NewURL(c.ctx, address) + urls = append(urls, newURL) + } + return urls +} + func (c URL) ServiceKey() string { intf := c.GetParam(constant.INTERFACE_KEY, strings.TrimPrefix(c.Path, "/")) if intf == "" { diff --git a/registry/directory/directory.go b/registry/directory/directory.go index fd4f6bec5..b2421d7ec 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -19,8 +19,6 @@ package directory import ( "fmt" - "github.com/apache/dubbo-go/common/utils" - "github.com/apache/dubbo-go/version" "reflect" "sync" "time" @@ -37,10 +35,12 @@ 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/common/utils" "github.com/apache/dubbo-go/protocol" "github.com/apache/dubbo-go/protocol/protocolwrapper" "github.com/apache/dubbo-go/registry" "github.com/apache/dubbo-go/remoting" + "github.com/apache/dubbo-go/version" ) const ( @@ -131,7 +131,11 @@ func (dir *registryDirectory) update(res *registry.ServiceEvent) { logger.Debugf("registry update, result{%s}", res) logger.Debugf("update service name: %s!", res.Service) + //todo + _ = dir.GetUrl() + if len(dir.Routers()) > 0 { + } dir.refreshInvokers(res) } @@ -228,6 +232,7 @@ func (dir *registryDirectory) List(invocation protocol.Invocation) ([]protocol.I localRouters := dir.Routers() if len(localRouters) > 0 { for _, router := range localRouters { + //todo nil error if reflect.ValueOf(router.Url()).IsNil() || router.Url().GetParamBool(constant.RUNTIME_KEY, false) { invokers = router.Route(invokers, *dir.ConsumerUrl, invocation) } -- GitLab