瀏覽代碼

Update command handler demo and code refinement

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 年之前
父節點
當前提交
ed5ecd8580
共有 6 個文件被更改,包括 38 次插入85 次删除
  1. +1
    -16
      sentinel-demo/sentinel-demo-command-handler/pom.xml
  2. +15
    -8
      sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/CommandDemo.java
  3. +12
    -10
      sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/EchoCommandHandler.java
  4. +0
    -48
      sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/MainController.java
  5. +6
    -1
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/annotation/CommandMapping.java
  6. +4
    -2
      sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java

+ 1
- 16
sentinel-demo/sentinel-demo-command-handler/pom.xml 查看文件

@@ -5,31 +5,16 @@
<parent>
<artifactId>sentinel-demo</artifactId>
<groupId>com.alibaba.csp</groupId>
<version>1.4.2-SNAPSHOT</version>
<version>1.5.0-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>

sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/Application.java → 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 {
* <p>To run this demo, we need to add the {@code sentinel-transport-simple-http} dependency.</p>
* <p>
* 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.
* </p>
*
* @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");
}
}

+ 12
- 10
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.
*
* <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>
* <li>1. Create a class which implements the {@link CommandHandler} SPI interface</li>
* <li>2. Use a {@link CommandMapping} to specify the url and desc of your CommandHandler</li>
* <li>3. Implement your own {@code handle} method </li>
* <li>4. Add your CommandHandler in {@code com.alibaba.csp.sentinel.command.CommandHandler} file which is stored in
* {@code resources/META-INF/services/} directory </li>
* </ul>
*
* @author houyi
**/
@CommandMapping(name = "echo", desc = "echo service")
*/
@CommandMapping(name = "echo", desc = "echo command for demo")
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){
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);
}

}

+ 0
- 48
sentinel-demo/sentinel-demo-command-handler/src/main/java/com/alibaba/csp/sentinel/demo/commandhandler/MainController.java 查看文件

@@ -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;
}

}

+ 6
- 1
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();

}

+ 4
- 2
sentinel-transport/sentinel-transport-common/src/main/java/com/alibaba/csp/sentinel/command/handler/ApiCommandHandler.java 查看文件

@@ -28,10 +28,12 @@ import java.util.Map;
/**
* <p>
* List all available command handlers by request: </br>
* {@code curl http://localhost:8719/api}
* {@code curl http://ip:commandPort/api}
* </p>
*
* @author houyi
**/
* @since 1.5.0
*/
@CommandMapping(name = "api", desc = "get all available command handlers")
public class ApiCommandHandler implements CommandHandler<String> {



Loading…
取消
儲存