温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Sentinel-【来源相关】【错误自定义】(七)

发布时间:2020-07-16 05:43:17 来源:网络 阅读:879 作者:lxhllf2005 栏目:编程语言

1.错误重定向: 增加组建,通用异常捕获处理(增加sentinel异常次数-Tracer.trace(e))

(1).错误重定向

@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","限流-异常");
        }else if (e instanceof DegradeException){
            backMap.put("code",101);
            backMap.put("msg","降级-异常");
        }else if (e instanceof ParamFlowException){
            backMap.put("code",102);
            backMap.put("msg","热点-异常");
        }else if (e instanceof SystemBlockException){
            backMap.put("code",103);
            backMap.put("msg","系统规则-异常");
        }else if (e instanceof AuthorityException){
            backMap.put("code",104);
            backMap.put("msg","认证-异常");
        }

        // 设置返回json数据
        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).通用异常捕获处理(增加sentinel异常次数)


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 处理身份异常
    @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);
    }

    // 处理表单字段异常
    @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);
    }
}

来源处理,服务来源传递

思路:
    (1).【Client方】,设置当前 Fegin 头部的【source-origin】值为当前应用名称: spring.application.name
        (2).【被调用方】,配置Sentinel的Origin解析操作

(1).Fegin 服务来源传递

@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).来源处理,增加组建

@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("头部名称是: "+ JSON.toJSONString(httpServletRequest.getHeaderNames()));
//        log.info("来源是: "+origin);
        return origin;
    }
}

(3).【sentinel控制台】测试服务来源限制

Sentinel-【来源相关】【错误自定义】(七)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI