Sentinel - source related error customization

Posted by healy787 on Wed, 27 Nov 2019 16:00:29 +0100

1. Error redirection: add build and general exception capture (increase the number of sentinel exceptions - Tracer.trace(e))

(1). Error redirection

@Component
public class MyUrlBlockHandler implements UrlBlockHandler {
    @Override
    public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
        Map<String,Object> backMap=new HashMap<>();
        backMap.put("error","sentinel error");
        if (e instanceof FlowException){
            backMap.put("code",100);
            backMap.put("msg","Current limiting-abnormal");
        }else if (e instanceof DegradeException){
            backMap.put("code",101);
            backMap.put("msg","Downgrade-abnormal");
        }else if (e instanceof ParamFlowException){
            backMap.put("code",102);
            backMap.put("msg","Hotspot-abnormal");
        }else if (e instanceof SystemBlockException){
            backMap.put("code",103);
            backMap.put("msg","System rules-abnormal");
        }else if (e instanceof AuthorityException){
            backMap.put("code",104);
            backMap.put("msg","Authentication-abnormal");
        }

        // Setting returns json data
        String header= MediaType.APPLICATION_JSON_UTF8_VALUE;
        httpServletResponse.setStatus(200);
        httpServletResponse.setHeader("content-Type",header);
        httpServletResponse.setContentType(header);
        httpServletResponse.getWriter().write(JSON.toJSONString(backMap));
    }
}

(2) general exception capture processing (increase the number of sentinel exceptions)

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // Handle identity exception
    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<ApiResult> handler(SecurityException e) {
        ApiResult backResult = ApiResult.builder().code(1001).message(e.getMessage()).build();
        Tracer.trace(e);
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }

    // Handling form field exceptions
    @ExceptionHandler(FormFieldErrorException.class)
    public ResponseEntity<ApiResult> handler(FormFieldErrorException e) {
        ApiResult backResult = ApiResult.builder()
                .code(1003)
                .data(e.getFieldMessage())
                .message(e.getMessage())
                .build();
        Tracer.trace(e);
        return new ResponseEntity<ApiResult>(backResult, HttpStatus.OK);
    }
}

Source processing, service source delivery

Train of thought:
    (1) [Client side], set the [source origin] value of the current Fegin header as the current application name: spring.application.name
        (2) [callee], configure Sentinel's Origin resolution operation

(1).Fegin service source delivery

@Configuration
@Slf4j
public class MyRequestInterceptor implements RequestInterceptor {
    @Value("${spring.application.name}")
    String applicationName;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header("source-origin", applicationName);
    }
}

(2) source processing, adding organization

@Slf4j
@Component
public class MyRequestOriginParse implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest httpServletRequest) {
        String origin=httpServletRequest.getHeader("source-origin");
        if (StringUtils.isBlank(origin)){
            origin="default";
        }
//        log.info("the header name is:" + JSON.toJSONString(httpServletRequest.getHeaderNames()));
//        log.info("source is:" + origin);
        return origin;
    }
}

(3) [sentinel console] test service source restriction

Topics: Java JSON Spring