Solr是作為一個(gè)Servlet運(yùn)行在Tomcat里面的,可以查看Solr的web.xml。
1.web.xml配置由web.xml可以看出,基本上所有Solr的操作都是在SolrDispatchFilter中實(shí)現(xiàn)的。當(dāng)輸入http://localhost:8080/solr/前綴的URL就會(huì)觸發(fā)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的實(shí)現(xiàn)
SolrDispatchFilter繼承了Filter,實(shí)現(xiàn)主要分為三個(gè)接口:init,dofilter,destory。其中init和destory分別在tomcat的啟動(dòng)和關(guān)閉時(shí)候進(jìn)行。
1 /** 2 *初始化,當(dāng)tomcat啟動(dòng)時(shí)候開(kāi)始初始化,其中主要調(diào)用createCoreContainer來(lái)實(shí)現(xiàn)Solr的初始化 3 */ 4 public void init(FilterConfig config) throws ServletException 5 { 6 log.info("SolrDispatchFilter.init()"); 7 8 try { 9 // web.xml configuration10 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 works17 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接口的具體實(shí)現(xiàn),這里主要實(shí)現(xiàn)了主要的Solr功能28 */29 @Override30 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {31 doFilter(request, response, chain, false);32 }33 34 /**35 * 關(guān)閉Solr36 */37 @Override38 public void destroy() {39 if (cores != null) {40 cores.shutdown();41 cores = null;42 } 43 }44 }3.Servlet的實(shí)現(xiàn)通過(guò)查看web.xml以及源碼可以看到,雖然Solr繼承并實(shí)現(xiàn)了Servlet接口,但是Solr的主要操作卻是主要集中在dofilter里面。以RedictServlet為例,它主要實(shí)現(xiàn)了http的重定向功能。從web.xml配置中可以看到,當(dāng)url為solr/admin時(shí)候就會(huì)重定向?yàn)閟olr/#/
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 @Override12 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 key24 if(destination.startsWith(CONTEXT_KEY)) {25 destination = config.getServletContext().getContextPath()26 +destination.substring(CONTEXT_KEY.length());27 }28 }29 30 @Override31 public void doGet(HttpServletRequest req, HttpServletResponse res)32 throws ServletException,IOException {33 34 res.setStatus(code);35 res.setHeader("Location", destination);36 }37 38 }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注