Recently, when I was learning spring cloud Alibaba, I needed sentinel for traffic management and control. When I configured the unified handling of returned exceptions, the implementation of urlblockhandler became popular. The reason is that I used sentinel 2.2.1.release, which was officially changed to blockexceptionhandler.
Solution: Inheritance BlockExceptionHandler The specific code is as follows:
package com.sentinel;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import com.Enum.RequestMsgEnum;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.global.BaseController;
/**
* Custom sentinel exception return message
*/
@Component
public class MyUrlBlockHandler extends BaseController implements BlockExceptionHandler{
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
String msg = null;
if (ex instanceof FlowException) {
msg = "Flow is restricted";
} else if (ex instanceof DegradeException) {
msg = "Degraded";
} else if (ex instanceof ParamFlowException) {
msg = "Hotspot parameter flow restriction";
} else if (ex instanceof SystemBlockException) {
msg = "System rule (load/... Not satisfied)";
} else if (ex instanceof AuthorityException) {
msg = "Authorization rule not passed";
}
// http status code
response.setStatus(500);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "application/json;charset=utf-8");
response.setContentType("application/json;charset=utf-8");
// spring mvc comes with a json manipulation tool called jackson
new ObjectMapper()
.writeValue(
response.getWriter(),
this.responseResult(RequestMsgEnum.Failtrue, msg)
);
}
}
Note: the basecontroller class is my own definition class. The custom format is returned to the front end and needs to be modified according to my own needs.
Read More:
- [Solved] Mybatis uses the PageHelper paging plugin error: Could not find method on interface ibatis.executor.Executor named query.
- [Go] Solve the empty interface interface{} cannot use (type []string) as type []interface {}
- [Solved] Failed to find glog – Could not find glog include directory, set…
- [Solved] Interface automation test: JSON parse error
- NPM start project error: cannot find module ‘webpack’ problem solution
- Two implementation methods of spring boot scan mapper interface class
- [Solved] ROS Error: Could NOT find move_base_msgs
- [Solved] TS Error: Could not find a declaration file for module
- Error: could not find function … in R [How to Solve]
- Tensorflow Run Error or the interface is stuck or report error: Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
- Spring boot uses configuration interface webmvcconfigurer to solve cross domain problems
- [Solved] Android HTTPS request resource or interface error: server certificate
- [ubuntu14.04]Tensorflow: Could not find any downloads that satisfy the requirement
- [Solved] Mybatis Error: CannotFindDataSourceException: dynamic-datasource can not find primary datasource
- Aspecj cannot intercept method annotations on an interface
- [Solved] Error: Error: Cannot find channel “latest.yml“ update info: HttpError: 404 Not Found
- [Solved] Could not find a package configuration file provided by “moveit_core“
- How to Solve Error: IncompleteElementException: Could not find result map…
- SSM custom 404 and 500 error reporting interface
- How to Solve Error “Could not find com.android.tools.build:gradle:3.0.1”