sentinel热点参数流控 之 对象参数流控

LXMXHJ LXMXHJ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

已知代码如下:

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的请求进行拦截。想请问下这是为什么

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
LXMXHJ
java技术交流 108 成员 85 内容