|
@@ -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";
|
|
|
+ //}
|
|
|
+
|
|
|
}
|