首页 > 其他分享 >sentinel

sentinel

时间:2022-08-18 01:33:07浏览次数:43  
标签: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 [email protected]
 * @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
From: https://www.cnblogs.com/gendway/p/16597398.html

相关文章

  • 解决ENVI 5.4打开Sentinel-2A数据出错问题
    之前我们发布了《ENVI5.3/5.3.1打开Sentinel-2数据出错解决方法》。由于较新的Sentinel-2数据更改了命名方式,即便安装了上述博文中的修复补丁,在ENVI53中打开新命名的Sent......
  • ENVI 5.3/5.3.1打开Sentinel-2数据出错解决方法
    从ENVI5.3版本开始支持Sentinel-2数据的打开。但是,在打开时会遇到一些问题。总结如下:问题1:路径太长ESA/SciHub最初发布的Sentinel-2数据文件名太长,特别是数据所在路径较......
  • sentinel 资源定义方式
    主流框架的适配dubbo<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-dubbo-adapter</artifactId><version>x.y.z</version></d......
  • Sentinel源码分析-滑动窗口统计原理
    滑动窗口技术是Sentinel比较关键的核心技术,主要用于数据统计通过分析StatisticSlot来慢慢引出这个概念@Overridepublicvoidentry(Contextcontext,ResourceWrap......
  • Sentinel 源码分析- 熔断降级原理分析
    直接从Sentinel源码demoExceptionRatioCircuitBreakerDemo看起直接看他的main函数publicstaticvoidmain(String[]args)throwsException{initDegradeRu......
  • Sentinel 源码分析-限流原理
    1.gitclonesenetinel源码到本地,切换到release1.8分支2.找到FlowQpsDemo.java,根据sentinel自带的案例来学习sentinel的原理3.先看main方法publicstaticvoid......