01、Solr源码:Solr的Servlet

Solr是作为一个Servlet运行在Tomcat里面的,可以查看Solr的web.xml。

1.web.xml配置

由web.xml可以看出,基本上所有Solr的操作都是在SolrDispatchFilter中实现的。当输入http://localhost:8080/solr/前缀的URL就会触发SolrDispatchFilter.

 1   <filter>
 2     <filter-name>SolrRequestFilter</filter-name>
 3     <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>
 4     <init-param>
 5       <param-name>path-prefix</param-name>
 6       <param-value>/xxx</param-value>
 7     </init-param>
 8   </filter>
 9 
10   <filter-mapping>
11     <filter-name>SolrRequestFilter</filter-name>
12     <url-pattern>/*</url-pattern>
13   </filter-mapping>
14   <servlet>
15     <servlet-name>RedirectOldAdminUI</servlet-name>
16     <servlet-class>org.apache.solr.servlet.RedirectServlet</servlet-class>
17     <init-param>
18       <param-name>destination</param-name>
19       <param-value>${context}/#/</param-value>
20     </init-param>
21   </servlet>
22   <servlet-mapping>
23     <servlet-name>RedirectOldAdminUI</servlet-name>
24     <url-pattern>/admin/</url-pattern>
25   </servlet-mapping>

2. SolrDispatchFilter的实现

SolrDispatchFilter继承了Filter,实现主要分为三个接口:init,dofilter,destory。其中init和destory分别在tomcat的启动和关闭时候进行。

 1  /**
 2   *初始化,当tomcat启动时候开始初始化,其中主要调用createCoreContainer来实现Solr的初始化
 3   */
 4 public void init(FilterConfig config) throws ServletException
 5   {
 6     log.info("SolrDispatchFilter.init()");
 7 
 8     try {
 9       // web.xml configuration
10       this.pathPrefix = config.getInitParameter( "path-prefix" );
11 
12       this.cores = createCoreContainer();
13       log.info("user.dir=" + System.getProperty("user.dir"));
14     }
15     catch( Throwable t ) {
16       // catch this so our filter still works
17       log.error( "Could not start Solr. Check solr/home property and the logs");
18       SolrCore.log( t );
19       if (t instanceof Error) {
20         throw (Error) t;
21       }
22     }
23 
24     log.info("SolrDispatchFilter.init() done");
25   }
26   /**
27    * filter接口的具体实现,这里主要实现了主要的Solr功能
28    */
29   @Override
30   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
31     doFilter(request, response, chain, false);
32   }
33 
34   /**
35    * 关闭Solr
36    */
37   @Override
38   public void destroy() {
39     if (cores != null) {
40       cores.shutdown();
41       cores = null;
42     }    
43   }
44 }

3.Servlet的实现

通过查看web.xml以及源码可以看到,虽然Solr继承并实现了Servlet接口,但是Solr的主要操作却是主要集中在dofilter里面。以RedictServlet为例,它主要实现了http的重定向功能。从web.xml配置中可以看到,当url为solr/admin时候就会重定向为solr/#/

 1 /**
 2  * A Simple redirection servlet to help us deprecate old UI elements
 3  */
 4 public class RedirectServlet extends BaseSolrServlet {
 5   
 6   static final String CONTEXT_KEY = "${context}";
 7   
 8   String destination;
 9   int code = HttpServletResponse.SC_MOVED_PERMANENTLY;
10   
11   @Override
12   public void init(ServletConfig config) throws ServletException {
13     super.init(config);
14     
15     destination = config.getInitParameter("destination");
16     if(destination==null) {
17       throw new ServletException("RedirectServlet missing destination configuration");
18     }
19     if( "false".equals(config.getInitParameter("permanent") )) {
20       code = HttpServletResponse.SC_MOVED_TEMPORARILY;
21     }
22         // 获取重定向的url 解析init-param 获取destination值
23     // Replace the context key
24     if(destination.startsWith(CONTEXT_KEY)) {
25       destination = config.getServletContext().getContextPath()
26           +destination.substring(CONTEXT_KEY.length());
27     }
28   }
29   
30   @Override
31   public void doGet(HttpServletRequest req, HttpServletResponse res)
32           throws ServletException,IOException {
33       
34     res.setStatus(code);
35     res.setHeader("Location", destination);
36   }
37 
38 }

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: