zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Sentinel采用SphO方式定义资源,报错:The order of entry exit can‘t be paired with the order of entry

资源 报错 方式 The of with 定义 be
2023-09-14 09:13:55 时间

(1)问题描述

在搭建sentinel工程的时候,采用SphO方式手动定义资源,启动工程后,访问资源,发现报错,报错内容大致如下:

o.s.web.servlet.HandlerExecutionChain    :
 HandlerInterceptor.afterCompletion threw exception

com.alibaba.csp.sentinel.ErrorEntryFreeException: The order of entry exit can't be paired with the order of entry,
 current entry in context: <testSphO>, but expected: </api/sentinel/testSphO>

 

o.s.web.servlet.HandlerExecutionChain    :
 HandlerInterceptor.afterCompletion threw exception

com.alibaba.csp.sentinel.ErrorEntryFreeException: The order of entry exit can't be paired with the order of entry,
 current entry in context: <testSphO>, but expected: </api/sentinel/testSphO>

(2)解决方案

报错原因:

使用SphO方式定义资源时候,通过entry()方法开启,但是没有调用exit()结束,所以导致上面的报错。

解决办法:

调用【SphO.exit()】关闭资源。

具体案例代码如下:

@GetMapping("/testSphO")
public String testSphO() {
    boolean testSphO = SphO.entry("testSphO");
    if (testSphO) {
        try {
            // TODO 这里执行具体业务逻辑
            System.out.println("这里执行具体业务逻辑");
            return "sentinel call testSphU() method.";
        } finally {
            SphO.exit(); // 关闭
        }
    } else {
        // TODO 这里执行限流的逻辑
        System.out.println("这里执行限流的逻辑");
        return "testSphU 被限流啦。";
    }
}

以上就是我遇到的SphO错误以及解决方案。