Skip to content

Commit 9a9c24a

Browse files
AetheranceAetherance
andauthored
fix(router): use ApplicationKey instead of Tagkey to build config key in Route method (#3251)
* fix(tag-router): use ApplicationKey instead of Tagkey to build config key in Route method * add unit tests --------- Co-authored-by: Aetherance <inkToAether@gmail.com>
1 parent ba17613 commit 9a9c24a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cluster/router/tag/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func (p *PriorityRouter) Route(invokers []base.Invoker, url *common.URL, invocat
5252
logger.Warnf("[tag router] invokers from previous router is empty")
5353
return invokers
5454
}
55-
// get prefix from invoker
56-
application := invokers[0].GetURL().GetParam(constant.Tagkey, "")
55+
// get application name from invoker to look up tag routing config
56+
application := invokers[0].GetURL().GetParam(constant.ApplicationKey, "")
5757
key := strings.Join([]string{application, constant.TagRouterRuleSuffix}, "")
5858
value, ok := p.routerConfigs.Load(key)
5959
if !ok {

cluster/router/tag/router_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ func TestRouter(t *testing.T) {
334334
result := p.Route(invokerList, url3, invocation.NewRPCInvocation("GetUser", nil, nil))
335335
assert.Len(t, result, 3)
336336
})
337+
338+
t.Run("dynamicTag_withApplicationKey", func(t *testing.T) { //NOSONAR
339+
p, _ := NewTagPriorityRouter()
340+
ivk1 := base.NewBaseInvoker(url1)
341+
ivk1.GetURL().SetParam(constant.ApplicationKey, "test-app") //NOSONAR
342+
ivk2 := base.NewBaseInvoker(url2)
343+
ivk2.GetURL().SetParam(constant.ApplicationKey, "test-app") //NOSONAR
344+
345+
configKey := "test-app" + constant.TagRouterRuleSuffix //NOSONAR
346+
p.routerConfigs.Store(configKey, global.RouterConfig{
347+
Enabled: truePointer,
348+
Valid: truePointer,
349+
Tags: []global.Tag{{
350+
Addresses: []string{"192.168.0.1:20000"}, //NOSONAR
351+
}},
352+
})
353+
354+
result := p.Route([]base.Invoker{ivk1, ivk2}, consumerUrl, invocation.NewRPCInvocation("GetUser", nil, nil))
355+
assert.Len(t, result, 1)
356+
})
337357
}
338358

339359
func TestNotify(t *testing.T) {

0 commit comments

Comments
 (0)