sentinel如何整合spring cloud限流

发布时间:2022-03-29 09:11:33 作者:小新
来源:亿速云 阅读:210

这篇文章将为大家详细讲解有关sentinel如何整合spring cloud限流,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

spring cloud基于http进行服务调用,大致过程如下:

相关依赖

           <!-- 服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
 
       <!-- sentinel限流 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

示例

为简化处理,直接对url接口进行限流,不做服务调用

sentinel如何整合spring cloud限流

application.yml

spring:
  application:
    name: hello-sentinel
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8081

限流可使用本地配置、或者sentinel dashboard配置

HelloController

@RestController
public class HelloController {
 
    @SentinelResource(value = "hello", blockHandler = "blockHandle")
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
    public String blockHandle(BlockException e){
        e.printStackTrace();
        return "被限流了";
}

************

本地限流配置

sentinel如何整合spring cloud限流

CustomFlowRule

public class CustomFlowRule implements InitFunc {
 
    @Override
    public void init() throws Exception {
        List<FlowRule> flowRules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("hello");
        flowRule.setCount(1);
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        flowRules.add(flowRule);
        FlowRuleManager.loadRules(flowRules);
    }
}

META-INF/services/com.alibaba.csp.sentinel.init.InitFunc

com.example.demo.rule.CustomFlowRule

jmeter 测试

sentinel如何整合spring cloud限流

sentinel如何整合spring cloud限流

sentinel如何整合spring cloud限流

2022-03-27 22:30:50.534  INFO 1791 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-27 22:30:50.547  INFO 1791 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-27 22:30:50.557  INFO 1791 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.227 seconds (JVM running for 2.824)
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-27 22:31:04.044  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-27 22:31:04.049  INFO 1791 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

************

sentinel dashboard配置限流

启动sentinel dashboard

java -Dserver.port=8081 -Dcsp.sentinel.dashboard.server=localhost:8081 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
 
参数说明:
-Dserver.port=8081:指定控制台启动端口
-Dcsp.sentinel.dashboard.server:指定控制台地址和端口
-Dproject.name=sentinel-dashboard:指定控制台项目名称

localhost:8081,控制台配置流控策略

sentinel如何整合spring cloud限流

sentinel如何整合spring cloud限流

说明:若需使用本地降级方法,需在下方的hello配置流控规则

jmeter 测试

sentinel如何整合spring cloud限流

sentinel如何整合spring cloud限流

sentinel如何整合spring cloud限流

2022-03-28 08:50:29.165  INFO 853 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-28 08:50:29.198  INFO 853 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP hello-sentinel 192.168.5.11:8080 register finished
2022-03-28 08:50:29.210  INFO 853 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.315 seconds (JVM running for 4.03)
2022-03-28 08:52:05.792  INFO 853 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-28 08:52:05.793  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-03-28 08:52:05.802  INFO 853 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: /Users/huli/logs/csp/
INFO: Sentinel log name use pid is: false
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException

关于“sentinel如何整合spring cloud限流”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Spring Cloud Gateway 之 限流
  2. spring cloud gateway整合sentinel实现网关限流的方法

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

sentinel spring cloud

上一篇:基于uni-app开发刻度尺组件的示例分析

下一篇:Python如何使用execjs执行包含中文参数的JavaScript

相关阅读

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

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