diff --git a/cluster/router/condition/file.go b/cluster/router/condition/file.go index 1eba3d69244bc38bba67333482320723df9d6bd9..d4293c9809ba5c3ff445768b682a2ca6bd41073d 100644 --- a/cluster/router/condition/file.go +++ b/cluster/router/condition/file.go @@ -76,17 +76,20 @@ func (f *FileConditionRouter) URL() common.URL { ) if routerRule.Scope == constant.RouterApplicationScope { f.url.AddParam(constant.APPLICATION_KEY, routerRule.Key) - } else { - grp, srv, ver, _ := parseServiceRouterKey(routerRule.Key) - if len(grp) > 0 { - f.url.AddParam(constant.GROUP_KEY, grp) - } - if len(ver) > 0 { - f.url.AddParam(constant.VERSION_KEY, ver) - } - if len(srv) > 0 { - f.url.AddParam(constant.INTERFACE_KEY, srv) - } + return + } + grp, srv, ver, e := parseServiceRouterKey(routerRule.Key) + if e != nil { + return + } + if len(grp) > 0 { + f.url.AddParam(constant.GROUP_KEY, grp) + } + if len(ver) > 0 { + f.url.AddParam(constant.VERSION_KEY, ver) + } + if len(srv) > 0 { + f.url.AddParam(constant.INTERFACE_KEY, srv) } }) return f.url @@ -98,9 +101,12 @@ func parseServiceRouterKey(key string) (string, string, string, error) { } reg := regexp.MustCompile(`(.*/{1})?([^:/]+)(:{1}[^:]*)?`) strs := reg.FindAllStringSubmatch(key, -1) - if strs == nil || len(strs) != 1 { + if strs == nil || len(strs) > 1 { return "", "", "", perrors.Errorf("Invalid key, service key must follow [{group}/]{service}[:{version}] pattern") } + if len(strs[0]) != 4 { + return "", "", "", perrors.Errorf("Parse service router key failed") + } grp := strings.TrimSpace(strings.TrimRight(strs[0][1], "/")) srv := strings.TrimSpace(strs[0][2]) ver := strings.TrimSpace(strings.TrimLeft(strs[0][3], ":")) diff --git a/cluster/router/condition/router_rule.go b/cluster/router/condition/router_rule.go index 576dea87f91b01cda330b99f128669fe6fd56f8f..9f2bf2d41a941398698f110f77a7a3051a0c9c11 100644 --- a/cluster/router/condition/router_rule.go +++ b/cluster/router/condition/router_rule.go @@ -58,7 +58,7 @@ func getRule(rawRule string) (*RouterRule, error) { return r, err } r.RawRule = rawRule - if len(r.Conditions) != 0 && r.Key != "" && (r.Scope == constant.RouterApplicationScope || r.Scope == constant.RouterServiceScope) { + if len(r.Conditions) > 0 && len(r.Key) > 0 && (r.Scope == constant.RouterApplicationScope || r.Scope == constant.RouterServiceScope) { r.Valid = true }