Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: Feature/application and service level router #662

Merged
Next Next commit
add scope and key support for condition router
  • Loading branch information
william feng committed Jul 18, 2020
commit 468187409db242ef44e7015bac2c8ae63fb0a710
5 changes: 4 additions & 1 deletion cluster/router/condition/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func (f *FileConditionRouter) URL() common.URL {
common.WithParamsValue(constant.RouterPriority, strconv.Itoa(routerRule.Priority)),
common.WithParamsValue(constant.RULE_KEY, base64.URLEncoding.EncodeToString([]byte(rule))),
common.WithParamsValue(constant.ROUTER_KEY, constant.CONDITION_ROUTE_PROTOCOL),
common.WithParamsValue(constant.CATEGORY_KEY, constant.ROUTERS_CATEGORY))
common.WithParamsValue(constant.CATEGORY_KEY, constant.ROUTERS_CATEGORY),
common.WithParamsValue(constant.RouterRuleKey, routerRule.Key),
common.WithParamsValue(constant.RouterScope, routerRule.Scope),
)
})
return f.url
}
Expand Down
5 changes: 3 additions & 2 deletions cluster/router/condition/router_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (
)

import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/cluster/router"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/yaml"
)

// RouterRule RouterRule config read from config file or config center
type RouterRule struct {
router.BaseRouterRule `yaml:",inline""`
router.BaseRouterRule `yaml:",inline"`
Conditions []string
}

Expand All @@ -57,7 +58,7 @@ func getRule(rawRule string) (*RouterRule, error) {
return r, err
}
r.RawRule = rawRule
if len(r.Conditions) != 0 {
if len(r.Conditions) != 0 && r.Key != "" && (r.Scope == constant.RouterApplicationScope || r.Scope == constant.RouterServiceScope){
r.Valid = true
}

Expand Down
26 changes: 24 additions & 2 deletions cluster/router/condition/router_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
func TestGetRule(t *testing.T) {
testyml := `
scope: application
key: test-provider
runtime: true
force: false
conditions:
Expand All @@ -50,10 +51,31 @@ conditions:
assert.True(t, rule.Runtime)
assert.Equal(t, false, rule.Force)
assert.Equal(t, testyml, rule.RawRule)
assert.True(t, true, rule.Valid)
assert.True(t, rule.Valid)
assert.Equal(t, false, rule.Enabled)
assert.Equal(t, false, rule.Dynamic)
assert.Equal(t, "", rule.Key)
assert.Equal(t, "test-provider", rule.Key)

testyml = `
key: test-provider
runtime: true
force: false
conditions:
- >
method!=sayHello =>`
rule, e = getRule(testyml)
assert.Nil(t, e)
assert.False(t, rule.Valid)

testyml = `
scope: noApplication
key: test-provider
conditions:
- >
method!=sayHello =>`
rule, e = getRule(testyml)
assert.Nil(t, e)
assert.False(t, rule.Valid)
}

func TestIsMatchGlobPattern(t *testing.T) {
Expand Down
60 changes: 60 additions & 0 deletions cluster/router/condition/router_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 condition

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

import (
"github.com/dubbogo/gost/container/set"
)

func TestParseRule(t *testing.T) {
testString := ``
matchPair, err := parseRule(testString)
assert.Nil(t, err)
assert.EqualValues(t, matchPair, make(map[string]MatchPair, 16))

testString = `method!=sayHello&application=sayGoodBye`
matchPair, err = parseRule(testString)
assert.Nil(t, err)
assert.EqualValues(t, matchPair["method"].Mismatches, gxset.NewSet("sayHello"))
assert.EqualValues(t, matchPair["application"].Matches, gxset.NewSet("sayGoodBye"))

testString = `noRule`
matchPair, err = parseRule(testString)
assert.Nil(t, err)
assert.EqualValues(t, matchPair["noRule"].Mismatches, gxset.NewSet())
assert.EqualValues(t, matchPair["noRule"].Matches, gxset.NewSet())

testString = `method!=sayHello,sayGoodBye`
matchPair, err = parseRule(testString)
assert.Nil(t, err)
assert.EqualValues(t, matchPair["method"].Mismatches, gxset.NewSet(`sayHello`, `sayGoodBye`))

testString = `method!=sayHello,sayGoodDay=sayGoodBye`
matchPair, err = parseRule(testString)
assert.Nil(t, err)
assert.EqualValues(t, matchPair["method"].Mismatches, gxset.NewSet(`sayHello`, `sayGoodDay`))
assert.EqualValues(t, matchPair["method"].Matches, gxset.NewSet(`sayGoodBye`))
}
2 changes: 1 addition & 1 deletion cluster/router/tag/router_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// RouterRule RouterRule config read from config file or config center
type RouterRule struct {
router.BaseRouterRule `yaml:",inline""`
router.BaseRouterRule `yaml:",inline"`
}

func getRule(rawRule string) (*RouterRule, error) {
Expand Down
9 changes: 8 additions & 1 deletion common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ const (
RouterEnabled = "enabled"
// Priority Priority key in router module
RouterPriority = "priority"

// RouterScope Scope key in router module
RouterScope = "scope"
// RouterApplicationScope Scope key in router module
RouterApplicationScope = "application"
// RouterServiceScope Scope key in router module
RouterServiceScope = "service"
// RouterRuleKey defines the key of the router, service's/application's name
RouterRuleKey = "key"
// ForceUseTag is the tag in attachment
ForceUseTag = "dubbo.force.tag"
Tagkey = "dubbo.tag"
Expand Down