Skip to content

Commit

Permalink
Merge pull request #1066 from yakecanlee/feature/fix-zk-configurators
Browse files Browse the repository at this point in the history
Fix: fix zk listener func pathToKey
  • Loading branch information
AlexStocks committed Mar 11, 2021
2 parents fab8f84 + dbba5ee commit 7037558
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
17 changes: 16 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,22 @@ make test
</table>
</div>

如果想加入到社区微信群,可以先添加社区负责人 于雨 的微信 AlexanderStocks 。添加微信之前,请先给 dubbo-go 点 star 作为对项目的支持,添加好友时请报上 github ID 以进行验证。
dubbogo 社区已经开通微信公众号 "dubbogo大区",可在微信搜索 "dubbogo大区" 或者扫描如下二维码关注,可通过公众号私信留言加入 dubbogo 微信社区。

<div>
<table>
<tbody>
<tr></tr>
<tr>
<td align="center" valign="middle">
<img width="80px" height="115px" src="./doc/pic/misc/dubbogo-wechat.png">
</a>
</td>
</tr>
<tr></tr>
</tbody>
</table>
</div>

作为一个维护已经帮助构建了经受多家大型微服务系统的社区,我们足以为现有的成绩感到自豪。社区欢迎能提出建设性意见者,只知索取者和喷子请绕行。

Expand Down
10 changes: 9 additions & 1 deletion config_center/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

import (
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/remoting"
)
Expand Down Expand Up @@ -77,5 +78,12 @@ func (l *CacheListener) DataChange(event remoting.Event) bool {
}

func (l *CacheListener) pathToKey(path string) string {
return strings.Replace(strings.Replace(path, l.rootPath+"/", "", -1), "/", ".", -1)
key := strings.Replace(strings.Replace(path, l.rootPath+"/", "", -1), "/", ".", -1)
if strings.HasSuffix(key, constant.CONFIGURATORS_SUFFIX) ||
strings.HasSuffix(key, constant.TagRouterRuleSuffix) ||
strings.HasSuffix(key, constant.ConditionRouterRuleSuffix) {
//governance config, so we remove the "dubbo." prefix
return key[strings.Index(key, ".")+1:]
}
return key
}
101 changes: 101 additions & 0 deletions config_center/zookeeper/listener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 zookeeper

import (
"path"
"strconv"
"testing"
)

import (
"github.com/dubbogo/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/config_center/parser"
)

func initZkDynamicConfiguration(t *testing.T) (*zk.TestCluster, *zookeeperDynamicConfiguration) {
ts, err := zk.StartTestCluster(1, nil, nil)
assert.NoError(t, err)
assert.NotNil(t, ts.Servers[0])
urlString := "registry://127.0.0.1:" + strconv.Itoa(ts.Servers[0].Port)
regurl, err := common.NewURL(urlString)
assert.NoError(t, err)
regurl.AddParam(constant.REGISTRY_TIMEOUT_KEY, "15s")
zkFactory := &zookeeperDynamicConfigurationFactory{}
reg, err := zkFactory.GetDynamicConfiguration(regurl)
zreg, ok := reg.(*zookeeperDynamicConfiguration)
assert.True(t, ok)
assert.NoError(t, err)
assert.True(t, zreg.IsAvailable())
assert.Equal(t, zreg.GetUrl(), regurl)
assert.True(t, zreg.RestartCallBack())
zreg.SetParser(&parser.DefaultConfigurationParser{})

data := `
dubbo.application.name=dubbogo
`
err = zreg.client.Create(path.Join(zreg.rootPath, dubboPropertyFileName))
assert.NoError(t, err)
_, err = zreg.client.Conn.Set(path.Join(zreg.rootPath, dubboPropertyFileName), []byte(data), 0)
assert.NoError(t, err)

return ts, zreg
}

func TestZookeeperDynamicConfigurationPathToKey(t *testing.T) {
ts, reg := initZkDynamicConfiguration(t)
defer func() {
err := ts.Stop()
assert.NoError(t, err)
}()
listener := &mockDataListener{}
key := path.Join("dubbogoDemo" + constant.CONFIGURATORS_SUFFIX)
reg.AddListener(key, listener)
listener.wg.Add(1)

data := `
scope: application
key: dubbogoDemo
enabled: true
configs:
- addresses: [0.0.0.0:20880]
side: provider
parameters:
weight: 60
- addresses: [0.0.0.0:20881]
side: provider
parameters:
weight: 40
`
zkPath := path.Join(reg.rootPath, "dubbo", key)
exists, _, err := reg.client.Conn.Exists(zkPath)
assert.NoError(t, err)
if !exists {
err = reg.client.Create(zkPath)
assert.NoError(t, err)
}
_, err = reg.client.SetContent(zkPath, []byte(data), 0)
assert.NoError(t, err)
listener.wg.Wait()
assert.Equal(t, key, listener.event)
}
Binary file added doc/pic/misc/dubbogo-wechat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7037558

Please sign in to comment.