sentinel分布式系统 流控 降级

2021/7/15 23:17:40

本文主要是介绍sentinel分布式系统 流控 降级,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

        1.由于 Netflflix 中多项开源产品已进入维护阶段,不再开发新的版本,就目前来看是没有什么问题的。但是从长远角度
出发,我们还是需要考虑是否有可替代产品使用。比如本文中要介绍的 Alibaba Sentinel 就是一款高性能且轻量级
的 == 流量控制,熔断降级 == 可替换方案。
Sentinel 官网: http://github.com/alibaba/Sentinel

        2.首先在根目录下开启cmd黑窗口启用jar文件

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar xxxxx.jar

1.添加依赖

 2.书写 配置文件

3. 定义资源
资源 是 Sentinel 中的核心概念之一。我们说的资源,可以是任何东西,服务,服务里的方法,甚至是一段代码。最
常用的资源是我们代码中的 Java 方法。 Sentinel 提供了 @SentinelResource 注解用于定义资源,并提供了 AspectJ 的
扩展用于自动定义资源、处理 BlockException 等。
 

package com.chen.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.chen.entity.Product;
import com.chen.order.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/*
 * @program: springcloud-parent
 * @description:
 * @author: 陈
 * @create: 2021-07-15 15:14
 */


@Service
public class ProductServiceImpl_blank implements ProductService {

    @Autowired
    private RestTemplate restTemplate;
    @Override

    //定义资源
    @SentinelResource(value = "findById",fallback = "fallbackMethod",blockHandler = "blockHandlerMethod")
    public Product findById(Integer pid) {
        if (pid==1){
            throw new RuntimeException("pid 不能为1");
        }

        return restTemplate.getForObject("http://shop-product/product/findById/"+pid,Product.class);
    }

    //限流兜底方法。 必须与资源的方法参数一致。返回类型一致。
    public Product fallbackMethod(Integer pid,Throwable e){
        Product product=new Product();
        product.setPid(pid);
        product.setPname("服务器出现异常的兜底方法");
        return product;
    }

    //熔断降级的兜底方法 异常 或者响应超时时  BlockExecption
    public Product blockHandlerMethod(Integer pid, BlockException e){
        Product product=new Product();
        product.setPid(pid);
        product.setPname("服务熔断降级限流控制-兜底的数据");
        return product;
    }

}

测试:

 

 

 点击新增之后,继续访问页面同时点击多次就会出现熔断,这个熔断是我们在后台写的

blockHandlerMethod方法里

  降级是通常是为异常设置的,设置好降级之后,一般我们要删除流控设置的东西。要不然时间间隔效果看不出来。设置id为1的时候出现异常 

 多次访问id为1就会出现下图所示

 过了30秒之后才会成功访问

 程序运行自动设置限流或者降级

 

 RestTemplate支持 ,只要是用到RestTemplate类,sentinel会自动加到簇点链路上

 

  创建util类ExceptionUtil

package com.chen.order.util;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.chen.entity.Product;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

/**
* @program: springcloud-parent
*
* @description: Util
*
* @author: 陈
*
* @create: 2021-07-15 18:49
**/
@Component
public class ExceptionUtil {

    public static ClientHttpResponse fallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception){

        return new SentinelClientHttpResponse(JSON.toJSONString(new Product(1,"fallback",5555.0,1000)));
    }

    public static ClientHttpResponse handlerException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception){

        return new SentinelClientHttpResponse(JSON.toJSONString(new Product(1,"block",6666.0,1000)));
    }
}

在主启动类上加上注解,方法名要和创建的 ExceptionUti的方法名字一致

 

openfeign的支持

在配置里多加3行代码,是openfeign让sentinel管理所需要的

 

创建一个实现类继承所需要的openfeign的方法

 openfeign的接口

 实现类

 运行成功后如下图所示

 当设置降级或者流控之后进行操作如下图所示

 



这篇关于sentinel分布式系统 流控 降级的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程