上文写到Solr的启动过程是在SolrDispatchFilter的init()里实现,当Tomcat启动时候会自动调用init();

Solr的启动主要在 this.cores = createCoreContainer();语句中实现。

/**
*初始化,当tomcat启动时候开始初始化,其中主要调用createCoreContainer来实现Solr的初始化
*/
public void init(FilterConfig config) throws ServletException
{
log.info("SolrDispatchFilter.init()"); try {
// web.xml configuration
this.pathPrefix = config.getInitParameter( "path-prefix" ); this.cores = createCoreContainer();
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");
}

Solr的启动过程主要包括:

1. 获取SolrHome:分别先后通过JNDI,System property,default directory三种方式尝试获取
2. 实例化启动过程中使用的类加载器SolrResourceLoader
3. 加载solrhome下的solr.xml文件,封装为ConfigSolr
4. 实例化一个CoreContainer,通过CoreContainer来加载cores
5. 遍历SolrHome,当寻找到含有core.properties的文件夹,则作为一个core
6. 多线程加载cores
7. 加载每个core时先加载solrconfig.xml封装为SolrConfig
8. 再加载schema.xml封装为IndexSchema
9. 最后实例化SolrCore

以下是createCoreContainer()实现代码,该函数就包含了以上的启动过程。

   /**
* Override this to change CoreContainer initialization
* @return a CoreContainer to hold this server's cores
*/
protected CoreContainer createCoreContainer() {
SolrResourceLoader loader = new SolrResourceLoader(SolrResourceLoader.locateSolrHome());
ConfigSolr config = loadConfigSolr(loader);
CoreContainer cores = new CoreContainer(loader, config);
cores.load();
return cores;
}

1. 获取SolrHome信息

Solr通过三种方式来获取SolrHome,分别是JNDI,System property,以及当前路径。

JNDI的java:comp/env/solr/home的获取,主要通过在tomcat/conf/Catalina/localhost目录下新建solr.xml。Tomcat启动的时候就会该目录下自动获取  SolrHome的信息.

System property的SolrHome获取是直接在tomcat启动脚本catalina.sh中加入set JAVA_OPTS -Dsolr.solr.home=/Users/rcf/workspace/java/solr 。

最后的获取SolrHome的方式就是在当前目录下查找。

   /**
* Determines the solrhome from the environment.
* Tries JNDI (java:comp/env/solr/home) then system property (solr.solr.home);
* if both fail, defaults to solr/
* @return the instance directory name
*/
/**
* Finds the solrhome based on looking up the value in one of three places:
* <ol>
* <li>JNDI: via java:comp/env/solr/home</li>
* <li>The system property solr.solr.home</li>
* <li>Look in the current working directory for a solr/ directory</li>
* </ol>
*
* The return value is normalized. Normalization essentially means it ends in a trailing slash.
* @return A normalized solrhome
* @see #normalizeDir(String)
*/
public static String locateSolrHome() { String home = null;
// Try JNDI
try {
Context c = new InitialContext();
home = (String)c.lookup("java:comp/env/"+project+"/home");
log.info("Using JNDI solr.home: "+home );
} catch (NoInitialContextException e) {
log.info("JNDI not configured for "+project+" (NoInitialContextEx)");
} catch (NamingException e) {
log.info("No /"+project+"/home in JNDI");
} catch( RuntimeException ex ) {
log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage());
} // Now try system property
if( home == null ) {
String prop = project + ".solr.home";
home = System.getProperty(prop);
if( home != null ) {
log.info("using system property "+prop+": " + home );
}
} // if all else fails, try
if( home == null ) {
home = project + '/';
log.info(project + " home defaulted to '" + home + "' (could not find system property or JNDI)");
}
return normalizeDir( home );
}
 <Context docBase="/Users/rcf/workspace/java/tomcat/apache-tomcat-8.0.9/webapps/solr.war" debug="0" crossContext="true" >
<Environment name="solr/home" type="java.lang.String" value="/Users/rcf/workspace/java/solr" override="true" />
</Context>

