Просмотр исходного кода

feat:测试Sentinel热点规则的代码

yang yi 1 год назад
Родитель
Сommit
6300298195

+ 36 - 0
member-service-provider_10000/src/main/java/anyi/space/memberServer/controller/SentinelTestController.java

@@ -1,9 +1,16 @@
 package anyi.space.memberServer.controller;
 
+import com.alibaba.csp.sentinel.annotation.SentinelResource;
+import com.alibaba.csp.sentinel.slots.block.BlockException;
+import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * @ProjectName: distributedSystemLearn
  * @FileName: SentinelTestController
@@ -74,4 +81,33 @@ public class SentinelTestController {
         System.out.println(msg);
         return msg;
     }
+
+    /**
+     * news为测试Sentinel热点规则的接口
+     * @param id
+     * @param type
+     * @return
+     */
+    @GetMapping("/news")
+    //使用@SentinelResource注解标识一个资源,value为资源名称,blockHandler为流控规则触发后的处理方法
+    @SentinelResource(value = "news",blockHandler = "newsBlockHandler")
+    public String news(Integer id,String type){
+        String msg = "id为%d类型为%s的news被访问".formatted(id, type);
+        System.out.println(msg);
+        return msg;
+    }
+
+    /**
+     * newsBlockHandler为news接口流控规则触发后的处理方法
+     * @param id
+     * @param type
+     * @param e
+     * @return
+     */
+    public String newsBlockHandler(Integer  id, String type, BlockException e){
+        String msg = "id为%d类型为%s的news被限流".formatted(id, type);
+        System.out.println(msg);
+        e.printStackTrace();
+        return msg;
+    }
 }