标签:return RequestMapping id sentinel import com public
package com.tuling.mall.sentineldemo.controller; import com.alibaba.csp.sentinel.Entry; import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.annotation.SentinelResource; 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 lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; @RestController @Slf4j public class HelloController { private static final String RESOURCE_NAME = "HelloWorld"; @RequestMapping(value = "/hello") public String hello() { try (Entry entry = SphU.entry(RESOURCE_NAME)) { // 被保护的逻辑 log.info("hello world"); return "hello world"; } catch (BlockException ex) { // 处理被流控的逻辑 log.info("blocked!"); return "被流控了"; } } /** * 定义流控规则 */ @PostConstruct private static void initFlowRules(){ List<FlowRule> rules = new ArrayList<>(); FlowRule rule = new FlowRule(); //设置受保护的资源 rule.setResource(RESOURCE_NAME); // 设置流控规则 QPS rule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 设置受保护的资源阈值 rule.setCount(1); rules.add(rule); // 加载配置好的规则 FlowRuleManager.loadRules(rules); } @SentinelResource(value = RESOURCE_NAME, blockHandler = "handleException", fallback = "fallbackException") @RequestMapping("/hello2") public String hello2() { int i = 1 / 0; return "helloworld"; } public String handleException(BlockException ex){ return "被流控了"; } public String fallbackException(Throwable t){ return "被异常降级了"; } }
package com.tuling.mall.sentineldemo.controller; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.tuling.mall.sentineldemo.entity.UserEntity; import com.tuling.mall.sentineldemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.atomic.AtomicInteger; /** * @author Fox */ @RestController public class LimitFlowController { @RequestMapping("/test") public String test() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return "========test()========"; } AtomicInteger atomicInteger = new AtomicInteger(); @RequestMapping("/test2") public String test2() { atomicInteger.getAndIncrement(); if (atomicInteger.get() % 2 == 0){ //模拟异常和异常比率 int i = 1/0; } return "========test2()========"; } public String handleException(BlockException exception) { return "===被限流降级啦==="; } @Autowired private UserService userService; @RequestMapping(value = "/test3") public UserEntity test3() { UserEntity user = userService.getById(1); return user; } @RequestMapping(value = "/test4") public UserEntity test4() { UserEntity user = userService.getById(1); return user; } }
package com.tuling.mall.sentineldemo.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.tuling.mall.sentineldemo.entity.UserEntity; import com.tuling.mall.sentineldemo.feign.OrderFeignService; import com.tuling.mall.sentineldemo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tuling.common.utils.R; /** * * * @author fox * @email 2763800211@qq.com * @date 2021-01-28 15:53:24 */ @RestController @RequestMapping(value = "/user") public class UserController { @Autowired UserService userService; @Autowired OrderFeignService orderFeignService; @RequestMapping(value = "/findOrderByUserId/{id}") // @SentinelResource(value = "findOrderByUserId", // blockHandler = "handleException") public R findOrderByUserId(@PathVariable("id") Integer id) { // try { // // 模拟测试并发线程数限流 // Thread.sleep(900); // } catch (InterruptedException e) { // e.printStackTrace(); // } //feign调用 R result = orderFeignService.findOrderByUserId(id); return result; } public R handleException(@PathVariable("id") Integer id,BlockException exception){ return R.error(-1,"===被限流降级啦==="); } public R fallback(@PathVariable("id") Integer id,Throwable e){ return R.error(-1,"===被熔断降级啦==="+e.getMessage()); } @RequestMapping("/info/{id}") @SentinelResource(value = "userinfo", blockHandler = "handleException") public R info(@PathVariable("id") Integer id){ UserEntity user = userService.getById(id); if(id==4){ throw new IllegalArgumentException("异常参数"); } return R.ok().put("user", user); } }
SentinelResourceaspect
标签:return,RequestMapping,id,sentinel,import,com,public 来源: https://www.cnblogs.com/gendway/p/16597398.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。