sentinel
作者:互联网
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