在接口访问流控规则生效,被控制以后会返回一个提示,这个提示我们是可以自定义的。
默认返回:
添加自定义类:
@Component
public class SentinelConfig implements BlockExceptionHandler {@Overridepublic void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {//根据限流降级的策略规则,不同的异常返回不同的提示语,String res="";if (e instanceof FlowException) {res ="接口限流了";}else if (e instanceof DegradeException){res ="服务降级了";}else if (e instanceof ParamFlowException){res ="热点参数限流了";}else if (e instanceof SystemBlockException){res ="触发系统保护规则了";}else if(e instanceof AuthorityException){res ="授权规则不通过";}httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());httpServletResponse.setCharacterEncoding("utf-8");httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);new ObjectMapper().writeValue(httpServletResponse.getWriter(),res);//跳转到指定页面://request.getRequestDispatcher("/index.jsp").forward(request,response);//跳转到网站//response.sendRedirect("https://www.baidu.com");}
}
添加完成,启动项目。
请求接口并添加流控规则,请求接口测试。