Sentinel流控

自定义流控响应

@Configuration
public class SentinelConfig {
    public SentinelConfig(){
        WebCallbackManager.setUrlBlockHandler((request,response,ex)->{
            R error = R.error(BizCodeEnum.TO_MANY_REQUEST);
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            response.getWriter().write(JSON.toJSONString(error));
        });
    }
}
public enum BizCodeEnum {
    TO_MANY_REQUEST(80000,"太多请求");

    private int code;
    private String msg;

    private BizCodeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

image-20201209142649561

熔断降级

@FeignClient(value = "gulimall-seckill", fallback = SeckillFeignServiceFallBack.class)
public interface SeckillFeignService {

    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId);
}

@Slf4j
@Component
public class SeckillFeignServiceFallBack implements SeckillFeignService {
    @Override
    public R getSkuSeckillInfo(Long skuId) {
        log.info("熔断方法调用....");
        return R.error(BizCodeEnum.TO_MANY_REQUEST);
    }
}

自定义受保护资源

摘自SphU.java源码

   public void foo() {
      Entry entry = null;
      try {
         entry = SphU.entry("abc");
         // resource that need protection
      } catch (BlockException blockException) {
          // when goes there, it is blocked
          // add blocked handle logic here
      } catch (Throwable bizException) {
         // business exception
          Tracer.trace(bizException);
      } finally {
          // ensure finally be executed
          if (entry != null){
             entry.exit();
          }
      }
   }

注解方式定义资源:(fallback会针对所有类型的异常,blockHandler会在原方法被限流/降级/系统保护时调用)


@SentinelResourcce(value="resource",blockHandler="blockHandler")
public R doService(){
    
}

public R blockHandler(BlockException e){
    
}

网关流控

引入和gateway整合的依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

配置文件yml

spring:
  cloud:
    sentinel:
     transport:
      dashboard: localhost:8080
      port: 8719

启动sentinel web项目,进入控制台

我这里用的sentinel-dashboard-1.8.0.jar,低版本的可能控制台跟我的不一样。

image-20201209190616397

新增流控规则:这里的routeid就是配置文件中的route id,想流控哪个服务就流控哪个服务。

image-20201209190749152

image-20201209190702429

此外还可以对针对请求属性进行流控

image-20201209191230388

sentinel定制网关流控返回

@Configuration
public class SentinelGatewayConfig {
    public SentinelGatewayConfig(){
        GatewayCallbackManager.setBlockHandler((serverWebExchange, throwable) -> {
            R error = R.error(BizCodeEnum.TO_MANY_REQUEST);
            String errJson = JSON.toJSONString(error);
            return ServerResponse.ok().body(Mono.just(errJson),String.class);
        });
    }
}

流控效果:

image-20201209193023720

原文地址:https://www.cnblogs.com/wwjj4811/p/14110491.html