Add an ApiCommandHandler to list all available commands and description (#491)

* Add a ApiCommandHandler which fetch all registered CommandHandlers so that user can get all available CommandHandlers by request /api
* Add a customized CommandHandler demo
This commit is contained in:
逅弈 2019-03-01 13:39:50 +08:00 committed by Eric Zhao
parent 13de41ba6f
commit 59d923ebc1
40 changed files with 259 additions and 37 deletions

View File

@ -29,7 +29,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/client/fetchConfig")
@CommandMapping(name = "cluster/client/fetchConfig", desc = "get cluster client config")
public class FetchClusterClientConfigHandler implements CommandHandler<String> {
@Override

View File

@ -33,7 +33,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/client/modifyConfig")
@CommandMapping(name = "cluster/client/modifyConfig", desc = "modify cluster client config")
public class ModifyClusterClientConfigHandler implements CommandHandler<String> {
@Override

View File

@ -27,7 +27,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/flowRules")
@CommandMapping(name = "cluster/server/flowRules", desc = "get cluster flow rules")
public class FetchClusterFlowRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -27,7 +27,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.1
*/
@CommandMapping(name = "cluster/server/metricList")
@CommandMapping(name = "cluster/server/metricList", desc = "get cluster server metrics")
public class FetchClusterMetricCommandHandler implements CommandHandler<String> {
@Override

View File

@ -27,7 +27,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/paramRules")
@CommandMapping(name = "cluster/server/paramRules", desc = "get cluster server param flow rules")
public class FetchClusterParamFlowRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -29,7 +29,7 @@ import com.alibaba.fastjson.JSONObject;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/fetchConfig")
@CommandMapping(name = "cluster/server/fetchConfig", desc = "get cluster server config")
public class FetchClusterServerConfigHandler implements CommandHandler<String> {
@Override

View File

@ -35,7 +35,7 @@ import com.alibaba.fastjson.JSONObject;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/info")
@CommandMapping(name = "cluster/server/info", desc = "get cluster server info")
public class FetchClusterServerInfoCommandHandler implements CommandHandler<String> {
@Override

View File

@ -32,7 +32,7 @@ import com.alibaba.fastjson.JSONArray;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/modifyFlowRules")
@CommandMapping(name = "cluster/server/modifyFlowRules", desc = "modify cluster flow rules")
public class ModifyClusterFlowRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -32,7 +32,7 @@ import com.alibaba.fastjson.JSONArray;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/modifyParamRules")
@CommandMapping(name = "cluster/server/modifyParamRules", desc = "modify cluster param flow rules")
public class ModifyClusterParamFlowRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -32,7 +32,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/modifyFlowConfig")
@CommandMapping(name = "cluster/server/modifyFlowConfig", desc = "modify cluster server flow config")
public class ModifyClusterServerFlowConfigHandler implements CommandHandler<String> {
@Override

View File

@ -27,7 +27,7 @@ import com.alibaba.csp.sentinel.util.StringUtil;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/modifyTransportConfig")
@CommandMapping(name = "cluster/server/modifyTransportConfig", desc = "modify cluster server transport config")
public class ModifyClusterServerTransportConfigHandler implements CommandHandler<String> {
@Override

View File

@ -33,7 +33,7 @@ import com.alibaba.fastjson.TypeReference;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "cluster/server/modifyNamespaceSet")
@CommandMapping(name = "cluster/server/modifyNamespaceSet", desc = "modify server namespace set")
public class ModifyServerNamespaceSetHandler implements CommandHandler<String> {
@Override

View File

@ -29,6 +29,7 @@
<module>sentinel-demo-parameter-flow-control</module>
<module>sentinel-demo-slot-chain-spi</module>
<module>sentinel-demo-cluster</module>
<module>sentinel-demo-command-handler</module>
</modules>
<dependencies>

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sentinel-demo</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>1.4.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-demo-command-handler</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-cluster-client-default</artifactId>
<version>1.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-cluster-server-default</artifactId>
<version>1.4.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.17.RELEASE</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
/*
* Copyright 1999-2019 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.demo.commandhandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author houyi
**/
@SpringBootApplication(scanBasePackages = {"com.alibaba.csp.sentinel"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 1999-2019 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.demo.commandhandler;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
/**
* Customized CommandHandler
* This Class is a demo shows how to create a customized CommandHandler
* <ul>
* <li>1.Create a class which implements the {@link CommandHandler} interface </li>
* <li>2.Use a {@link CommandMapping} to specify the url and desc of your CommandHandler </li>
* <li>3.Implement your own handle method </li>
* <li>4.Add your CommandHandler in com.alibaba.csp.sentinel.command.CommandHandler file which is stored in resources/META-INF/services/ </li>
* </ul>
* @author houyi
**/
@CommandMapping(name = "echo", desc = "echo service")
public class EchoCommandHandler implements CommandHandler<String> {
@Override
public CommandResponse<String> handle(CommandRequest request) {
String name = request.getParam("name");
if(name==null || name.trim().length()==0){
return CommandResponse.ofSuccess("Tell us what's your name by submit a name parameter");
}
return CommandResponse.ofSuccess("Hello: "+name);
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 1999-2019 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.demo.commandhandler;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author houyi
**/
@Controller
public class MainController {
/**
* init sentinel
* @return pass or block
*/
@GetMapping("/init")
public @ResponseBody
String init() {
String retVal;
String resource = "init";
try{
SphU.entry(resource);
retVal = "pass";
}catch (BlockException e){
retVal = "block";
}
return retVal;
}
}

View File

@ -0,0 +1 @@
com.alibaba.csp.sentinel.demo.commandhandler.EchoCommandHandler

View File

@ -30,7 +30,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 0.2.0
*/
@CommandMapping(name = "topParams")
@CommandMapping(name = "topParams", desc = "get topN param in specified resource, accept param: res={resourceName}&idx={paramIndex}&n={topN}")
public class FetchTopParamsCommandHandler implements CommandHandler<String> {
@Override

View File

@ -26,7 +26,7 @@ import com.alibaba.fastjson.JSON;
* @author Eric Zhao
* @since 0.2.0
*/
@CommandMapping(name = "getParamFlowRules")
@CommandMapping(name = "getParamFlowRules", desc = "get param flow rules")
public class GetParamFlowRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -33,7 +33,7 @@ import com.alibaba.fastjson.JSONArray;
* @author Eric Zhao
* @since 0.2.0
*/
@CommandMapping(name = "setParamFlowRules")
@CommandMapping(name = "setParamFlowRules", desc = "set param flow rules, accept param: data={paramFlowRule Json}")
public class ModifyParamFlowRulesCommandHandler implements CommandHandler<String> {
private static WritableDataSource<List<ParamFlowRule>> paramFlowWds = null;

View File

@ -15,11 +15,7 @@
*/
package com.alibaba.csp.sentinel.command.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;
/**
* @author Eric Zhao
@ -30,4 +26,7 @@ import java.lang.annotation.Target;
public @interface CommandMapping {
String name();
String desc();
}

View File

@ -0,0 +1,60 @@
/*
* Copyright 1999-2019 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.command.handler;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandHandlerProvider;
import com.alibaba.csp.sentinel.command.CommandRequest;
import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Map;
/**
* <p>
* List all available command handlers by request: </br>
* {@code curl http://localhost:8719/api}
* </p>
* @author houyi
**/
@CommandMapping(name = "api", desc = "get all available command handlers")
public class ApiCommandHandler implements CommandHandler<String> {
@Override
public CommandResponse<String> handle(CommandRequest request) {
Map<String, CommandHandler> handlers = CommandHandlerProvider.getInstance().namedHandlers();
JSONArray array = new JSONArray();
if (handlers.isEmpty()) {
return CommandResponse.ofSuccess(array.toJSONString());
}
for (CommandHandler handler : handlers.values()) {
CommandMapping commandMapping = handler.getClass().getAnnotation(CommandMapping.class);
if (commandMapping == null) {
continue;
}
String api = commandMapping.name();
String desc = commandMapping.desc();
JSONObject obj = new JSONObject();
obj.put("url", "/" + api);
obj.put("desc", desc);
array.add(obj);
}
return CommandResponse.ofSuccess(array.toJSONString());
}
}

View File

@ -26,7 +26,7 @@ import com.alibaba.csp.sentinel.util.HostNameUtil;
*
* @author Eric Zhao
*/
@CommandMapping(name = "basicInfo")
@CommandMapping(name = "basicInfo", desc = "get sentinel config info")
public class BasicInfoCommandHandler implements CommandHandler<String> {
@Override

View File

@ -28,7 +28,7 @@ import com.alibaba.fastjson.JSON;
/**
* @author jialiang.linjl
*/
@CommandMapping(name = "getRules")
@CommandMapping(name = "getRules", desc = "get all active rules by type, request param: type={ruleType}")
public class FetchActiveRuleCommandHandler implements CommandHandler<String> {
@Override

View File

@ -28,7 +28,7 @@ import com.alibaba.fastjson.JSON;
/**
* @author qinan.qn
*/
@CommandMapping(name = "clusterNodeById")
@CommandMapping(name = "clusterNodeById", desc = "get clusterNode VO by id, request param: id={resourceName}")
public class FetchClusterNodeByIdCommandHandler implements CommandHandler<String> {
@Override

View File

@ -29,7 +29,7 @@ import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
/**
* @author qinan.qn
*/
@CommandMapping(name = "cnode")
@CommandMapping(name = "cnode", desc = "get clusterNode metrics by id, request param: id={resourceName}")
public class FetchClusterNodeHumanCommandHandler implements CommandHandler<String> {
private final static String FORMAT = "%-4s%-80s%-10s%-10s%-10s%-11s%-9s%-6s%-10s%-11s%-9s%-11s";

View File

@ -32,7 +32,7 @@ import com.alibaba.fastjson.JSON;
/**
* @author leyou
*/
@CommandMapping(name = "jsonTree")
@CommandMapping(name = "jsonTree", desc = "get tree node VO start from root node")
public class FetchJsonTreeCommandHandler implements CommandHandler<String> {
@Override

View File

@ -29,7 +29,7 @@ import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
/**
* @author qinan.qn
*/
@CommandMapping(name = "origin")
@CommandMapping(name = "origin", desc = "get origin clusterNode by id, request param: id={resourceName}")
public class FetchOriginCommandHandler implements CommandHandler<String> {
private final static String FORMAT = "%-4s%-80s%-10s%-10s%-11s%-9s%-6s%-10s%-11s%-9s";

View File

@ -32,7 +32,7 @@ import com.alibaba.fastjson.JSONArray;
/**
* @author jialiang.linjl
*/
@CommandMapping(name = "clusterNode")
@CommandMapping(name = "clusterNode", desc = "get all clusterNode VO, use type=notZero to ignore those nodes with totalRequest <=0")
public class FetchSimpleClusterNodeCommandHandler implements CommandHandler<String> {
@Override

View File

@ -28,7 +28,7 @@ import com.alibaba.fastjson.JSONObject;
/**
* @author jialiang.linjl
*/
@CommandMapping(name = "systemStatus")
@CommandMapping(name = "systemStatus", desc = "get system status")
public class FetchSystemStatusCommandHandler implements CommandHandler<String> {
@Override

View File

@ -27,7 +27,7 @@ import com.alibaba.csp.sentinel.node.Node;
/**
* @author qinan.qn
*/
@CommandMapping(name = "tree")
@CommandMapping(name = "tree", desc = "get metrics in tree mode, use id to specify detailed tree root")
public class FetchTreeCommandHandler implements CommandHandler<String> {
@Override

View File

@ -41,7 +41,7 @@ import static com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry
* @author jialiang.linjl
* @author Eric Zhao
*/
@CommandMapping(name = "setRules")
@CommandMapping(name = "setRules", desc = "modify the rules, accept param: type={ruleType}&data={ruleJson}")
public class ModifyRulesCommandHandler implements CommandHandler<String> {
@Override

View File

@ -24,7 +24,7 @@ import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
/**
* @author youji.zj
*/
@CommandMapping(name = "getSwitch")
@CommandMapping(name = "getSwitch", desc = "get sentinel switch status")
public class OnOffGetCommandHandler implements CommandHandler<String> {
@Override

View File

@ -25,7 +25,7 @@ import com.alibaba.csp.sentinel.Constants;
/**
* @author youji.zj
*/
@CommandMapping(name = "setSwitch")
@CommandMapping(name = "setSwitch", desc = "set sentinel switch, accept param: value={true|false}")
public class OnOffSetCommandHandler implements CommandHandler<String> {
@Override

View File

@ -34,7 +34,7 @@ import com.alibaba.csp.sentinel.node.metric.MetricWriter;
* @author leyou
* @author Eric Zhao
*/
@CommandMapping(name = "metric")
@CommandMapping(name = "metric", desc = "get and aggregate metrics, accept param: startTime={startTime}&endTime={endTime}&maxLines={maxLines}&identify={resourceName}")
public class SendMetricCommandHandler implements CommandHandler<String> {
private MetricSearcher searcher;

View File

@ -25,7 +25,7 @@ import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
* @author jialiang.linjl
* @author Eric Zhao
*/
@CommandMapping(name = "version")
@CommandMapping(name = "version", desc = "get sentinel version")
public class VersionCommandHandler implements CommandHandler<String> {
@Override

View File

@ -28,7 +28,7 @@ import com.alibaba.fastjson.JSONObject;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "getClusterMode")
@CommandMapping(name = "getClusterMode", desc = "get cluster mode status")
public class FetchClusterModeCommandHandler implements CommandHandler<String> {
@Override

View File

@ -28,7 +28,7 @@ import com.alibaba.csp.sentinel.log.RecordLog;
* @author Eric Zhao
* @since 1.4.0
*/
@CommandMapping(name = "setClusterMode")
@CommandMapping(name = "setClusterMode", desc = "set cluster mode, accept param: mode={0|1} 0:client mode 1:server mode")
public class ModifyClusterModeCommandHandler implements CommandHandler<String> {
@Override

View File

@ -13,4 +13,5 @@ com.alibaba.csp.sentinel.command.handler.OnOffSetCommandHandler
com.alibaba.csp.sentinel.command.handler.SendMetricCommandHandler
com.alibaba.csp.sentinel.command.handler.VersionCommandHandler
com.alibaba.csp.sentinel.command.handler.cluster.FetchClusterModeCommandHandler
com.alibaba.csp.sentinel.command.handler.cluster.ModifyClusterModeCommandHandler
com.alibaba.csp.sentinel.command.handler.cluster.ModifyClusterModeCommandHandler
com.alibaba.csp.sentinel.command.handler.ApiCommandHandler