SpringCloudAlibaba Sentinel如何实现限流降级

60次阅读
没有评论

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

SpringCloudAlibaba Sentinel 如何实现限流降级,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面丸趣 TV 小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

大概有半个月没有进行更新了,确实也在纠结一些 SpringCloudAlibaba 的使用场景问题。同时基于当前的业务与人员配置来考虑甚至有点想放弃微服务的方案了。
Sentinel 的限流降级是必不可少的场景,其实也是基于当前的业务考虑是否需要 Sentinel。
但是最终肯定是需要 Sentinel 的场景的,还是直接一步到位吧

 Setinel 的基本概念与使用场景

Setinel 的介绍为「一个高可用的流量控制与防护组件」。流量控制与流量防护就可以看到这个组件的意义就是为了保障微服务的稳定性。Sentinel 与原有 SpringCloud 家族的 Hystrix 的意义是一致的。
其实能够实现该方案的场景很多,例如一开始提到的本来没有准备使用 Sentinel 一个是因为业务的原因。另外一个就是我们本身的一些问题从而考虑使用一些更简单的方案来实现。例如  「nginx」  等。

当然相对于 nginx 来说,Sentinel 能够实现的功能与灵活性更好一些。Sentinel 的功能更多,所以我也会慢慢来开始 Sentinel 的使用介绍。首先我们使用 Sentinel 实现一个限流的功能。当然首先我们需要一套 Sentinel 的环境。

  部署 Sentinel Dashboard

Sentinel 分为两个部分,sentinel-core 与 sentinel-dashboard。
sentinel-core 部分能够支持在本地引入 sentinel-core 进行限流规则的整合与配置。
sentinel-dashboard 则在 core 之上能够支持在线的流控规则与熔断规则的维护与调整等。
言归正传我们先部署一个 Sentinel Dashboard。

项目地址:https://github.com/alibaba/Sentinel 下载地址: https://github.com/alibaba/Sentinel/releases
   
本次我们选择当前的最新版 v1.7.2 进行部署。Sentinel 使用的 SpringBoot 进行的开发,所以直接下载 jar 包启动即可使用。

java -jar sentinel-dashboard-1.7.2.jar

 

由于 Sentinel-Dashboard 是使用 SpringBoot 进行开发的,所以本身没有太多的配置文件。默认的端口为 8080。如果端口冲突可以使用  「–server.port」  进行修改绑定。启动成功后使用浏览器访问可以看到如下页面:

 

访问方式为: 「sentinel」/「sentinel」
「WARN:」  如果需要调整相关的参数可以参考 github 中的具体配置文件进行修改。配置如下:

#spring settings
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true

#cookie name setting
server.servlet.session.cookie.name=sentinel_dashboard_cookie

#logging settings
logging.level.org.springframework.web=INFO
logging.file=${user.home}/logs/csp/sentinel-dashboard.log
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
#logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

#auth settings
auth.filter.exclude-urls=/,/auth/login,/auth/logout,/registry/machine,/version
auth.filter.exclude-url-suffixes=htm,html,js,css,map,ico,ttf,woff,png
# If auth.enabled=false, Sentinel console disable login
auth.username=sentinel
auth.password=sentinel

# Inject the dashboard version. It s required to enable
# filtering in pom.xml for this resource file.
sentinel.dashboard.version=${project.version}

 
  项目集成首先引入相关的依赖, 引入
   「spring-cloud-starter-alibaba-sentinel」模块。
   
[
   「groupId」再次提醒一下,com.alibaba.cloud 为毕业版本]

dependencies 
  dependency
  groupId com.alibaba.cloud /groupId
  artifactId spring-cloud-starter-alibaba-sentinel /artifactId
  /dependency
  dependency
  groupId org.springframework.boot /groupId
  artifactId spring-boot-starter-test /artifactId
  scope test /scope
  /dependency
  dependency
  groupId org.springframework.boot /groupId
  artifactId spring-boot-starter-web /artifactId
  /dependency
/dependencies

  然后我们就可以把项目中的增加 sentinel-dashboard 的相关配置

spring.cloud.sentinel.transport.dashboard=localhost:8080

  创建启动类与增加 Rest 访问接口
   

   「SentinelFlowControlApplication.java」

@SpringBootApplication
public class SentinelFlowControlApplication {

 public static void main(String[] args) {
 SpringApplication.run(SentinelFlowControlApplication.class,args);
 }

}

 

「SentinelTestController.java」

@RestController
@RequestMapping(value =  sentinel)
public class SentinelTestController {

 @RequestMapping(value =  hello ,method = RequestMethod.GET)
 public String hello(){
 return  CainGao
 }

}

 

访问 REST 接口打开 Sentinel-dashboard 页面可以看到我们每次访问的都会在 Dashboard 中显示出来,并且我们的服务也已经注册到了 sentinel 中。

增加流控规则
现在我们可以直接在左侧的 簇点链路 中查找到我们访问的端口进行流控规则的设置。现在新增一个 QPS=1 的规则,其他使用默认

配置完成后我们当前的限流效果应该就是每秒只能有一条请求成功发送。其他请求将会直接失败。

直接快速访问接口实现测试
   

当前可以发现,当请求的 QPS 大于 1 时,也就是每秒发送的次数大于 1 就会直接返回  「Blocked by Sentinel」。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注丸趣 TV 行业资讯频道,感谢您对丸趣 TV 的支持。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-17发表,共计3390字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)