阿里面試:看過框架源碼嗎?舉例說明一下
前兩天有朋友面試“淘汰集團(tuán)”,也就是“淘寶”+“天貓”的組合,最后被面試官問到了這道題:“你看過哪些開源框架的源碼?舉例說明一下”。
誠然,這是一道比較考驗(yàn)應(yīng)聘者基本功的問題,也是很好區(qū)分“好學(xué)生”和“普通學(xué)生”的一道經(jīng)典的開放性問題。
那這個問題應(yīng)該怎么回答呢?
解答思路
我這給大家提供兩個思路吧:
- 可以回答比較常見的,你比較熟悉的源碼,例如 Spring Boot 收到請求之后,執(zhí)行流程的源碼。
- 還可以回答 Spring Cloud 微服務(wù)中,某個組件執(zhí)行的流程源碼,這樣能很好的體現(xiàn)你對微服務(wù)比較熟悉,因?yàn)槲⒎?wù)在公司中應(yīng)用比較廣泛,所以回答的好,是一個極大的加分項。
1.Spring Boot 源碼分析
Spring Boot 在收到請求之后,會先執(zhí)行前端控制器 DispatcherServlet,并調(diào)用其父類 FrameworkServlet 中的 service 方法,其核心源碼如下:
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response);
} else {
super.service(request, response);
}
}
繼續(xù)往下看,processRequest 實(shí)現(xiàn)源碼如下:
protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 省略一堆初始化配置
try {
// 真正執(zhí)行邏輯的方法
doService(request, response);
}
catch (ServletException | IOException ex) {
...
}
}
doService 實(shí)現(xiàn)源碼如下:
protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception;
doService 是抽象方法,由其之類 DispatcherServlet 來重寫實(shí)現(xiàn),其核心源碼如下:
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 省略初始化過程...
try {
doDispatch(request, response);
}
finally {
// 省略其他...
}
}
此時就進(jìn)入到了 DispatcherServlet 中的 doDispatch 方法了:
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 獲取原生請求
HttpServletRequest processedRequest = request;
// 獲取Handler執(zhí)行鏈
HandlerExecutionChain mappedHandler = null;
// 是否為文件上傳請求, 默認(rèn)為false
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
// 檢查是否為文件上傳請求
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
// 獲取能處理此請求的Handler
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
// 獲取適配器
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
// 執(zhí)行攔截器(鏈)的前置處理
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 真正的執(zhí)行對應(yīng)方法
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
// 忽略其他...
}
通過上述的源碼我們可以看到,請求的核心代碼都在 doDispatch 中,他里面包含的主要執(zhí)行流程有以下這些:
- 調(diào)用 HandlerExecutionChain 獲取處理器:DispatcherServlet 首先調(diào)用 getHandler 方法,通過 HandlerMapping 獲取請求對應(yīng)的 HandlerExecutionChain 對象,包含了處理器方法和攔截器列表。
- 調(diào)用 HandlerAdapter 執(zhí)行處理器方法:DispatcherServlet 使用 HandlerAdapter 來執(zhí)行處理器方法。根據(jù) HandlerExecutionChain 中的處理器方法類型不同,選擇對應(yīng)的 HandlerAdapter 進(jìn)行處理。常用的適配器有 RequestMappingHandlerAdapter 和 HttpRequestHandlerAdapter。
- 解析請求參數(shù):DispatcherServlet 調(diào)用 HandlerAdapter 的 handle 方法,解析請求參數(shù),并將解析后的參數(shù)傳遞給處理器方法執(zhí)行。
- 調(diào)用處理器方法:DispatcherServlet 通過反射機(jī)制調(diào)用處理器方法,執(zhí)行業(yè)務(wù)邏輯。
- 處理攔截器:在調(diào)用處理器方法前后,DispatcherServlet 會調(diào)用攔截器的 preHandle 和 postHandle方法進(jìn)行相應(yīng)的處理。
- 渲染視圖:處理器方法執(zhí)行完成后,DispatcherServlet 會通過 ViewResolver 解析視圖名稱,找到對應(yīng)的 View 對象,并將模型數(shù)據(jù)傳遞給 View 進(jìn)行渲染。
- 生成響應(yīng):View 會將渲染后的視圖內(nèi)容生成響應(yīng)數(shù)據(jù)。
2.Spring Cloud 源碼
Spring Cloud 組件有很多,你可以挑一個源碼實(shí)現(xiàn)比較簡單的組件來講,這里推薦 Spring Cloud LoadBalancer,因?yàn)槠浜诵脑创a的實(shí)現(xiàn)比較簡單。
Spring Cloud LoadBalancer 中內(nèi)置了兩種負(fù)載均衡策略:
- 輪詢負(fù)載均衡策略
- 隨機(jī)負(fù)載均衡策略
輪詢負(fù)載均衡策略的核心實(shí)現(xiàn)源碼如下:
// ++i 去負(fù)數(shù),得到一個正數(shù)值
int pos = this.position.incrementAndGet() & Integer.MAX_VALUE;
// 正數(shù)值和服務(wù)實(shí)例個數(shù)取余 -> 實(shí)現(xiàn)輪詢
ServiceInstance instance = (ServiceInstance)instances.get(pos % instances.size());
// 將實(shí)例返回給調(diào)用者
return new DefaultResponse(instance);
隨機(jī)負(fù)載均衡策略的核心實(shí)現(xiàn)源碼如下:
// 通過 ThreadLocalRandom 獲取一個隨機(jī)數(shù),最大值為服務(wù)實(shí)例的個數(shù)
int index = ThreadLocalRandom.current().nextInt(instances.size());
// 得到實(shí)例
ServiceInstance instance = (ServiceInstance)instances.get(index);
// 返回
return new DefaultResponse(instance);
小結(jié)
開源框架的源碼在面試中經(jīng)常會被問到,但只因如此,就去完整的看某個框架的源碼,其實(shí)還是挺難的。第一,框架中的源碼很多,很難一次性看懂。第二,即使能看懂,看完之后也會很快忘記(因?yàn)閮?nèi)容太多了)。此時,不如挑一些框架中的經(jīng)典實(shí)現(xiàn)源碼來看,其性價比更高,既能學(xué)到框架中的精髓,又能搞定面試,是一個不錯的選擇。