av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

Tomcat是怎樣處理搜索引擎爬蟲請求的?

每個置身于互聯(lián)網(wǎng)中的站點(diǎn),都需要搜索引擎的收錄,以及在適時在結(jié)果中的展現(xiàn),從而將信息提供給用戶、讀者。而搜索引擎如何才能收錄我們的站點(diǎn)呢?

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了萬源免費(fèi)建站歡迎大家使用!

這就涉及到一個「搜索引擎的爬蟲」爬取站點(diǎn)內(nèi)容的過程。只有被搜索引擎爬過并收錄的內(nèi)容才有機(jī)會在特定query***之后在結(jié)果中展現(xiàn)。

這些搜索引擎內(nèi)容的工具,又被稱為爬蟲、Sprider,Web crawler 等等。我們一方面歡迎其訪問站點(diǎn)以便收錄內(nèi)容,一方面又因其對于正常服務(wù)的影響頭疼。畢竟 Spider 也是要占用服務(wù)器資源的, Spider 太多太頻繁的資源占用,正常用戶請求處理就會受到影響。所以一些站點(diǎn)干脆直接為搜索引擎提供了單獨(dú)的服務(wù)供其訪問,其他正常的用戶請求走另外的服務(wù)器。

說到這里需要提一下,對于是否是 Spider 的請求識別,是通過HTTP 請求頭中的User-Agent 字段來判斷的,每個搜索引擎有自己的獨(dú)立標(biāo)識。而且通過這些內(nèi)容,管理員也可以在訪問日志中了解搜索引擎爬過哪些內(nèi)容。

此外,在對搜索引擎的「爬取聲明文件」robots.txt中,也會有類似的User-agent 描述。比如下面是taobao 的robots.txt描述

 
 
 
 
  1. User-agent:  Baiduspider 
  2. Allow:  /article 
  3. Allow:  /oshtml 
  4. Disallow:  /product/ 
  5. Disallow:  / 
  6.  
  7. User-Agent:  Googlebot 
  8. Allow:  /article 
  9. Allow:  /oshtml 
  10. Allow:  /product 
  11. Allow:  /spu 
  12. Allow:  /dianpu 
  13. Allow:  /oversea 
  14. Allow:  /list 
  15. Disallow:  / 
  16.  
  17. User-agent:  Bingbot 
  18. Allow:  /article 
  19. Allow:  /oshtml 
  20. Allow:  /product 
  21. Allow:  /spu 
  22. Allow:  /dianpu 
  23. Allow:  /oversea 
  24. Allow:  /list 
  25. Disallow:  / 
  26.  
  27. User-Agent:  360Spider 
  28. Allow:  /article 
  29. Allow:  /oshtml 
  30. Disallow:  / 
  31.  
  32. User-Agent:  Yisouspider 
  33. Allow:  /article 
  34. Allow:  /oshtml 
  35. Disallow:  / 
  36.  
  37. User-Agent:  Sogouspider 
  38. Allow:  /article 
  39. Allow:  /oshtml 
  40. Allow:  /product 
  41. Disallow:  / 
  42.  
  43. User-Agent:  Yahoo!  Slurp 
  44. Allow:  /product 
  45. Allow:  /spu 
  46. Allow:  /dianpu 
  47. Allow:  /oversea 
  48. Allow:  /list 
  49. Disallow:  / 

我們再來看 Tomcat對于搜索引擎的請求做了什么特殊處理呢?

對于請求涉及到 Session,我們知道通過 Session,我們在服務(wù)端得以識別一個具體的用戶。那 Spider 的大量請求到達(dá)后,如果訪問頻繁同時請求量大時,就需要創(chuàng)建巨大量的 Session,需要占用和消耗很多內(nèi)存,這無形中占用了正常用戶處理的資源。

為此, Tomcat 提供了一個 「Valve」,用于對 Spider 的請求做一些處理。

首先識別 Spider 請求,對于 Spider 請求,使其使用相同的 SessionId繼續(xù)后面的請求流程,從而避免創(chuàng)建大量的 Session 數(shù)據(jù)。

這里需要注意,即使Spider顯式的傳了一個 sessionId過來,也會棄用,而是根據(jù)client Ip 來進(jìn)行判斷,即對于 相同的 Spider 只提供一個Session。

