3-7 Service fault tolerance sentinel- abnormal return

1 Exception return

Including custom global exception and individual resource exception handling

## 2 Customized exception page demonstration
needs to implement the interface UrlBlockHandler, add the class MyUrlBlockHandler in the config method

@Component
public class MyUrlBlockHandler implements UrlBlockHandler {
    @Override
    public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ResponseDate responseDate = null;
        if (e instanceof FlowException) {
            responseDate = new ResponseDate(-1, "接口被限流了");
        } else if (e instanceof DegradeException) {
            responseDate = new ResponseDate(-2, "接口被降级了");
        } else if (e instanceof ParamFlowException) {
            responseDate = new ResponseDate(-2, "接口被热点限流了");
        } else if (e instanceof AuthorityException) {
            responseDate = new ResponseDate(-2, "接口被授权规则限制访问了");
        } else if (e instanceof SystemBlockException) {
            responseDate = new ResponseDate(-2, "接口被系统规则限制了了");
        }
        httpServletResponse.getWriter().write(JSON.toJSONString(responseDate));
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class ResponseDate {
    private Integer code;
    private String message;
}

3 Annotate the specific exception return in sentinelResource

3.1 Concept description

  • The attribute value value represents the resource name
  • The attribute blockHandler represents the method that needs to be handled when the sentinel exception is generated
  • The attribute fallback represents the method to handle any exception
  • The attribute blockHandlerClass represents the class of the method pointed to by the blockHandler. The method used with this attribute must be a static method.
    Note: Do not use the custom exception page MyUrlBlockHandler at this time to cause conflicts.

3.2 Simple demonstration

3.2.1 Add a test method to the controller of the order service

    /**
     * 服务容错组件sentinel-演示sentinelResource属性
     */
    int m = 0;

    @RequestMapping("/sentinel/sentinelResource")
    @SentinelResource(value = "resourceName",
            blockHandlerClass = MyBlockHandler.class,
            // 这里blockHandler方法必须为静态方法
            blockHandler = "blockHandler",
            fallback = "fallback")
    public String sentinelResource(String name) {
        log.info("进入sentinelResource注解测试,参数name={}", name);
        m++;
        if (m % 3 == 0) {
            throw new RuntimeException("fallback异常");
        }
        return "服务容错组件sentinel中注解sentinel属性的使用演示";
    }

#### 3.2.2 New exception handling class MyBlockHandler

package cn.hzp.Exception;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyBlockHandler {
    // 此方法必须为静态方法
    public static String blockHandler(String name, BlockException b) {
        log.info("进入sentinelResource注解测试,进入blockHandler,参数name={},b={}", name, b.toString());
        return "blockHandler";
    }
}

3.2.3 Add an exception handling method to the controller of the order service

Note that the parameter is Throwable, this method can be extracted like BlockException

    public String fallback(String name, Throwable e){
        log.info("进入sentinelResource注解测试,进入fallback,参数name={},b={}", name, e.toString());
        return "fallback";
    }

3.2.4 Test verification

  • Set the qps of the flow control rule 1s to 1 for the resource name
  • accesshttp://localhost:8091/sentinel/sentinelResource?name=test
第一次可以正常访问
多次刷新会返回blockHandler
每个第三次都会返回fallback

Guess you like

Origin blog.csdn.net/weixin_45544465/article/details/105939078