掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
前兩天有朋友面試“淘汰集團(tuán)”,也就是“淘寶”+“天貓”的組合,最后被面試官問(wèn)到了這道題:“你看過(guò)哪些開(kāi)源框架的源碼?舉例說(shuō)明一下”。

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站開(kāi)發(fā)、程序開(kāi)發(fā)、微網(wǎng)站、重慶小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(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),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
誠(chéng)然,這是一道比較考驗(yàn)應(yīng)聘者基本功的問(wèn)題,也是很好區(qū)分“好學(xué)生”和“普通學(xué)生”的一道經(jīng)典的開(kāi)放性問(wèn)題。
那這個(gè)問(wèn)題應(yīng)該怎么回答呢?
我這給大家提供兩個(gè)思路吧:
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 來(lái)重寫(xiě)實(shí)現(xiàn),其核心源碼如下:
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 省略初始化過(guò)程...
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);
}
// 忽略其他...
}通過(guò)上述的源碼我們可以看到,請(qǐng)求的核心代碼都在 doDispatch 中,他里面包含的主要執(zhí)行流程有以下這些:
Spring Cloud 組件有很多,你可以挑一個(gè)源碼實(shí)現(xiàn)比較簡(jiǎn)單的組件來(lái)講,這里推薦 Spring Cloud LoadBalancer,因?yàn)槠浜诵脑创a的實(shí)現(xiàn)比較簡(jiǎn)單。
Spring Cloud LoadBalancer 中內(nèi)置了兩種負(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)源碼如下:
// 通過(guò) 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);開(kāi)源框架的源碼在面試中經(jīng)常會(huì)被問(wèn)到,但只因如此,就去完整的看某個(gè)框架的源碼,其實(shí)還是挺難的。第一,框架中的源碼很多,很難一次性看懂。第二,即使能看懂,看完之后也會(huì)很快忘記(因?yàn)閮?nèi)容太多了)。此時(shí),不如挑一些框架中的經(jīng)典實(shí)現(xiàn)源碼來(lái)看,其性價(jià)比更高,既能學(xué)到框架中的精髓,又能搞定面試,是一個(gè)不錯(cuò)的選擇。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流