Sentinel1.8.6自定义错误页

在Sentinel中,限流和降级的提示词都是一样的。我们可以进行优化

Sentinel1.8.6中,Spring Cloud Alibaba Sentinel 不再依赖 sentinel-web-servlet 实现,而是依赖 sentinel-spring-webmvc-dapter , 基于 Spring 的 Interceptor 拦截资源,而不再是 CommonFilter。
低版本 实现 com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler的blocked方法
高版本 实现 com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler的handle方法

这里我们实现BlockExceptionHandler的handle方法

package com.itmuch.contentcenter.sentineltest;

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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

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

@Component
public class MyUrlBlockHandler implements BlockExceptionHandler {

    //实现 com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler的handle方法

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws Exception {
        ErrorMsg msg = null;
        if(ex instanceof FlowException){
           msg = ErrorMsg.builder()
                    .status(100)
                    .msg("限流了")
                    .build();

        }else if(ex instanceof DegradeException){
            msg = ErrorMsg.builder()
                    .status(101)
                    .msg("降级了")
                    .build();

        }else if(ex instanceof ParamFlowException){
            msg = ErrorMsg.builder()
                    .status(102)
                    .msg("热点参数限流")
                    .build();

        }else if(ex instanceof SystemBlockException){
            msg = ErrorMsg.builder()
                    .status(103)
                    .msg("系统规则(负载/...不满足要求)")
                    .build();

        }else if(ex instanceof AuthorityException){
            msg = ErrorMsg.builder()
                    .status(104)
                    .msg("授权规则不通过")
                    .build();

        }
        response.setStatus(500);
        response.setCharacterEncoding("utf-8");
        response.setHeader("ContentType","application/json;charset=utf-8");
        response.setContentType("application/json;charset=utf-8");
        new ObjectMapper()
                .writeValue(
                        response.getWriter(),
                        msg
                );

    }


}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class ErrorMsg{
    private Integer status;
    private String msg;
}

实现完后,别忘了加上@Component,这样可以实现不同类型返回不同的提示。

同理RequestOriginParser、UrlCleaner也移动到了com.alibaba.csp.sentinel.adapter.spring.webmvc.callback包里,可以自己实现扩展功能。

你可能感兴趣的:(sentinel,错误页,BlockException)