Sentinel BlockException全局处理

在集成Sentinel之前,项目中可能已经存在了异常全局处理器,示例如下:

package tech.foolfish.demo;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import com.google.common.base.Throwables;
import com.immotors.zeus.web.vo.ResponseContainer;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice({ "tech.foolfish.demo" })
@Order
@ResponseBody
@Slf4j
public class MyExceptionHandler {

	/**
	 * 
	 * @param request
	 * @param e
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler({ Exception.class })
	public ResponseContainer handleException(Exception e) {
		log.error(Throwables.getStackTraceAsString(e));
		ResponseContainer<Object> responseContainer = ResponseContainer.error("200", "100001", "系统异常");
		return responseContainer;
	}

	/**
	 * 
	 * @param request
	 * @param appChargeMapApiException
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler({ AppChargeMapApiException.class })
	public ResponseContainer handleAppChargeMapApiException(AppChargeMapApiException appChargeMapApiException) {
		log.error(appChargeMapApiException.getMessage(), Throwables.getStackTraceAsString(appChargeMapApiException));
		return ResponseContainer.error("200", appChargeMapApiException.getAppChargeMapApiEnum().getCode(), appChargeMapApiException.getAppChargeMapApiEnum().getMessage());
	}
}

集成了Sentinel,我们会写一个自定义的BlockExceptionHandler,示例如下:

package tech.foolfish.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

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.alibaba.fastjson.JSON;
import com.google.common.base.Throwables;
import com.immotors.zeus.web.vo.ResponseContainer;

import lombok.extern.slf4j.Slf4j;


@Slf4j
@Component
public class MyBlockExceptionHandler implements BlockExceptionHandler {

	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
		@SuppressWarnings("rawtypes")
		ResponseContainer responseContainer;
		if (e instanceof FlowException) {
			// 限流
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_QPS_CURRENT_LIMIT);
		} else if (e instanceof DegradeException) {
			// 降级
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_SERVICE_CIRCUIT_BREAK);
		} else if (e instanceof ParamFlowException) {
			// 热点参数
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_QPS_CURRENT_LIMIT);
		} else if (e instanceof SystemBlockException) {
			// 系统保护
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_QPS_CURRENT_LIMIT);
		} else if (e instanceof AuthorityException) {
			// 授权规则
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_SERVICE_UNAUTHORIZED_ACCESS);
		} else {
			responseContainer = ResponseContainerUtil.error(MySentinelExceptionEnum.CHARGE_MAP_QPS_CURRENT_LIMIT);
		}
		// 返回json数据
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type", "application/json;charset=UTF-8");
        PrintWriter out;
		try {
			out = response.getWriter();
			out.print(JSON.toJSON(responseContainer));
			out.flush();
			out.close();
		} catch (IOException e1) {
			log.error("Sentinel流控的系统异常统一处理逻辑发生了IO异常,异常 = {}", Throwables.getStackTraceAsString(e1));
		}

	}

}

修改异常全局处理器,示例如下:

package tech.foolfish.demo;

import javax.servlet.http.HttpServletRequest;

import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
import com.google.common.base.Throwables;
import com.immotors.zeus.web.vo.ResponseContainer;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice({ "tech.foolfish.demo" })
@Order
@ResponseBody
@Slf4j
public class MyExceptionHandler {

	// sentinelWebInterceptor
	private static SentinelWebInterceptor sentinelWebInterceptor;

	static {
		SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
		sentinelWebMvcConfig.setBlockExceptionHandler(new MyBlockExceptionHandler());
		sentinelWebMvcConfig.setHttpMethodSpecify(true);
		sentinelWebMvcConfig.setWebContextUnify(true);
		sentinelWebInterceptor = new SentinelWebInterceptor(sentinelWebMvcConfig);
	}

	/**
	 * sentinelWebInterceptor.afterCompletion:避免BlockException不生效!
	 * 
	 * @param request
	 * @param e
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler({ Exception.class })
	public ResponseContainer handleException(HttpServletRequest request, Exception e) {
		log.error(Throwables.getStackTraceAsString(e));
		try {
			sentinelWebInterceptor.afterCompletion(request, null, null, e);
		} catch (Exception e) {

		}
		ResponseContainer<Object> responseContainer = ResponseContainer.error("200", "100001", "系统异常");
		return responseContainer;
	}

	/**
	 * sentinelWebInterceptor.afterCompletion:避免BlockException不生效!
	 * 
	 * @param request
	 * @param appChargeMapApiException
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@ExceptionHandler({ AppChargeMapApiException.class })
	public ResponseContainer handleAppChargeMapApiException(HttpServletRequest request, AppChargeMapApiException appChargeMapApiException) {
		try {
			sentinelWebInterceptor.afterCompletion(request, null, null, appChargeMapApiException);
		} catch (Exception e) {

		}
		log.error(appChargeMapApiException.getMessage(), Throwables.getStackTraceAsString(appChargeMapApiException));
		return ResponseContainer.error("200", appChargeMapApiException.getAppChargeMapApiEnum().getCode(), appChargeMapApiException.getAppChargeMapApiEnum().getMessage());
	}
}