新聞中心
前兩天有朋友面試“淘汰集團(tuán)”,也就是“淘寶”+“天貓”的組合,最后被面試官問到了這道題:“你看過哪些開源框架的源碼?舉例說明一下”。

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站開發(fā)、程序開發(fā)、微網(wǎng)站、重慶小程序開發(fā)等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站制作、做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
誠(chéng)然,這是一道比較考驗(yàn)應(yīng)聘者基本功的問題,也是很好區(qū)分“好學(xué)生”和“普通學(xué)生”的一道經(jīng)典的開放性問題。
那這個(gè)問題應(yīng)該怎么回答呢?
解答思路
我這給大家提供兩個(gè)思路吧:
- 可以回答比較常見的,你比較熟悉的源碼,例如 Spring Boot 收到請(qǐng)求之后,執(zhí)行流程的源碼。
- 還可以回答 Spring Cloud 微服務(wù)中,某個(gè)組件執(zhí)行的流程源碼,這樣能很好的體現(xiàn)你對(duì)微服務(wù)比較熟悉,因?yàn)槲⒎?wù)在公司中應(yīng)用比較廣泛,所以回答的好,是一個(gè)極大的加分項(xiàng)。
1.Spring Boot 源碼分析
Spring Boot 在收到請(qǐng)求之后,會(huì)先執(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 {
// 省略其他...
}
}此時(shí)就進(jìn)入到了 DispatcherServlet 中的 doDispatch 方法了:
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 獲取原生請(qǐng)求
HttpServletRequest processedRequest = request;
// 獲取Handler執(zhí)行鏈
HandlerExecutionChain mappedHandler = null;
// 是否為文件上傳請(qǐng)求, 默認(rèn)為false
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
// 檢查是否為文件上傳請(qǐng)求
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
// 獲取能處理此請(qǐng)求的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í)行對(duì)應(yīng)方法
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
// 忽略其他...
}通過上述的源碼我們可以看到,請(qǐng)求的核心代碼都在 doDispatch 中,他里面包含的主要執(zhí)行流程有以下這些:
- 調(diào)用 HandlerExecutionChain 獲取處理器:DispatcherServlet 首先調(diào)用 getHandler 方法,通過 HandlerMapping 獲取請(qǐng)求對(duì)應(yīng)的 HandlerExecutionChain 對(duì)象,包含了處理器方法和攔截器列表。
- 調(diào)用 HandlerAdapter 執(zhí)行處理器方法:DispatcherServlet 使用 HandlerAdapter 來執(zhí)行處理器方法。根據(jù) HandlerExecutionChain 中的處理器方法類型不同,選擇對(duì)應(yīng)的 HandlerAdapter 進(jìn)行處理。常用的適配器有 RequestMappingHandlerAdapter 和 HttpRequestHandlerAdapter。
- 解析請(qǐng)求參數(shù):DispatcherServlet 調(diào)用 HandlerAdapter 的 handle 方法,解析請(qǐng)求參數(shù),并將解析后的參數(shù)傳遞給處理器方法執(zhí)行。
- 調(diào)用處理器方法:DispatcherServlet 通過反射機(jī)制調(diào)用處理器方法,執(zhí)行業(yè)務(wù)邏輯。
- 處理攔截器:在調(diào)用處理器方法前后,DispatcherServlet 會(huì)調(diào)用攔截器的 preHandle 和 postHandle方法進(jìn)行相應(yīng)的處理。
- 渲染視圖:處理器方法執(zhí)行完成后,DispatcherServlet 會(huì)通過 ViewResolver 解析視圖名稱,找到對(duì)應(yīng)的 View 對(duì)象,并將模型數(shù)據(jù)傳遞給 View 進(jìn)行渲染。
- 生成響應(yīng):View 會(huì)將渲染后的視圖內(nèi)容生成響應(yīng)數(shù)據(jù)。
2.Spring Cloud 源碼
Spring Cloud 組件有很多,你可以挑一個(gè)源碼實(shí)現(xiàn)比較簡(jiǎn)單的組件來講,這里推薦 Spring Cloud LoadBalancer,因?yàn)槠浜诵脑创a的實(shí)現(xiàn)比較簡(jiǎn)單。
Spring Cloud LoadBalancer 中內(nèi)置了兩種負(fù)載均衡策略:
- 輪詢負(fù)載均衡策略
- 隨機(jī)負(fù)載均衡策略
輪詢負(fù)載均衡策略的核心實(shí)現(xiàn)源碼如下:
// ++i 去負(fù)數(shù),得到一個(gè)正數(shù)值
int pos = this.position.incrementAndGet() & Integer.MAX_VALUE;
// 正數(shù)值和服務(wù)實(shí)例個(gè)數(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 獲取一個(gè)隨機(jī)數(shù),最大值為服務(wù)實(shí)例的個(gè)數(shù)
int index = ThreadLocalRandom.current().nextInt(instances.size());
// 得到實(shí)例
ServiceInstance instance = (ServiceInstance)instances.get(index);
// 返回
return new DefaultResponse(instance);小結(jié)
開源框架的源碼在面試中經(jīng)常會(huì)被問到,但只因如此,就去完整的看某個(gè)框架的源碼,其實(shí)還是挺難的。第一,框架中的源碼很多,很難一次性看懂。第二,即使能看懂,看完之后也會(huì)很快忘記(因?yàn)閮?nèi)容太多了)。此時(shí),不如挑一些框架中的經(jīng)典實(shí)現(xiàn)源碼來看,其性價(jià)比更高,既能學(xué)到框架中的精髓,又能搞定面試,是一個(gè)不錯(cuò)的選擇。
當(dāng)前文章:阿里面試:看過框架源碼嗎?舉例說明一下
文章出自:http://www.5511xx.com/article/djgeisd.html


咨詢
建站咨詢
