当前位置: 首页 > news >正文

Sentinel降级操作

1.通过对feign调用的降级 如果访问失败,则返回另外的信息

正常的feign调用

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

    /**
     * 写给商品服务的接口,查询秒杀服务信息
     * @param skuId
     * @return
     */
    @GetMapping("/currentSeckillSkus/sku/seckill/{skuId}")
    R getSkuSeckillInfo(@PathVariable("skuId") Long skuId);
}

兜底方法返回另外的信息

/**
 * 如果feign调用失败返回的信息
 */
@Component
public class SeckillFeignServiceFallback implements SeckillFeignService {
    @Override
    public R getSkuSeckillInfo(Long skuId) {
        return R.error(500,方法调用失败);
    }
}

2.对代码进行访问控制,在可视化工具中对seckillSkus进行数据设置

@Override
    public List<SecKillSkuRedisTo> getCurrentSeckillSkus() {
        //1.确定当前时间属于哪个秒杀场次
        long time = new Date().getTime();
        try(Entry seckillSkus = SphU.entry("seckillSkus")){
            //正常的业务代码-----
            }
            //然后在可视化工具中对这个代码进行限流控制设置
        }catch (BlockException e){
            System.out.println("资源被限流");
        }
        return null;
    }

3.统一config配置返回降级信息

**
 * 自定义流量控制请求失败后返回信息
 */
@Configuration
public class SeckillSentinelConfig {

    public SeckillSentinelConfig(){
        WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
            @Override
            public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
                R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMsg());
                httpServletResponse.setCharacterEncoding("UTF-8");
                httpServletResponse.setContentType("application/json");
                httpServletResponse.getWriter().write(JSON.toJSONString(error));
            }
        });
    }
}

4.使用注解结合可视化操作进行控制

 public List<SecKillSkuRedisTo> blockHandler(BlockException e){
        log.error("getCurrentSeckillSkus被限流了");
        return null;
    }

    /**
     * 返回当前时间段可以参加秒杀的商品信息
     * Entry seckillSkus = SphU.entry("seckillSkus"对代码进行降级
     * @return
     */
    @SentinelResource(value = "getCurrentSeckillSkus",blockHandler = "blockHandler")
    @Override
    public List<SecKillSkuRedisTo> getCurrentSeckillSkus() {
       
       
        return null;
    }

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

value的名称是可视化页面的限流处理名称

blockHandler的内容是限流降级后的操作方法

两个方法返回类型要一致

5.整合sentine和gateway

引入依赖

<!--        sentinel和gateway整合-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

版本需要和common中的阿里依赖一致

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

config文件

@Configuration
public class SentinelGatewayConfig {
    public SentinelGatewayConfig(){
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMsg());
                String jsonString = JSON.toJSONString(error);
                Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just(jsonString), String.class);
                return body;
            }
        });
    }
}

也可以配置相关的信息

配置网关返回的状态码
spring.cloud.sentinel.scg.fallback.response-status=400

相关文章:

  • 数据预处理技术之数据归一化
  • k8s中netty服务器容器tcp连接数量优化
  • Sentinel-1 扩展时序注释数据集 (ETAD)的查询和下载
  • Android 基于Fragment的权限封装
  • ASP.NET Core NE8实现HTTP Upgrade和HTTP CONNECT代理服务器
  • php/js:实现几秒后进行页面跳转
  • 【软考中级】3天擦线过软考中级-软件设计师
  • 使用vscode查bug
  • 几款提高开发效率的Idea 插件
  • 力扣题目训练(1)
  • c++ class总结
  • 微认证 openEuler社区开源贡献实践
  • 头歌C语言递归函数、嵌套函数
  • SpringBoot整合ElasticSearch实现分页查询
  • 【Python】01快速上手爬虫案例一
  • JVM-初始JVM
  • cmd_to_robot 讨论及 G29 控制优化
  • hcip----ospf
  • 2023年总结我所经历的技术大变革
  • 探索无尽可能性:C++深度优先搜索算法解析
  • 中山网站制作专业/搜索引擎排名优化程序
  • 外贸网站如何做外链/百度手机点击排名工具
  • 网络营销上的网站建设流程/爱站网站
  • 小地方做b2b网站/湖北网络推广有限公司
  • 公众号链接网站都是怎么做的/alexa
  • 创建一个网站的步骤是/网站优化策略分析