sentinel热点参数流控 之 对象参数流控
已知代码如下:
controller部分
@RequestMapping("/hello2")
@ServiceId(BusinessEnum.TEST_XHJ)
public ResultDto hello2(TestObject testObject){
return testHandler.hello2(testObject);
}
handler部分
@SentinelResource(value = "test2",blockHandler = "handleBlockForHello2")
public ResultDto hello2(TestObject testObject) {
System.out.println("hello2:"+testObject.getApikey()+","+testObject.getName());
return ResultDto.result(ResultCodeEnum.SUCCESS,"Hello,Sentinel!");
}
public ResultDto handleBlockForHello2(TestObject testObject,BlockException e) {
log.error("sentinel block2 :",e);
return ResultDto.result(ResultCodeEnum.ApikeyError);
}
对象部分
public class TestObject {
private String name;
private String apikey;
public TestObject(String name, String apikey) {
this.name = name;
this.apikey = apikey;
}
public TestObject(){}
@Override
public boolean equals(Object o) {
// 首先检查是不是同一个对象实例
if (this == o) return true;
// 检查o是不是null或者类型是否正确
if (!(o instanceof TestObject)) return false;
// 将o转换为UnifySendReqDto类型
TestObject that = (TestObject) o;
System.out.println("testObject ing");
// 比较两者的appCode字段
return Objects.equals(this.apikey, that.apikey);
}
@Override
public int hashCode() {
return Objects.hash(apikey);
}
}
sentinelConfig部分
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.unicom.opn.api.core.dto.TestObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Slf4j
@Configuration
public class SentinelConfig {
public static TestObject testObject = new TestObject("xxx","a");
@PostConstruct
public void initFlowRules() {
limitApikey();
}
public void limitApikey(){
List<ParamFlowRule> rules = new ArrayList<>();
ParamFlowRule a = new ParamFlowRule("test2")
.setCount(Double.MAX_VALUE) // 允许访问的数量
.setParamIdx(0)
.setGrade(RuleConstant.FLOW_GRADE_QPS)
.setDurationInSec(10) // 持续时间,单位为秒
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT) //即当流量达到阈值时,直接拒绝多余的请求。
.setParamFlowItemList(
Collections.singletonList(new ParamFlowItem()
.setObject(testObject.toString())
.setCount(0))
);
rules.add(a);
ParamFlowRuleManager.loadRules(rules);
}
}
其中上述的setObject部分 已经改成过"a" 进行尝试;
问题:
访问hell2接口 无法实现对apikey=a的请求进行拦截。想请问下这是为什么
...全文
相关推荐
Java 微服务架构实战Seata 事务、Sentinel 规则、ShardingSphere、CI/CD
Java 微服务架构实战Seata 事务、Sentinel 规则、ShardingSphere、CI/CD
SpringCloud Alibaba使用 - sentinel 流控规则、降级规则、热点key限流、系统规则、@SentinelResource.zip
Sentinel 的主要工作机制如下: 对主流框架提供适配或者显示的 API,来定义需要保护的资源,并提供设施对资源进行实时统计和调用链路分析。 根据预设的规则,结合对资源的实时统计信息,对流量进行控制。同时,Sentinel 提供开放的接口,方便您定义及改变规则。 Sentinel 提供实时的监控系统,方便您快速了解目前系统的状态。
sentinel-global-flow全局流控管理
基于sentinel的1.7.2 release版本进行改造, 支持全局流控规则的动态管理配置
微服务接入sentinel流控中心
微服务接入sentinel流控中心
Sentinel 流控、统计和熔断
Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定 里面包含了相关jar文件


