tianyun 2 rokov pred
rodič
commit
c70af80f54

+ 16 - 0
springboot-main/pom.xml

@@ -54,6 +54,22 @@
             <artifactId>sentinel-core</artifactId>
             <version>1.8.1</version>
         </dependency>
+        <dependency>
+            <groupId>com.alibaba.csp</groupId>
+            <artifactId>sentinel-transport-simple-http</artifactId>
+            <version>1.8.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba.csp</groupId>
+            <artifactId>sentinel-web-servlet</artifactId>
+            <version>1.8.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 18 - 34
springboot-main/src/main/java/com/alvin/Application.java

@@ -1,46 +1,30 @@
 package com.alvin;
 
 
-import com.alibaba.csp.sentinel.Entry;
-import com.alibaba.csp.sentinel.SphU;
-import com.alibaba.csp.sentinel.slots.block.BlockException;
-import com.alibaba.csp.sentinel.slots.block.RuleConstant;
-import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
-import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
+import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
-import java.util.ArrayList;
-import java.util.List;
 
 @SpringBootApplication
 public class Application {
     public static void main(String[] args) throws Exception {
-        //SpringApplication.run(Application.class);
-        // 配置规则.
-        initFlowRules();
+        SpringApplication.run(Application.class);
 
-        while (true) {
-            // 1.5.0 版本开始可以直接利用 try-with-resources 特性,自动 exit entry
-            try (Entry entry = SphU.entry("HelloWorld")) {
-                // 被保护的逻辑
-                System.out.println("hello world");
-            } catch (BlockException ex) {
-                // 处理被流控的逻辑
-                //System.out.println("blocked!");
-            }
-        }
-    }
-    private static void initFlowRules(){
-        List<FlowRule> rules = new ArrayList<>();
-        FlowRule rule = new FlowRule();
-        //绑定资源
-        rule.setResource("HelloWorld");
-        //限流阈值类型
-        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
-        //数量级别
-        rule.setCount(20);
-        //添加到本地内存
-        rules.add(rule);
-        FlowRuleManager.loadRules(rules);
     }
+    //@Bean
+    //public FilterRegistrationBean sentinelFilterRegistration() {
+    //    FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
+    //    registration.setFilter(new CommonFilter());
+    //    registration.addUrlPatterns("/*");
+    //    registration.setName("sentinelFilter");
+    //    registration.setOrder(1);
+    //
+    //    return registration;
+    //}
+    //
+    //@RequestMapping("/index")
+    //public String index(){
+    //    return "hello index";
+    //}
+
 }

+ 10 - 0
springboot-main/src/main/java/com/alvin/ISentinelService.java

@@ -0,0 +1,10 @@
+package com.alvin;
+
+/**
+ * @author Milo Lee
+ * @date 2021-03-23 11:34
+ */
+public interface ISentinelService {
+
+    String hello (long s);
+}

+ 23 - 0
springboot-main/src/main/java/com/alvin/SentinelController.java

@@ -0,0 +1,23 @@
+package com.alvin;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 注解 @SentinelResource学习
+ * @author Milo Lee
+ * @date 2021-03-23 11:33
+ */
+@RestController
+public class SentinelController {
+
+    @Autowired
+    private ISentinelService service;
+
+    @GetMapping(value = "/hello/{s}")
+    public String apiHello(@PathVariable long s) {
+        return service.hello(s);
+    }
+}

+ 45 - 0
springboot-main/src/main/java/com/alvin/SentinelServiceImpl.java

@@ -0,0 +1,45 @@
+package com.alvin;
+
+import com.alibaba.csp.sentinel.annotation.SentinelResource;
+import com.alibaba.csp.sentinel.slots.block.BlockException;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author Milo Lee
+ * @date 2021-03-23 11:34
+ */
+@Service
+@Slf4j
+public class SentinelServiceImpl implements ISentinelService {
+
+
+    /**
+     *Sentinel 提供了 @SentinelResource 注解用于定义资源
+     * @param s
+     * @return
+     */
+    @Override
+    //value:资源名称,必需项(不能为空)
+    //blockHandler 对应处理 BlockException 的函数名称
+    //fallback  用于在抛出异常的时候提供 fallback 处理逻辑
+    @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
+    public String hello(long s) {
+        log.error("hello:{}",s);
+        return String.format("Hello at %d", s);
+    }
+
+    // Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
+    public String helloFallback(long s) {
+        log.error("helloFallback:{}",s);
+        return String.format("Halooooo %d", s);
+    }
+
+    // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
+    public String exceptionHandler(long s, BlockException ex) {
+        // Do some log here.
+        log.error("exceptionHandler:{}",s);
+        ex.printStackTrace();
+        return "Oops, error occurred at " + s;
+    }
+}

+ 1 - 1
springboot-main/src/main/java/com/alvin/FunUtil.java → springboot-main/src/main/java/com/alvin/tmp/FunUtil.java

@@ -1,4 +1,4 @@
-package com.alvin;
+package com.alvin.tmp;
 
 import java.util.concurrent.*;
 import java.util.concurrent.atomic.AtomicInteger;

+ 2 - 2
springboot-main/src/main/java/com/alvin/Tmp.java → springboot-main/src/main/java/com/alvin/tmp/Tmp.java

@@ -1,6 +1,6 @@
-package com.alvin;
+package com.alvin.tmp;
 
-import static com.alvin.FunUtil.exec;
+import static com.alvin.tmp.FunUtil.exec;
 
 public class Tmp {
     public static int method(int a) {