Sentinel personaliza el proceso de degradación de los recursos protegidos en el proveedor

Personalice los recursos protegidos

  1. trata de atraparlo()
try(Entry entry = SphU.entry("seckillSkus")) {
      ///业务逻辑
    }

2) Basado en anotaciones

  @SentinelResource(value = "getCurrentSeckillSkusResource", blockHandler = "blockHandler")

1 y 2 deben configurar la
URL de retorno predeterminada después de haber sido restringida , y la solicitud se puede configurar para que regrese
de manera unificada . Agregar en application.yml

#开启feign sentinel 熔断保护
feign:
  sentinel:
    enabled: true

Escribe los recursos que quieres proteger

public List<SeckillSkuRedisTo> blockHandler(BlockException e) {
    
    

        log.info("getCurrentSeckillSkusResource资源被限流了.");
        return null;
    }
/**
     * 返回当前时间可以参与的秒杀商品信息
     * 被保护资源 try (Entry entry = SphU.entry("seckillSkus")) {
     * 被限流了就调用blockHandler = "blockHandler"方法
     * fallback = ""针对异常的处理
     */
    //定义被保护的资源
    @SentinelResource(value = "getCurrentSeckillSkusResource", blockHandler = "blockHandler")
    @Override
    public List<SeckillSkuRedisTo> getCurrentSeckillSkus() {
    
    

        //1 确定当前时间属于哪个秒杀场次
        //当前时间
        long now = new Date().getTime();
        try (Entry entry = SphU.entry("seckillSkus")) {
    
    
            Set<String> keys = redisTemplate.keys(SESSION_CACHE_PREFIX + "*");
            for (String key : keys) {
    
    
                //seckill:sessions:1613757600000_1613761200000
                String replace = key.replace(SESSION_CACHE_PREFIX, "");
                String[] s = replace.split("_");
                long start = Long.parseLong(s[0]);
                long end = Long.parseLong(s[1]);
                if (start <= now && now <= end) {
    
    
                    //说明在当前场次的时间区间内
                    List<String> row = redisTemplate.opsForList().range(key, -100, 100);
                    //已修改 BoundHashOperations<String, Object, Object> hashOps = r
                    BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(SKUKILL_CACHE_PREFIX);
                    List<String> list = hashOps.multiGet(row);
                    if (list != null) {
    
    
                        List<SeckillSkuRedisTo> collect = list.stream().map((item) -> {
    
    
                            SeckillSkuRedisTo to = JSON.parseObject((String) item, SeckillSkuRedisTo.class);
                            //                        to.setRandomCode(null);//当前秒杀开始就需要随机码
                            return to;
                        }).collect(Collectors.toList());
                        return collect;
                    }
                    break;
                }
            }
        } catch (BlockException e) {
    
    
            log.error("自定义被保护资源被限流-{}", e.getMessage());
        }
        return null;
    }

Inserte la descripción de la imagen aquí
Siempre que la restricción de control de flujo se realice aquí, se activará log.info en el método
blockHandler ("el recurso getCurrentSeckillSkusResource está restringido");

Supongo que te gusta

Origin blog.csdn.net/u014496893/article/details/114378571
Recomendado
Clasificación