Sentinel系列-黑白名单与注解支持

Sentinel黑白名单与注解支持

来源访问控制(黑白名单)

官方解释:很多时候,我们需要根据调用方来限制资源是否通过,这时候可以使用 Sentinel 的黑白名单控制的功能。黑白名单根据资源的请求来源(origin)限制资源是否通过,若配置白名单则只有请求来源位于白名单内时才可通过;若配置黑名单则请求来源位于黑名单时不通过,其余的请求通过。

规则配置

黑白名单规则(AuthorityRule)非常简单,主要有以下配置项:

  • resource:资源名,即限流规则的作用对象
  • limitApp:对应的黑名单/白名单,不同 origin 用 , 分隔,如 appA,appB
  • strategy:限制模式,AUTHORITY_WHITE 为白名单模式AUTHORITY_BLACK 为黑名单模式,默认为白名单模式
    根据介绍可以知道,黑白名单的限制分为3个参数,第一个参数resource为资源,这里不多做解析,资源是sentinel里面的基本元素。第二个参数limitApp就是规则参数,下面示例做详细解释。
示例

新建好springcloud项目,添加sentinel依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   sentinel     -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

修改application.yml文件

server:
  port: 8601
spring:
  application:
    name: cloudalibaba-sentinel-service-8601
  cloud:
    sentinel:
      transport: #dashboard地址
        dashboard: localhost:8088
        port: 8719  #默认端口,如果被占用则从8719依次+1扫描

添加limitapp限制配置

/**
 * 根据httpServletRequest中的参数进行限制黑白名单
 * 本文以ip来做限制
 */
public class IpRequestOriginParser implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest httpServletRequest) {
        return httpServletRequest.getRemoteAddr();
    }
}

@Configuration
public class SentinelConfig {

    @PostConstruct
    public void init(){
        //将自定义的阈值提示加载到应用中
        //      WebCallbackManager.setUrlBlockHandler(new DemoUrlBlockHandler());
        //黑白名单
        WebCallbackManager.setRequestOriginParser(new IpRequestOriginParser());
    }
}

配置控制台参数
在这里插入图片描述
这里以本地的ip地址为限制资源。访问的时候就会出现被限制的情况。

注解支持

Sentinel最主要的注解是@SentinelResource,Sentinel 提供了 @SentinelResource 注解用于定义资源,并提供了 AspectJ 的扩展用于自动定义资源、处理 BlockException 等。

@SentinelResource 注解

@SentinelResource 用于定义资源,和controller中的接口一样,用@SentinelResource注解标记的方法,会直接被Sentinel定义为资源,在控制台中可以看到。Sentinel同时提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:

  • value:资源名称,必需项(不能为空)

  • entryType:entry 类型,可选项(默认为 EntryType.OUT) blockHandler /

  • blockHandlerClass: blockHandler 对应处理 BlockException
    的函数名称,可选项。blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

  • fallback:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback
    函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。fallback
    函数签名和位置要求: 返回值类型必须与原函数返回值类型一致; 方法参数列表需要和原函数一致,或者可以额外多一个 Throwable
    类型的参数用于接收对应的异常。 fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定
    fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

  • defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback
    逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所以类型的异常(除了 exceptionsToIgnore
    里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求: 返回值类型必须与原函数返回值类型一致; 方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。 defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。

  • exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback逻辑中,而是会原样抛出。
    注意:
    如果 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。
    示例代码如下:

public class TestService {

    // 对应的 `handleException` 函数需要位于 `ExceptionUtil` 类中,并且必须为 static 函数.
    @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
    public void test() {
        System.out.println("Test");
    }

    // 原函数
    @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
    public String hello(long s) {
        return String.format("Hello at %d", s);
    }
    
    // Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String helloFallback(long s) {
        return String.format("Halooooo %d", s);
    }

    // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
    public String exceptionHandler(long s, BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + s;
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些示例代码,演示如何将 Sentinel-2 和 Sentinel-1 数据进行融合: 1. 利用Python的sentinelsat库下载Sentinel-2和Sentinel-1数据: ```python from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt # 登录sentinelsat账号 api = SentinelAPI('username', 'password', 'https://scihub.copernicus.eu/dhus') # 下载Sentinel-2数据 footprint = geojson_to_wkt(read_geojson('path/to/footprint.geojson')) products = api.query(footprint, platformname='Sentinel-2', cloudcoverpercentage=(0, 30), producttype='S2MSI1C') # 下载Sentinel-1数据 products = api.query(footprint, platformname='Sentinel-1', polarisationmode='VV VH', producttype='GRD', orbitdirection='ASCENDING') ``` 2. 使用Python的gdal库读取和处理Sentinel-1数据: ```python from osgeo import gdal # 读取Sentinel-1数据 s1_vv = gdal.Open('path/to/sentinel1_vv.tif') s1_vh = gdal.Open('path/to/sentinel1_vh.tif') # 将Sentinel-1数据转换为dB单位 s1_vv_db = 10 * np.log10(s1_vv.ReadAsArray()) s1_vh_db = 10 * np.log10(s1_vh.ReadAsArray()) # 对Sentinel-1数据进行滤波和校正 # ... # 将Sentinel-1数据重采样到Sentinel-2的分辨率 # ... # 将Sentinel-1数据和Sentinel-2数据进行融合 # ... ``` 3. 使用Python的scikit-image库将Sentinel-2和Sentinel-1数据进行融合: ```python from skimage import exposure # 将Sentinel-2数据进行拉伸和直方图匹配,使其与Sentinel-1数据的动态范围一致 s2_rgb = exposure.rescale_intensity(s2_rgb, in_range=(0, 0.3), out_range=(0, 1)) s2_rgb_matched = exposure.match_histograms(s2_rgb, s1_vv_db) # 将Sentinel-1数据和Sentinel-2数据进行加权融合 s1_weight = 0.6 s2_weight = 0.4 fused = (s1_weight * s1_vv_db + (1 - s1_weight) * s1_vh_db) * s2_weight + (1 - s2_weight) * s2_rgb_matched ``` 这只是一些示例代码,具体的融合方法和参数需要根据具体的应用场景进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值