Sentinel 自定义扩展

Sentinel 自定义扩展

1.自定义返回值扩展

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
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;
import java.io.IOException;
@Component
public class MyUrlBlockHandler implements UrlBlockHandler {
    @Override
    public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
        ErrorMsg msg = null;
        if (e instanceof FlowException){
            msg = ErrorMsg.builder().status(100).msg("限流了").build();
        }else if(e instanceof DegradeException){
            msg = ErrorMsg.builder().status(100).msg("降级了").build();
        }else if(e instanceof ParamFlowException){
            msg = ErrorMsg.builder().status(100).msg("热点参数限流").build();
        }else if(e instanceof SystemBlockException){
            msg = ErrorMsg.builder().status(100).msg("系统规则(负载不满足)").build();
        }else if(e instanceof AuthorityException){
            msg = ErrorMsg.builder().status(100).msg("授权规则不通过").build();
        }
        httpServletResponse.setStatus(500);
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        new ObjectMapper().writeValue(httpServletResponse.getWriter(),msg);
    }
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class ErrorMsg{
    private Integer status;
    private String msg;
}

2.实现区分来源

import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.util.StringUtil;

import javax.servlet.http.HttpServletRequest;

@Component
public class MyRequestOriginPareser implements RequestOriginParser {
    @Override
    public String parseOrigin(HttpServletRequest httpServletRequest) {
        //参数中获取origin的参数并返回
        String origin  =httpServletRequest.getParameter("origin");
        if(StringUtil.isBlank(origin)){
            throw new IllegalArgumentException("origin must be specified");
        }
        return origin;
    }
}

3.RESTfuURL支持

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyUrlCleaner implements UrlCleaner {
    @Override
    public String clean(String s) {
        String[] split = s.split("/");
        return Arrays.stream(split).map(str -> {
                    if (NumberUtils.isNumber(str)) {
                        return "{number}";
                    }
                    return str;
                }
        ).reduce((a, b) -> a + "/" + b).orElse("");

    }
}