关于java:Spring-Cloud-Alibaba-整合-Sentinel

3次阅读

共计 1771 个字符,预计需要花费 5 分钟才能阅读完成。

一、需要

实现一个简略的 整合 sentinel, 不波及 sentinel 的用法

二、实现步骤

1、下载 sentinel dashboard

https://github.com/alibaba/Sentinel/releases
留神:

默认会启动 8080 端口,如果端口抵触,能够在启动命令上退出 -Dserver.port= 新端口

默认用户名和明码 [sentinel/sentinel]
启动控制台可用的配置项

2、服务提供者和消费者引入 sentinel 依赖

 <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

留神: 在这个外面查找 sentinel-core的版本号,能够确定咱们须要下载那个版本的 sentinel dashboard

3、配置控制台信息

spring:
    sentinel:
      transport:
        # 控制台的地址
        dashboard: localhost:8080
        # 与控制台通信的端口,默认是 8719,不可用会始终 +1,晓得找到一个可用的
        port: 8719
        # 和控制台放弃心跳的 ip 地址
        client-ip: 127.0.0.1
        # 发送心跳的周期,默认是 10s
        heartbeat-interval-ms: 10000

4、一个简答的整合就整合完了,拜访资源,查看控制台。

三、整合Feigen

1、配置文件中退出 feign.sentinel.enabled=true 即可。
2、退出依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3、对 Feign接口的降级或限流等操作时,资源名称的格局为:httpmethod:protocol://requesturl

四、整合 RestTemplate

1、在 RestTemplate 上退出 @SentinelRestTemplate注解。
2、配置文件中退出 resttemplate.sentinel.enabled: true, 默认就是 true
3、降级或限流时的解决

抄 sentinel 的官网形容,文本给出链接

@SentinelRestTemplate 注解的属性反对限流 (blockHandler, blockHandlerClass) 和降级 (fallback, fallbackClass) 的解决。其中 blockHandler 或 fallback 属性对应的办法必须是对应 blockHandlerClass 或 fallbackClass 属性中的静态方法。该办法的参数跟返回值跟 org.springframework.http.client.ClientHttpRequestInterceptor#interceptor 办法统一,其中参数多出了一个 BlockException 参数用于获取 Sentinel 捕捉的异样。比方上述 @SentinelRestTemplate 注解中 ExceptionUtil 的 handleException 属性对应的办法申明如下:public class ExceptionUtil {public static ClientHttpResponse handleException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {...}
}

五、@SentinelResource 的用法

1、@SentinelResource 注解用来标识资源是否被限流、降级。
2、个别举荐将 @SentinelResource 注解加到服务实现上
3、能够指定 blockHandlerfallback,在产生异样时的解决。

六、代码门路

https://gitee.com/huan1993/spring-cloud-alibaba-parent/tree/master/sentinel

七、参考文档

Sentinel Wike
Sentinel 注解反对

正文完
 0