Tomcat是怎样处理搜索引擎爬虫请求的?

开发 开发工具
每个置身于互联网中的站点,都需要搜索引擎的收录,以及在适时在结果中的展现,从而将信息提供给用户、读者。而搜索引擎如何才能收录我们的站点呢?

每个置身于互联网中的站点,都需要搜索引擎的收录,以及在适时在结果中的展现,从而将信息提供给用户、读者。而搜索引擎如何才能收录我们的站点呢?

这就涉及到一个「搜索引擎的爬虫」爬取站点内容的过程。只有被搜索引擎爬过并收录的内容才有机会在特定query***之后在结果中展现。

[[233840]]

这些搜索引擎内容的工具,又被称为爬虫、Sprider,Web crawler 等等。我们一方面欢迎其访问站点以便收录内容,一方面又因其对于正常服务的影响头疼。毕竟 Spider 也是要占用服务器资源的, Spider 太多太频繁的资源占用,正常用户请求处理就会受到影响。所以一些站点干脆直接为搜索引擎提供了单独的服务供其访问,其他正常的用户请求走另外的服务器。

说到这里需要提一下,对于是否是 Spider 的请求识别,是通过HTTP 请求头中的User-Agent 字段来判断的,每个搜索引擎有自己的独立标识。而且通过这些内容,管理员也可以在访问日志中了解搜索引擎爬过哪些内容。

此外,在对搜索引擎的「爬取声明文件」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,我们在服务端得以识别一个具体的用户。那 Spider 的大量请求到达后,如果访问频繁同时请求量大时,就需要创建巨大量的 Session,需要占用和消耗很多内存,这无形中占用了正常用户处理的资源。

为此, Tomcat 提供了一个 「Valve」,用于对 Spider 的请求做一些处理。

首先识别 Spider 请求,对于 Spider 请求,使其使用相同的 SessionId继续后面的请求流程,从而避免创建大量的 Session 数据。

这里需要注意,即使Spider显式的传了一个 sessionId过来,也会弃用,而是根据client Ip 来进行判断,即对于 相同的 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<String> 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的时候进行compile 
  4. uaPattern = Pattern.compile(crawlerUserAgents); 

这样当 Spider 到达的时候就能通过 User-agent识别出来并进行特别处理从而减小受其影响。

这个 Valve的名字是:「CrawlerSessionManagerValve」,好名字一眼就能看出来作用。

其他还有问题么?我们看看,通过ClientIp来判断进行Session共用。

最近 Tomcat 做了个bug fix,原因是这种通过ClientIp的判断方式,当 Valve 配置在Engine下层,给多个Host 共用时,只能有一个Host生效。 fix之后,对于请求除ClientIp外,还有Host和 Context的限制,这些元素共同组成了 client标识,就能更大程度上共用Session。

修改内容如下:

总结下, 该Valve 通过标识识别出 Spider 请求后,给其分配一个固定的Session,从而避免大量的Session创建导致我资源占用。

默认该Valve未开启,需要在 server.xml中 增加配置开启。另外我们看上面提供的 正则 pattern,和taobao 的robots.txt对比下,你会出现并没有包含国内的这些搜索引擎的处理,这个时候怎么办呢?

在配置的时候传一下进来就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.     } 

 【本文为51CTO专栏作者“侯树成”的原创稿件,转载请通过作者微信公众号『Tomcat那些事儿』获取授权】

戳这里,看该作者更多好文

责任编辑:赵宁宁 来源: 51CTO专栏
相关推荐

2019-09-26 09:34:51

网络爬虫搜索引擎大数据

2014-08-05 15:10:05

Larbin搜索引擎

2011-06-20 18:23:06

SEO

2010-08-02 16:08:39

ibmdwJava搜索引擎

2016-08-18 00:54:59

Python图片处理搜索引擎

2011-05-17 17:40:59

搜索引擎信任度

2009-02-19 09:41:36

搜索引擎搜狐百度

2009-09-22 16:23:52

搜索引擎

2011-05-10 12:55:13

反向链接

2011-05-10 12:32:56

搜索引擎反向链接

2017-08-07 08:15:31

搜索引擎倒排

2020-03-20 10:14:49

搜索引擎倒排索引

2010-06-13 16:27:28

搜索引擎

2016-12-26 13:41:19

大数据搜索引擎工作原理

2012-09-07 13:22:21

搜索搜狗

2022-10-08 09:13:18

搜索引擎⽹站

2010-04-20 11:43:46

2020-02-24 08:52:08

开源索引YaCy

2015-08-31 10:41:58

搜索引擎Google云应用

2012-05-14 11:01:50

搜索引擎微软
点赞
收藏

51CTO技术栈公众号