這是堅持技術寫做計劃(含翻譯)的第8篇,定個小目標999,每週最少2篇。php
前段時間的文章可能是運維方面的,最近放出一波後端相關的。html
最近開始使用Sentinel進行流量保護,可是默認的web servlet filter是攔截所有http請求。在傳統的項目中問題不大。可是若是項目中用了Spring MVC,而且用了@PathVariable就尷尬了。
好比 uri pattern是 /foo/{id}
,而從Sentinel監控看 /foo/1
和 /foo/2
就是兩個資源了,而且Sentinel最大支持6000個資源,再多就不生效了。前端
WebCallbackManager.setUrlCleaner(new UrlCleaner() {
@Override
public String clean(String originUrl) {
if (originUrl.startsWith(fooPrefix)) {
return "/foo/*";
}
return originUrl;
}
});
複製代碼
可是想一想就吐, /v1/{foo}/{bar}/qux/{baz}
這種的來個20來個,截一個我看看。java
換種思路,uri pattern難搞,用笨辦法 aop總行吧?答案是能夠的。git
@Aspect
public class SentinelResourceAspect {
@Pointcut("within(com.anjia.*.web.rest..*)")
public void sentinelResourcePackagePointcut() {
// Method is empty as this is just a Pointcut, the implementations are
// in the advices.
}
@Around("sentinelResourcePackagePointcut()")
public Object sentinelResourceAround(ProceedingJoinPoint joinPoint) throws Throwable {
Entry entry = null;
// 務必保證finally會被執行
try {
// 資源名可以使用任意有業務語義的字符串
// 注意此處只是類名#方法名,方法重載是合併的,若是須要進行區分,
// 能夠獲取參數類型加入到資源名稱上
entry = SphU.entry(joinPoint.getSignature().getDeclaringTypeName()+
"#"+joinPoint.getSignature().getName());
// 被保護的業務邏輯
// do something...
} catch (BlockException ex) {
// 資源訪問阻止,被限流或被降級
// 進行相應的處理操做
} finally {
if (entry != null) {
entry.exit();
}
}
return result;
}
}
複製代碼
溫習一下 Spring mvc的執行流程 doFilter -> doService -> dispatcher -> preHandle -> controller -> postHandle -> afterCompletion -> filterAfter
程序員
核心的是 String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
可是是在dispatcher階段才賦值的,因此在CommFilter是取不到的,因此致使使用官方的Filter是不行的。只能用攔截器
github
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class SentinelHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String origin = parseOrigin(request);
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String uriTarget = StringUtils.defaultString(pattern,FilterUtil.filterTarget(request));
try {
// Clean and unify the URL.
// For REST APIs, you have to clean the URL (e.g. `/foo/1` and `/foo/2` -> `/foo/:id`), or
// the amount of context and resources will exceed the threshold.
UrlCleaner urlCleaner = WebCallbackManager.getUrlCleaner();
if (urlCleaner != null) {
uriTarget = urlCleaner.clean(uriTarget);
}
RecordLog.info(String.format("[Sentinel Pre Filter] Origin: %s enter Uri Path: %s", origin, uriTarget));
SphU.entry(uriTarget, EntryType.IN);
return true;
} catch (BlockException ex) {
RecordLog.warn(String.format("[Sentinel Pre Filter] Block Exception when Origin: %s enter fall back uri: %s", origin, uriTarget), ex);
WebCallbackManager.getUrlBlockHandler().blocked(request, response, ex);
return false;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
while (ContextUtil.getContext() != null && ContextUtil.getContext().getCurEntry() != null) {
ContextUtil.getContext().getCurEntry().exit();
}
ContextUtil.exit();
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
private String parseOrigin(HttpServletRequest request) {
RequestOriginParser originParser = WebCallbackManager.getRequestOriginParser();
String origin = EMPTY_ORIGIN;
if (originParser != null) {
origin = originParser.parseOrigin(request);
if (StringUtil.isEmpty(origin)) {
return EMPTY_ORIGIN;
}
}
return origin;
}
private static final String EMPTY_ORIGIN = "";
}
複製代碼
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Inject
SentinelHandlerInterceptor sentinelHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sentinelHandlerInterceptor);
}
}
複製代碼
上面說過,UrlCleaner是爲了歸併請求,清洗url用的。而UrlBlockHandler是在被攔截後的默認處理器。可是clean和handler都不是鏈式的,因此若是有多種處理,須要本身在一個方法裏,進行邏輯判斷。web
UrlCleanerspring
WebCallbackManager.setUrlCleaner(new UrlCleaner() {
@Override
public String clean(String originUrl) {
if (originUrl.startsWith(fooPrefix)) {
return "/foo/*";
}
return originUrl;
}
});
複製代碼
UrlBlockHandler
若是通用一點的,能夠本身根據request的 content-type進行自適應返回內容(PLAN_TEXT和JSON)apache
WebCallbackManager.setUrlBlockHandler((request, response, ex) -> {
response.addHeader("Content-Type","application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("{\"code\"":429,\"msg\":\"系統繁忙,請稍後重試\""}");
out.flush();
out.close();
});
複製代碼
WebServletConfig.setBlockPage(blockPage)
WebServletConfig.setBlockPage("http://www.baidu.com")
複製代碼
注意,三個方法都不是不支持調用鏈,好比我寫兩個UrlBlockHandler,只認最後一個。
山東濟南的小夥伴歡迎投簡歷啊 加入咱們 , 一塊兒搞事情。
長期招聘,Java程序員,大數據工程師,運維工程師,前端工程師。