-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dashboard: Add ZooKeeper flow rule dynamic publisher sample (#714)
- Loading branch information
Showing
5 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...est/java/com/alibaba/csp/sentinel/dashboard/rule/zookeeper/FlowRuleZookeeperProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.csp.sentinel.dashboard.rule.zookeeper; | ||
|
||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; | ||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; | ||
import com.alibaba.csp.sentinel.datasource.Converter; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Component("flowRuleZookeeperPublisher") | ||
public class FlowRuleZookeeperProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { | ||
|
||
@Autowired | ||
private CuratorFramework zkClient; | ||
@Autowired | ||
private Converter<String, List<FlowRuleEntity>> converter; | ||
|
||
@Override | ||
public List<FlowRuleEntity> getRules(String appName) throws Exception { | ||
String zkPath = ZookeeperConfigUtil.getPath(appName); | ||
byte[] bytes = zkClient.getData().forPath(zkPath); | ||
if (null == bytes || bytes.length == 0) { | ||
return new ArrayList<>(); | ||
} | ||
String s = new String(bytes); | ||
|
||
return converter.convert(s); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...st/java/com/alibaba/csp/sentinel/dashboard/rule/zookeeper/FlowRuleZookeeperPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.csp.sentinel.dashboard.rule.zookeeper; | ||
|
||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; | ||
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; | ||
import com.alibaba.csp.sentinel.datasource.Converter; | ||
import com.alibaba.csp.sentinel.util.AssertUtil; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.apache.zookeeper.data.Stat; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.List; | ||
|
||
@Component("flowRuleZookeeperPublisher") | ||
public class FlowRuleZookeeperPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { | ||
@Autowired | ||
private CuratorFramework zkClient; | ||
@Autowired | ||
private Converter<List<FlowRuleEntity>, String> converter; | ||
|
||
@Override | ||
public void publish(String app, List<FlowRuleEntity> rules) throws Exception { | ||
AssertUtil.notEmpty(app, "app name cannot be empty"); | ||
|
||
String path = ZookeeperConfigUtil.getPath(app); | ||
Stat stat = zkClient.checkExists().forPath(path); | ||
if (stat == null) { | ||
zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); | ||
} | ||
byte[] data = CollectionUtils.isEmpty(rules) ? "".getBytes() : converter.convert(rules).getBytes(); | ||
zkClient.setData().forPath(path, data); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...oard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/zookeeper/ZookeeperConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.csp.sentinel.dashboard.rule.zookeeper; | ||
|
||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; | ||
import com.alibaba.csp.sentinel.datasource.Converter; | ||
import com.alibaba.fastjson.JSON; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class ZookeeperConfig { | ||
|
||
@Bean | ||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { | ||
return JSON::toJSONString; | ||
} | ||
|
||
@Bean | ||
public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() { | ||
return s -> JSON.parseArray(s, FlowRuleEntity.class); | ||
} | ||
|
||
@Bean | ||
public CuratorFramework zkClient() { | ||
CuratorFramework zkClient = | ||
CuratorFrameworkFactory.newClient("127.0.0.1:2181", | ||
new ExponentialBackoffRetry(ZookeeperConfigUtil.SLEEP_TIME, ZookeeperConfigUtil.RETRY_TIMES)); | ||
zkClient.start(); | ||
|
||
return zkClient; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../src/test/java/com/alibaba/csp/sentinel/dashboard/rule/zookeeper/ZookeeperConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed 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 com.alibaba.csp.sentinel.dashboard.rule.zookeeper; | ||
|
||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
public class ZookeeperConfigUtil { | ||
public static final String RULE_ROOT_PATH = "/sentinel_rule_config"; | ||
|
||
public static final int RETRY_TIMES = 3; | ||
public static final int SLEEP_TIME = 1000; | ||
|
||
public static String getPath(String appName) { | ||
StringBuilder stringBuilder = new StringBuilder(RULE_ROOT_PATH); | ||
|
||
if (StringUtils.isBlank(appName)) { | ||
return stringBuilder.toString(); | ||
} | ||
if (appName.startsWith("/")) { | ||
stringBuilder.append(appName); | ||
} else { | ||
stringBuilder.append("/") | ||
.append(appName); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |