diff --git a/sentinel-demo/sentinel-demo-command-handler/pom.xml b/sentinel-demo/sentinel-demo-command-handler/pom.xml index bb0cf7af..83aefe46 100644 --- a/sentinel-demo/sentinel-demo-command-handler/pom.xml +++ b/sentinel-demo/sentinel-demo-command-handler/pom.xml @@ -5,31 +5,16 @@ sentinel-demo com.alibaba.csp - 1.4.2-SNAPSHOT + 1.5.0-SNAPSHOT 4.0.0 sentinel-demo-command-handler - - com.alibaba.csp - sentinel-cluster-client-default - 1.4.2-SNAPSHOT - - - com.alibaba.csp - sentinel-cluster-server-default - 1.4.2-SNAPSHOT - com.alibaba.csp sentinel-transport-simple-http - - org.springframework.boot - spring-boot-starter-web - 1.5.17.RELEASE - \ No newline at end of file diff --git a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/Application.java b/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/CommandDemo.java similarity index 53% rename from sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/Application.java rename to sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/CommandDemo.java index 614c9157..48a45733 100644 --- a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/Application.java +++ b/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/CommandDemo.java @@ -15,17 +15,24 @@ */ package com.alibaba.csp.sentinel.demo.commandhandler; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import com.alibaba.csp.sentinel.init.InitExecutor; /** - * @author houyi - **/ -@SpringBootApplication(scanBasePackages = {"com.alibaba.csp.sentinel"}) -public class Application { + *

To run this demo, we need to add the {@code sentinel-transport-simple-http} dependency.

+ *

+ * As soon as the CommandCenter has been initialized, we can visit {@code http://ip:commandPort/api} + * to see all available command APIs (by default the port is 8719). + * We can also visit our customized {@code /echo} command. + *

+ * + * @author Eric Zhao + */ +public class CommandDemo { public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + // Only for demo. You don't have to do this in your application. + InitExecutor.doInit(); + System.out.println("Sentinel CommandCenter has been initialized"); + } } diff --git a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/EchoCommandHandler.java b/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/EchoCommandHandler.java index 717f5623..406d30cb 100644 --- a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/EchoCommandHandler.java +++ b/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/EchoCommandHandler.java @@ -21,26 +21,28 @@ 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 + * This class is a demo shows how to create and register a customized CommandHandler. + * * + * * @author houyi - **/ -@CommandMapping(name = "echo", desc = "echo service") + */ +@CommandMapping(name = "echo", desc = "echo command for demo") public class EchoCommandHandler implements CommandHandler { @Override public CommandResponse handle(CommandRequest request) { String name = request.getParam("name"); - if(name==null || name.trim().length()==0){ + 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); + return CommandResponse.ofSuccess("Hello: " + name); } } diff --git a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/MainController.java b/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/MainController.java deleted file mode 100644 index 3dfa7e1c..00000000 --- a/sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/MainController.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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; - } - -} diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/annotation/CommandMapping.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/annotation/CommandMapping.java index b698da6b..11a466c5 100755 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/annotation/CommandMapping.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/annotation/CommandMapping.java @@ -27,6 +27,11 @@ public @interface CommandMapping { String name(); + /** + * Get brief description of the command. + * + * @return brief description of the command + * @since 1.5.0 + */ String desc(); - } diff --git a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java index b8db7594..c915eed8 100644 --- a/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java +++ b/sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java @@ -28,10 +28,12 @@ import java.util.Map; /** *

* List all available command handlers by request:
- * {@code curl http://localhost:8719/api} + * {@code curl http://ip:commandPort/api} *

+ * * @author houyi - **/ + * @since 1.5.0 + */ @CommandMapping(name = "api", desc = "get all available command handlers") public class ApiCommandHandler implements CommandHandler {