温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloudAlibaba Sentinel如何实现限流降级

发布时间:2021-11-24 15:21:02 来源:亿速云 阅读:137 作者:柒染 栏目:云计算

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

大概有半个月没有进行更新了,确实也在纠结一些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」 进行修改绑定。启动成功后使用浏览器访问可以看到如下页面:

SpringCloudAlibaba Sentinel如何实现限流降级  

访问方式为: 「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页面SpringCloudAlibaba Sentinel如何实现限流降级可以看到我们每次访问的都会在Dashboard中显示出来,并且我们的服务也已经注册到了sentinel中。

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

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

  • 直接快速访问接口实现测试     SpringCloudAlibaba Sentinel如何实现限流降级

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI