日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

阿里面試:看過框架源碼嗎?舉例說明一下

來源: 責編: 時間:2023-11-10 09:15:52 292觀看
導讀前兩天有朋友面試“淘汰集團”,也就是“淘寶”+“天貓”的組合,最后被面試官問到了這道題:“你看過哪些開源框架的源碼?舉例說明一下”。誠然,這是一道比較考驗應聘者基本功的問題,也是很好區(qū)分“好學生”和“普通學生”的

前兩天有朋友面試“淘汰集團”,也就是“淘寶”+“天貓”的組合,最后被面試官問到了這道題:“你看過哪些開源框架的源碼?舉例說明一下”。Fhg28資訊網(wǎng)——每日最新資訊28at.com

誠然,這是一道比較考驗應聘者基本功的問題,也是很好區(qū)分“好學生”和“普通學生”的一道經(jīng)典的開放性問題。Fhg28資訊網(wǎng)——每日最新資訊28at.com

那這個問題應該怎么回答呢?Fhg28資訊網(wǎng)——每日最新資訊28at.com

解答思路

我這給大家提供兩個思路吧:Fhg28資訊網(wǎng)——每日最新資訊28at.com

  1. 可以回答比較常見的,你比較熟悉的源碼,例如 Spring Boot 收到請求之后,執(zhí)行流程的源碼。
  2. 還可以回答 Spring Cloud 微服務中,某個組件執(zhí)行的流程源碼,這樣能很好的體現(xiàn)你對微服務比較熟悉,因為微服務在公司中應用比較廣泛,所以回答的好,是一個極大的加分項。

1.Spring Boot 源碼分析

Spring Boot 在收到請求之后,會先執(zhí)行前端控制器 DispatcherServlet,并調(diào)用其父類 FrameworkServlet 中的 service 方法,其核心源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

/** * Override the parent class implementation in order to intercept PATCH requests. */@Overrideprotected 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 實現(xiàn)源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