我們來看代碼:

 
 
 
 
  1. // If the incoming request has a valid session ID, no action is required 
  2. if (request.getSession(false) == null) { 
  3.  
  4.     // Is this a crawler - check the UA headers 
  5.     Enumeration uaHeaders = request.getHeaders("user-agent"); 
  6.     String uaHeader = null; 
  7.     if (uaHeaders.hasMoreElements()) { 
  8.         uaHeader = uaHeaders.nextElement(); 
  9.     } 
  10.  
  11.     // If more than one UA header - assume not a bot 
  12.     if (uaHeader != null && !uaHeaders.hasMoreElements()) { 
  13.         if (uaPattern.matcher(uaHeader).matches()) { 
  14.             isBot = true; 
  15.             if (log.isDebugEnabled()) { 
  16.                 log.debug(request.hashCode() + 
  17.                         ": Bot found. UserAgent=" + uaHeader); 
  18.             } 
  19.         } 
  20.     } 
  21.  
  22.     // If this is a bot, is the session ID known? 
  23.     if (isBot) { 
  24.         clientIp = request.getRemoteAddr(); 
  25.         sessionId = clientIpSessionId.get(clientIp); 
  26.         if (sessionId != null) { 
  27.             request.setRequestedSessionId(sessionId); // 重用session 
  28.         } 
  29.     } 
  30.  
  31. getNext().invoke(request, response); 
  32.  
  33. if (isBot) { 
  34.     if (sessionId == null) { 
  35.         // Has bot just created a session, if so make a note of it 
  36.         HttpSession s = request.getSession(false); 
  37.         if (s != null) { 
  38.             clientIpSessionId.put(clientIp, s.getId()); //針對Spider生成session 
  39.             sessionIdClientIp.put(s.getId(), clientIp); 
  40.             // #valueUnbound() will be called on session expiration 
  41.             s.setAttribute(this.getClass().getName(), this); 
  42.             s.setMaxInactiveInterval(sessionInactiveInterval); 
  43.  
  44.             if (log.isDebugEnabled()) { 
  45.                 log.debug(request.hashCode() + 
  46.                         ": New bot session. SessionID=" + s.getId()); 
  47.             } 
  48.         } 
  49.     } else { 
  50.         if (log.isDebugEnabled()) { 
  51.             log.debug(request.hashCode() + 
  52.                     ": Bot session accessed. SessionID=" + sessionId); 
  53.         } 
  54.     } 

判斷Spider 是通過正則

 
 
 
 
  1. private String crawlerUserAgents = 
  2.     ".*[bB]ot.*|.*Yahoo! Slurp.*|.*Feedfetcher-Google.*"; 
  3. // 初始化Valve的時候進(jìn)行compile 
  4. uaPattern = Pattern.compile(crawlerUserAgents); 

這樣當(dāng) Spider 到達(dá)的時候就能通過 User-agent識別出來并進(jìn)行特別處理從而減小受其影響。

這個 Valve的名字是:「CrawlerSessionManagerValve」,好名字一眼就能看出來作用。

其他還有問題么?我們看看,通過ClientIp來判斷進(jìn)行Session共用。

最近 Tomcat 做了個bug fix,原因是這種通過ClientIp的判斷方式,當(dāng) Valve 配置在Engine下層,給多個Host 共用時,只能有一個Host生效。 fix之后,對于請求除ClientIp外,還有Host和 Context的限制,這些元素共同組成了 client標(biāo)識,就能更大程度上共用Session。

修改內(nèi)容如下:

總結(jié)下, 該Valve 通過標(biāo)識識別出 Spider 請求后,給其分配一個固定的Session,從而避免大量的Session創(chuàng)建導(dǎo)致我資源占用。

默認(rèn)該Valve未開啟,需要在 server.xml中 增加配置開啟。另外我們看上面提供的 正則 pattern,和taobao 的robots.txt對比下,你會出現(xiàn)并沒有包含國內(nèi)的這些搜索引擎的處理,這個時候怎么辦呢?

在配置的時候傳一下進(jìn)來就OK啦,這是個public 的屬性

 
 
 
 
  1. public void setCrawlerUserAgents(String crawlerUserAgents) { 
  2.     this.crawlerUserAgents = crawlerUserAgents; 
  3.     if (crawlerUserAgents == null || crawlerUserAgents.length() == 0) { 
  4.         uaPattern = null; 
  5.     } else { 
  6.         uaPattern = Pattern.compile(crawlerUserAgents); 
  7.     } 

 【本文為專欄作者“侯樹成”的原創(chuàng)稿件,轉(zhuǎn)載請通過作者微信公眾號『Tomcat那些事兒』獲取授權(quán)】


分享標(biāo)題:Tomcat是怎樣處理搜索引擎爬蟲請求的?
當(dāng)前網(wǎng)址:http://uogjgqi.cn/article/dhisiod.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流