|
Solr是作为一个Servlet运行在Tomcat里面的,可以查看Solr的web.xml。
1.web.xml配置
由web.xml可以看出,基本上所有Solr的操作都是在SolrDispatchFilter中实现的。当输入http://localhost:8080/solr/前缀的URL就会触发SolrDispatchFilter.
1
2 SolrRequestFilter
3 org.apache.solr.servlet.SolrDispatchFilter
4
5 path-prefix
6 /xxx
7
8
9
10
11 SolrRequestFilter
12 /*
13
14
15 RedirectOldAdminUI
16 org.apache.solr.servlet.RedirectServlet
17
18 destination
19 ${context}/#/
20
21
22
23 RedirectOldAdminUI
24 /admin/
25
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 }
|
|
|