Sentinel入门与进阶:微服务流量控制的最佳实践 ( 四 )

6.资源限流

6.1.修改程序

6.1.1.在Controller中增加下列代码

java 复制代码
    @Autowired
    private TestService testService;

    @RequestMapping("/callService")
    public String callService(){
        return testService.testMethod();
    }

6.1.2.增加service接口及接口实现类

java 复制代码
package com.yuan.scasentineldashboard.service;

public interface TestService {
    String testMethod();
}
java 复制代码
package com.yuan.scasentineldashboard.service.impl;

import com.yuan.scasentineldashboard.service.TestService;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Override
    public String testMethod() {
        return "这是service中的方法";
    }
}

6.1.3.测试

浏览器访问 http://localhost:9000/sentinelTest/callService

6.2.pom.xml 增加 依赖

增加依赖以支持资源限流的注解

xml 复制代码
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
        </dependency>

6.3.修改方法

在serviceImpl中增加 @SentinelResource注解,就可以实现对该方法的限流

java 复制代码
    @Override
    @SentinelResource(value = "testMethod")
    public String testMethod() {
        return "这是service中的方法";
    }

6.4.Sentinel 监听

查看 Sentinel Dashboard 可以看到

6.5.修改限流配置

json 复制代码
[
    {
        "resource": "/sentinelTest/sayHello",
        "limitApp": "default",
        "grade": 1,
        "count": 3    
    },
    {
        "resource": "/sentinelTest/callService",
        "limitApp": "default",
        "grade": 1,
        "count": 3    
    },
    {
        "resource": "testMethod",
        "limitApp": "default",
        "grade": 1,
        "count": 2    
    }
]

6.6.查看 sentinel 流控规则

6.7.测试

1秒内 连续请求 3次, 触发方法限流后抛出了异常

1秒内 连续请求4次, 触发请求限流

6.8.实现方法的限流

通过刚才的例子,我们看到方法已经成功被限流了,但是限流后直接抛出了异常, 实际开发中,我们不希望展示一个生硬的报错,我们希望限流后能自定义一些处理方式。我们可修改@SentinelResource注解的参数来达到目的,

注意:处理的方法需要与被限流的方法保持参数相同,并在最后加上BlockException异常参数,同时返回类型必须相同。

6.8.1.修改serviceImpl 类

java 复制代码
package com.yuan.scasentineldashboard.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yuan.scasentineldashboard.service.TestService;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {

    @Override
    @SentinelResource(value = "testMethod", blockHandler = "blockMethod")
    public String testMethod(String a) {
        return "这是service中的方法";
    }

    public String blockMethod(String a, BlockException e){
        return "方法被限流了";
    }
}

6.8.2.测试效果

1秒内 连续请求 3次, 触发方法限流后执行限流方法

相关推荐
fanly117 小时前
AI 赋能微服务工程化:Surging Engine-CLI 的插件化 Agent 架构革新
微服务·surging
SilentSamsara12 小时前
存储卷体系:EmptyDir/HostPath/PV/PVC/StorageClass 的选型决策树
服务器·微服务·云原生·容器·架构·kubernetes·k8s
indexsunny14 小时前
互联网大厂Java面试实录:微服务+Spring Boot在电商场景中的应用
java·spring boot·redis·微服务·eureka·kafka·spring security
SilentSamsara14 小时前
Service 与 Ingress:从 ClusterIP 到云厂商 ALB 的完整流量路径
linux·运维·服务器·微服务·kubernetes·k8s·运维开发
SilentSamsara15 小时前
ConfigMap 与 Secret:配置注入的四种姿势与安全边界
linux·运维·服务器·安全·微服务·kubernetes·k8s
zz07232015 小时前
Seata ——微服务分布式事务
分布式·微服务·架构·seata
rchmin17 小时前
Nacos 3.x 优势介绍及接入指南
微服务·服务发现·动态配置
匀泪1 天前
云原生(Kubernetes service微服务)
微服务·云原生·kubernetes
uNke DEPH1 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
全栈软件开发2 天前
企业年报服务系统/小微服务助手小程序源码带搭建教程
微服务·年报小程序源码