当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Cloud Alibaba整合Sentinel的实现步骤

Spring Cloud Alibaba整合Sentinel的实现步骤

2020年10月26日  | 移动技术网IT编程  | 我要评论
一、需求实现一个简单的 整合 sentinel,不涉及sentinel的用法二、实现步骤1、下载 sentinel dashboardhttps://github.com/alibaba/sentin

一、需求

实现一个简单的 整合 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 注解支持

到此这篇关于spring cloud alibaba整合sentinel的实现步骤的文章就介绍到这了,更多相关spring cloud alibaba整合sentinel内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网