年初,公司开发法律行业的搜索引擎。当时,我作为整个系统的核心成员,选择solr,并在solr根据我们的要求做了相应的二次开发。但是,对solr的还没有进行认真仔细的研究。最近,事情比较清闲,翻翻solr的源码,加深对solr的认识。在博客园上看到Ryan的Solr4.8.0源码分析(http://www.cnblogs.com/rcfeng/),跟着前人的脚步学习一下,并把5.0版本改动后的源码做一点补充。有什么不妥的地方,请Ryan谅解,或者联系我 QQ:503172601

  5.0相对于4.8版本,扩充了cloud的功能。我们以tomcat容器为例,先看SolrDispatchFilter的实现。

  1.   SolrDispatchFilter的实现

BaseSolrFilter实现了Filter接口。

abstract class BaseSolrFilter implements Filter {

  static {
CheckLoggingConfiguration.check();
} }

  再看CheckLoggingConfiguration代码,主要是进行SLF4j logging jars校验,很简单没啥可说的。

 final class CheckLoggingConfiguration {

   static void check() {
try {
LoggerFactory.getLogger(CheckLoggingConfiguration.class);
} catch (NoClassDefFoundError e) {
throw new NoClassDefFoundError("Failed to initialize Apache Solr: "
+"Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in "
+"the jetty lib/ext directory. For other containers, the corresponding directory should be used. "
+"For more information, see: http://wiki.apache.org/solr/SolrLogging");
}
} private CheckLoggingConfiguration() {} }

  SolrDispatchFilter继承BaseSolrFilter,并且solr要求所有solr filter不要直接实现Filter接口,都要通过继承BaseSolrFilter。SolrDispatchFilter重写了三个方法:init,dofilter,destory。其中init和destory分别在tomcat的启动和关闭时候运行;doFilter处理用户的http请求像select查询等,放到后面来说。

2.  Solr的启动

tomcat的启动的时候,会运行init方法,我们先来看看init方法

public void init(FilterConfig config) throws ServletException
{ try {
// web.xml configuration
this.pathPrefix = config.getInitParameter( "path-prefix" ); Properties extraProperties = (Properties) config.getServletContext().getAttribute(PROPERTIES_ATTRIBUTE);
if (extraProperties == null)
extraProperties = new Properties(); String solrHome = (String) config.getServletContext().getAttribute(SOLRHOME_ATTRIBUTE);
if (solrHome == null)
solrHome = SolrResourceLoader.locateSolrHome(); this.cores = createCoreContainer(solrHome, extraProperties); log.info("user.dir=" + System.getProperty("user.dir"));
}
catch( Throwable t ) {
// catch this so our filter still works
log.error( "Could not start Solr. Check solr/home property and the logs");
SolrCore.log( t );
if (t instanceof Error) {
throw (Error) t;
}
} log.info("SolrDispatchFilter.init() done");
}

  我们看到,先从web.xml读取path-prefix的属性值;

   (1)然后获取solrhome。

    在SolrResourceLoader.locateSolrHome()方法里通过三种方式获取solrhome

    1. JNDI: via java:comp/env/solr/home
    2. The system property solr.solr.home
    3. Look in the current working directory for a solr/ directory

   (2)然后调用createCoreContainer来实现Solr的初始化。

 protected CoreContainer createCoreContainer(String solrHome,           Properties extraProperties) {
NodeConfig nodeConfig = loadNodeConfig(solrHome, extraProperties);
cores = new CoreContainer(nodeConfig, extraProperties);
cores.load();
return cores;
}

(3) 类加载器SolrResourceLoader

    solr的初始化主要是loadNodeConfig方法。我们来看loadNodeConfig方法做了什么?

创建SolrResourceLoader,代码如下:

public SolrResourceLoader( String instanceDir, ClassLoader parent, Properties coreProperties )
{
if( instanceDir == null ) {
this.instanceDir = SolrResourceLoader.locateSolrHome();
log.info("new SolrResourceLoader for deduced Solr Home: '{}'",
this.instanceDir);
} else{
this.instanceDir = normalizeDir(instanceDir);
log.info("new SolrResourceLoader for directory: '{}'",
this.instanceDir);
} this.classLoader = createClassLoader(null, parent);
addToClassLoader("./lib/", null, true);
reloadLuceneSPI();
this.coreProperties = coreProperties;
}

    SolrResourceLoader主要是做了3个事情

    创建类装载器,加载lib目录下的类,装在LuceneSPI。

    然后

   (4)解析solr.xml文件

解析solr.xml文件,通过sorl.xml的地方从本地或者zookeeper的获取solr.xml文件。

然后调用SolrXmlConfig.fromSolrHome和SolrXmlConfig.fromInputStream解析solr.xml文件封装为NodeConfig。

(5)实例化一个CoreContainer,通过CoreContainer来加载cores

Solr5.0源码分析-SolrDispatchFilter的更多相关文章

  1. Solr4.8.0源码分析(25)之SolrCloud的Split流程

    Solr4.8.0源码分析(25)之SolrCloud的Split流程(一) 题记:昨天有位网友问我SolrCloud的split的机制是如何的,这个还真不知道,所以今天抽空去看了Split的原理,大 ...

  2. AFNetWorking3.0源码分析

    分析: AFNetWorking(3.0)源码分析(一)——基本框架 AFNetworking源码解析 AFNetworking2.0源码解析<一> end

  3. Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五)

    Solr4.8.0源码分析(24)之SolrCloud的Recovery策略(五) 题记:关于SolrCloud的Recovery策略已经写了四篇了,这篇应该是系统介绍Recovery策略的最后一篇了 ...

  4. Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四)

    Solr4.8.0源码分析(23)之SolrCloud的Recovery策略(四) 题记:本来计划的SolrCloud的Recovery策略的文章是3篇的,但是没想到Recovery的内容蛮多的,前面 ...

  5. Solr4.8.0源码分析(22)之SolrCloud的Recovery策略(三)

    Solr4.8.0源码分析(22)之SolrCloud的Recovery策略(三) 本文是SolrCloud的Recovery策略系列的第三篇文章,前面两篇主要介绍了Recovery的总体流程,以及P ...

  6. Solr4.8.0源码分析(21)之SolrCloud的Recovery策略(二)

    Solr4.8.0源码分析(21)之SolrCloud的Recovery策略(二) 题记:  前文<Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一)>中提 ...

  7. Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一)

    Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一) 题记: 我们在使用SolrCloud中会经常发现会有备份的shard出现状态Recoverying,这就表明Solr ...

  8. Solr4.8.0源码分析(14)之SolrCloud索引深入(1)

    Solr4.8.0源码分析(14) 之 SolrCloud索引深入(1) 上一章节<Solr In Action 笔记(4) 之 SolrCloud分布式索引基础>简要学习了SolrClo ...

  9. Solr4.8.0源码分析(15) 之 SolrCloud索引深入(2)

    Solr4.8.0源码分析(15) 之 SolrCloud索引深入(2) 上一节主要介绍了SolrCloud分布式索引的整体流程图以及索引链的实现,那么本节开始将分别介绍三个索引过程即LogUpdat ...

