22
23 flowRules.add(flowRule);
24
25 //加载配置好的规则
26 FlowRuleManager.loadRules(flowRules);
27 }
28
29 /**
30 *频繁请求接口http://localhost:8080/helloSentinelV1
31 *这种做法的缺点:
32 *1)业务侵入性很大,需要在你的controoler中写入非业务代码..
33 *2)配置不灵活若需要添加新的受保护资源需要手动添加init方法来添加流控规则
34 *@return
35 */
36 @RequestMapping("/helloSentinelV1")
37 publicStringtestHelloSentinelV1(){
38
39 Entryentity=null;
40 //关联受保护的资源
41 try{
42 entity=SphU.entry("helloSentinelV1");
43 //开始执行自己的业务方法
44 busiService.doBusi();
45 //结束执行自己的业务方法
46 }catch(BlockExceptione){
47 log.info("testHelloSentinelV1方法被流控了");
48 return"testHelloSentinelV1方法被流控了";
49 }finally{
50 if(entity!=null){
51 entity.exit();
52 }
53 }
54 return"OK";
55 }
56
57 }
测试效果:http://localhost:8080/helloSentinelV1
v1版本的缺陷:如下
业务侵入性很大,需要在你的controoler中写入非业务代码.
配置不灵活若需要添加新的受保护资源需要手动添加init方法来添加流控规则