protected final void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 省略一堆初始化配置      try {       // 真正執(zhí)行邏輯的方法       doService(request, response);   }   catch (ServletException | IOException ex) {       ...   }}

doService 實現(xiàn)源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

protected abstract void doService(HttpServletRequest request, HttpServletResponse response) throws Exception;

doService 是抽象方法,由其之類 DispatcherServlet 來重寫實現(xiàn),其核心源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

@Overrideprotected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {    // 省略初始化過程...    try {        doDispatch(request, response);    }    finally {  // 省略其他...    }}

此時就進入到了 DispatcherServlet 中的 doDispatch 方法了:Fhg28資訊網(wǎng)——每日最新資訊28at.com

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {    // 獲取原生請求    HttpServletRequest processedRequest = request;    // 獲取Handler執(zhí)行鏈    HandlerExecutionChain mappedHandler = null;    // 是否為文件上傳請求, 默認為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í)行對應方法            mv = ha.handle(processedRequest, response, mappedHandler.getHandler());            if (asyncManager.isConcurrentHandlingStarted()) {                return;            }            applyDefaultViewName(processedRequest, mv);            mappedHandler.applyPostHandle(processedRequest, response, mv);        }        // 忽略其他...}

通過上述的源碼我們可以看到,請求的核心代碼都在 doDispatch 中,他里面包含的主要執(zhí)行流程有以下這些:Fhg28資訊網(wǎng)——每日最新資訊28at.com

  1. 調(diào)用 HandlerExecutionChain 獲取處理器:DispatcherServlet 首先調(diào)用 getHandler 方法,通過 HandlerMapping 獲取請求對應的 HandlerExecutionChain 對象,包含了處理器方法和攔截器列表。
  2. 調(diào)用 HandlerAdapter 執(zhí)行處理器方法:DispatcherServlet 使用 HandlerAdapter 來執(zhí)行處理器方法。根據(jù) HandlerExecutionChain 中的處理器方法類型不同,選擇對應的 HandlerAdapter 進行處理。常用的適配器有 RequestMappingHandlerAdapter 和 HttpRequestHandlerAdapter。
  3. 解析請求參數(shù):DispatcherServlet 調(diào)用 HandlerAdapter 的 handle 方法,解析請求參數(shù),并將解析后的參數(shù)傳遞給處理器方法執(zhí)行。
  4. 調(diào)用處理器方法:DispatcherServlet 通過反射機制調(diào)用處理器方法,執(zhí)行業(yè)務邏輯。
  5. 處理攔截器:在調(diào)用處理器方法前后,DispatcherServlet 會調(diào)用攔截器的 preHandle 和 postHandle方法進行相應的處理。
  6. 渲染視圖:處理器方法執(zhí)行完成后,DispatcherServlet 會通過 ViewResolver 解析視圖名稱,找到對應的 View 對象,并將模型數(shù)據(jù)傳遞給 View 進行渲染。
  7. 生成響應:View 會將渲染后的視圖內(nèi)容生成響應數(shù)據(jù)。

2.Spring Cloud 源碼

Spring Cloud 組件有很多,你可以挑一個源碼實現(xiàn)比較簡單的組件來講,這里推薦 Spring Cloud LoadBalancer,因為其核心源碼的實現(xiàn)比較簡單。Fhg28資訊網(wǎng)——每日最新資訊28at.com

Spring Cloud LoadBalancer 中內(nèi)置了兩種負載均衡策略:Fhg28資訊網(wǎng)——每日最新資訊28at.com

  1. 輪詢負載均衡策略
  2. 隨機負載均衡策略

輪詢負載均衡策略的核心實現(xiàn)源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

// ++i 去負數(shù),得到一個正數(shù)值int pos = this.position.incrementAndGet() & Integer.MAX_VALUE;// 正數(shù)值和服務實例個數(shù)取余 -> 實現(xiàn)輪詢ServiceInstance instance = (ServiceInstance)instances.get(pos % instances.size());// 將實例返回給調(diào)用者return new DefaultResponse(instance);

隨機負載均衡策略的核心實現(xiàn)源碼如下:Fhg28資訊網(wǎng)——每日最新資訊28at.com

// 通過 ThreadLocalRandom 獲取一個隨機數(shù),最大值為服務實例的個數(shù)int index = ThreadLocalRandom.current().nextInt(instances.size());// 得到實例ServiceInstance instance = (ServiceInstance)instances.get(index);// 返回return new DefaultResponse(instance);

小結

開源框架的源碼在面試中經(jīng)常會被問到,但只因如此,就去完整的看某個框架的源碼,其實還是挺難的。第一,框架中的源碼很多,很難一次性看懂。第二,即使能看懂,看完之后也會很快忘記(因為內(nèi)容太多了)。此時,不如挑一些框架中的經(jīng)典實現(xiàn)源碼來看,其性價比更高,既能學到框架中的精髓,又能搞定面試,是一個不錯的選擇。Fhg28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-19925-0.html阿里面試:看過框架源碼嗎?舉例說明一下

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 十個使用Spring Cloud和Java創(chuàng)建微服務的實踐案例

下一篇: ?Gorm 中的鉤子和回調(diào)

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 永城市| 霸州市| 醴陵市| 阳东县| 瑞金市| 东源县| 临汾市| 广灵县| 精河县| 镇雄县| 鹤壁市| 兴化市| 务川| 沙湾县| 武隆县| 邹城市| 宣城市| 元江| 大邑县| 彭阳县| 新乡县| 永州市| 陈巴尔虎旗| 宜川县| 建昌县| 天柱县| 咸丰县| 勐海县| 调兵山市| 密山市| 云阳县| 揭东县| 遵义县| 来宾市| 玉溪市| 庐江县| 松原市| 南汇区| 北京市| 伊通| 疏勒县|