随机推荐

  1. doctrine2到底是个什么玩意

    之前和最近一个项目用到了Doctrine,由于是别人搭建的,自己没有很了解,最近又开始做的时候发现拙荆见肘,于是看了一下doctrine教程,本文就是加上自己理解的doctrine教程文档笔记了. D ...

  2. MVC中Action之间传值

    一  MVCAction之间的传值 之前一直觉得关于MVC里面的传值,只能从<视图—>Action>,和<Actoin->视图>但是今天在项目里面需要实现将几个视图 ...

  3. ajax请求跨域问题

    ajax跨域,这个是面试的时候常被问到,也是在做项目的时候会遇到的问题,在之前的项目中就有遇到过,这里根据经验写了三种分享下 1.使用中间层过渡的方式 简单来说就是"后台代理",把 ...

  4. 很震撼的HTML5视频播放器,电影院的感觉

    效果很震撼!有电影院的感觉了.呵呵. 看了下代码,依然是 在一个canvas里嵌入<video>然后getImageData 点击这里查看效果 代码: var canvas = docum ...

  5. 利用PBfunc在Powerbuilder中使用https获取微信的AccessToken

    在前篇中讲解了使用PBFunc在Powerbuilder自己进行http的GET和POST操作. 本篇简单用代码演示下https的微信AccessToken的获取: n_pbfunc_http lnv ...

  6. ahjesus动态生成表达式树

    直接上方法,看的懂的拿去用,看不懂的找资料看懂 , , Double floorprice = , Double topprice = , string brandstr = "" ...

  7. sublimeCodeIntel 的配置

    在项目的根目录目录下建立.codeintel/config 但是在windows 需要进入dos 环境下建立.以点开头的文件夹和文件.资源管理器不允许创建点开头的文件或文件夹,但在命令提示符下是可以的 ...

  8. [.NET] SQL数据总笔数查询

    [.NET] SQL数据总笔数查询 程序下载 范例下载:点此下载 原始码下载:点此下载 NuGet封装:点此下载 数据查询 开发系统时,使用C#执行SQL查询指令,就可以从SQL数据库里查询所需数据. ...

  9. ArrayList 与 LinkedList

    ArrayList:数组结构,插入删除的效率低,查询的效率较高. LinkedList:链接数据结构,插入删除的效率较高,查询的效率低. 两者的使用 ArrayList:适合用作添加数据,做查询. L ...

  10. C# 如何使用 svcutil.exe 创建 WCF 客户端代码

    工具:svcutil.exe 参数:指定wsdl.输出源码文件.输出配置文件 示例: D:\>svcutil.exe http://localhost:8087/DataService/?wsd ...