获取完SolrHome后,就会实例化SolrResourceLoader。因为传进去的parent是null,所以parents是ContextClassLoader,并且加载了SolrHome/lib/下的文件.

   /**
* <p>
* This loader will delegate to the context classloader when possible,
* otherwise it will attempt to resolve resources using any jar files
* found in the "lib/" directory in the specified instance directory.
* </p>
*
* @param instanceDir - base directory for this resource loader, if null locateSolrHome() will be used.
* @see #locateSolrHome
*/
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;
}
   /**
* Convenience method for getting a new ClassLoader using all files found
* in the specified lib directory.
*/
static URLClassLoader createClassLoader(final File libDir, ClassLoader parent) {
if ( null == parent ) {
parent = Thread.currentThread().getContextClassLoader();
}
return replaceClassLoader(URLClassLoader.newInstance(new URL[0], parent),
libDir, null);
}
 1   /**
* Adds every file/dir found in the baseDir which passes the specified Filter
* to the ClassLoader used by this ResourceLoader. This method <b>MUST</b>
* only be called prior to using this ResourceLoader to get any resources, otherwise
* it's behavior will be non-deterministic. You also have to {link @reloadLuceneSPI}
* before using this ResourceLoader.
*
* <p>This method will quietly ignore missing or non-directory <code>baseDir</code>
* folder.
*
* @param baseDir base directory whose children (either jars or directories of
* classes) will be in the classpath, will be resolved relative
* the instance dir.
* @param filter The filter files must satisfy, if null all files will be accepted.
* @param quiet Be quiet if baseDir does not point to a directory or if no file is
* left after applying the filter.
*/
void addToClassLoader(final String baseDir, final FileFilter filter, boolean quiet) {
File base = FileUtils.resolvePath(new File(getInstanceDir()), baseDir);
if (base != null && base.exists() && base.isDirectory()) {
File[] files = base.listFiles(filter);
if (files == null || files.length == 0) {
if (!quiet) {
log.warn("No files added to classloader from lib: "
+ baseDir + " (resolved as: " + base.getAbsolutePath() + ").");
}
} else {
this.classLoader = replaceClassLoader(classLoader, base, filter);
}
} else {
if (!quiet) {
log.warn("Can't find (or read) directory to add to classloader: "
+ baseDir + " (resolved as: " + base.getAbsolutePath() + ").");
}
}
}

Solr4.8.0源码分析(2)之Solr的启动(一)的更多相关文章

  1. Solr4.8.0源码分析(7)之Solr SPI

    Solr4.8.0源码分析(7)之Solr SPI 查看Solr源码时候会发现,每一个package都会由对应的resources. 如下图所示: 一时对这玩意好奇了,看了文档以后才发现,这个serv ...

  2. Solr4.8.0源码分析(1)之Solr的Servlet

    Solr是作为一个Servlet运行在Tomcat里面的,可以查看Solr的web.xml. 1.web.xml配置 由web.xml可以看出,基本上所有Solr的操作都是在SolrDispatchF ...

  3. Solr4.8.0源码分析(9)之Lucene的索引文件(2)

    Solr4.8.0源码分析(9)之Lucene的索引文件(2) 一. Segments_N文件 一个索引对应一个目录,索引文件都存放在目录里面.Solr的索引文件存放在Solr/Home下的core/ ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. D - Flip tile

    题目大意 翻瓷砖(姑且认为题目就是这个意思吧)     农民约翰知道越聪明越快乐的牛产的牛奶越多(神马鬼理论),于是他开始安排牛进行一个智力运动在一个M*N][] = { {,},{,},{,-},{ ...

  2. java mysql模板

    Java mysql的模版,很优雅.同时也兼顾了性能PreparedStatement和安全性(防SQL注入)两方面.对于比较简单的数据库操作基本满足要求. package dao; import j ...

  3. jeecms v7

    http://bbs.jeecms.com/res_base/jeecms_com_bbs/upload/2015_11/jeecmsv7.zip 安装包 http://bbs.jeecms.com/ ...

  4. Swift: 基本操作符

    这里只讲一下Swift中比较特殊的操作符,在其他语言中也存在操作符就不再讲了 Nil-Coalescing Operator: ?? The nil-coalescing operator (a ?? ...

  5. [转] Java快速教程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品 ...

  6. 动态代理 Proxy InvocationHandler

      前奏 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常 ...

  7. Java编程思想-注解生成外部例子代码

    如果本文帮助到您,请点击下面的链接,这是本人的网站,以示鼓励,谢谢!链接绝对安全! 本人的网站 java注解属于java中高大上的功能,许多开源框架都使用了java注解的功能.比如spring,hib ...

  8. java中this关键字和static关键字和super关键字的用法

    this关键字 1. this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性: 2.可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数(这里面上面有个 ...

  9. ViewHolder的作用和用法

    一直都看别人用ViewHolder,自己也用过,却不知道它的作用是什么?但知道肯定很有用,而且现在android studio应该有直接生产Viewholder的插件, 不过博主我是个新手,就没尝试去 ...

  10. UITableView 的使用总结

    确定单元格的位置:首先要知道分区号,在知道行号. UITableView:API文档的总结:1.UITableView的父类是:UIScrollview,所以他是能滚动的,但是只能在竖直方向滚动.2. ...