首先,下载solr5.5.0源码, http://apache.fayea.com/lucene/solr/5.5.0/solr-5.5.0-src.tgz
解压完成后,分为几个目录,然而solr是通过ant编译的,我们想将其转换为maven方式,发现其中存在dev-tools/maven中,其中对应的说明:
Copy the Maven POM templates from under dev-tools/maven/ to the maven-build/ directory using the following command from the top-level directory: ant get-maven-poms Note that you will need to do this whenever changes to the POM templates are committed. For this reason, it's a good idea run "ant get-maven-poms" after you update from origin.
在源代码的顶级目录中执行:
ant get-maven-poms,提示会查找ivy,没有安装,于是从ivy的官网下载。ivy属于ant的一个插件,需要将jar包copy到ant安装目录下的lib中,此时执行该命令后,就可以执行build。关于ivy的介绍,可以参考: http://www.cnblogs.com/ungshow/archive/2009/07/08/1519111.html
ant get-maven-poms Buildfile: /Users/mazhiqiang/develop/study/solr-5.5.0/build.xml resolve: resolve: ivy-availability-check: ivy-fail: ivy-configure: [ivy:configure] :: Apache Ivy 2.4.0 - 20141213170938 :: http://ant.apache.org/ivy/ :: [ivy:configure] :: loading settings :: file = /Users/xxx/develop/study/solr-5.5.0/lucene/ivy-settings.xml resolve: [ivy:retrieve] downloading https://repo1.maven.org/maven2/junit/junit/4.10/junit-4.10.jar ... [ivy:retrieve] ................. (247kB) [ivy:retrieve] .. (0kB) [ivy:retrieve] [SUCCESSFUL ] junit#junit;4.10!junit.jar (3616ms) [ivy:retrieve] downloading https://repo1.maven.org/maven2/com/carrotsearch/randomizedtesting/randomizedtesting-runner/2.3.2/randomizedtesting-runner-2.3.2.jar ... [ivy:retrieve] ................ (232kB) [ivy:retrieve] .. (0kB) [ivy:retrieve] [SUCCESSFUL ] com.carrotsearch.randomizedtesting#randomizedtesting-runner;2.3.2!randomizedtesting-runner.jar (2142ms) ivy-availability-check: …. -append-module-dependencies-properties: [echo] Get maven dependencies called under: /Users/mazhiqiang/develop/study/solr-5.5.0/lucene/.. -get-maven-poms: [copy] Copying 57 files to /Users/mazhiqiang/develop/study/solr-5.5.0/maven-build BUILD SUCCESSFUL Total time: 20 minutes 13 seconds
经过几次失败后,折腾一个小时,终于成功。ivy也比较类似maven,但是使用的仓库在用户目录/.ivy/下。
生成的pom文件按照工程的目录,已经放到了maven-build目录下,直接将其copy至顶层目录,本人的环境配置中使用的solr版本为5.5.0,但是发现生成的pom.xml用idea导入后问题非常多,不能直接启动。
不管用什么IDE首选都要设置Solr Home在IDE的JVM参数设置VM arguments写入 -Dsolr.solr.home=solr/example/solr一般就行了.不行也可以使用绝对路径.
solr使用StartSolrJetty文件作为入口文件进行调试代码,在这里可以设置服务器使用的端口和solr的webapps目录.一般都不用设置,默认的就可以进行调试.Solr Home也能可在代码中设置一样好用. System.setProperty("solr.solr.home", "E:\\Work\\solr-4.2.0-src-idea\\solr\\example\\solr");
目前是使用自带的一个example作为solr配置的根目录,如果你有其他的solr配置目录,设置之即可。点击run即可,debug也是一样可以用了。没有别的问题就应该能运行了.注意servlet 容器使用的端口,如查提示:
FAILED [email protected]:8983: java.net.BindException: Address already in use: JVM_Bind 就说明当前端口占用中.改一下就可以了.如果没有报错启动成功后就可以在浏览器中输入地址: http://localhost:8983/solr/ 就可以看到如下界面
从github上fork一个lucene-solr的项目: https://github.com/clamaa/lucene-solr,这个版本是6的,所以跟之前用到的有些jar包可能不太兼容(一些Deprecated的类已经被删除)。
使用idea启动
下载solr源码后,如果使用idea IDE作为启动容器,现在根目录下执行 ant idea,再启动StartSolrJetty.main函数即可。
resolve: idea: [copy] Copying 81 files to /Users/xxx/develop/workspace/github/lucene-solr -post-idea-instructions: [echo] [echo] To complete IntelliJ IDEA setup, you must manually configure [echo] File | Project Structure | Project | Project SDK. [echo] [echo] You won't have to do this in the future if you define property [echo] ${idea.jdk}, e.g. in ~/lucene.build.properties, ~/build.properties [echo] or lucene/build.properties, with a value consisting of the [echo] following two XML attributes/values (adjust values according to [echo] JDKs you have defined locally - see [echo] File | Project Structure | Platform Settings | SDKs): [echo] [echo] idea.jdk = project-jdk-name="1.8" project-jdk-type="JavaSDK" [echo] BUILD SUCCESSFUL Total time: 3 minutes 9 seconds
用idea导入项目后,可以直接查看所有的java类以及配置文件信息。
修改SolrDispatchFilter, 理论上是需要读取当前ServletContext上下文中的solr.solr.home属性,我们这里将其直接修改为一个本地绝对路径,以便能够直接启动:
// String solrHome = (String) config.getServletContext().getAttribute(SOLRHOME_ATTRIBUTE); String solrHome = "/Users/xxx/develop/workspace/github/lucene-solr/solr/server/solr";
org.apache.solr.client.solrj.StartSolrJetty作为启动类,需要修改一下 bb.setWar("solr/webapp/web”)。
打印出如下信息后,启动成功:
objc[5820]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined. Connected to the target VM, address: '127.0.0.1:61273', transport: 'socket' 0 INFO (main) [ ] o.e.j.u.log Logging initialized @975ms 292 INFO (main) [ ] o.e.j.s.Server jetty-9.3.8.v20160314 >>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP 633 INFO (main) [ ] o.e.j.w.StandardDescriptorProcessor NO JSP Support for /solr, did not find org.eclipse.jetty.jsp.JettyJspServlet 697 INFO (main) [ ] o.a.s.s.SolrDispatchFilter SolrDispatchFilter.init(): sun.misc.Launcher$AppClassLoader@58644d46 773 INFO (main) [ ] o.a.s.c.SolrResourceLoader new SolrResourceLoader for directory: '/Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr' 9565 INFO (main) [ ] o.a.s.c.SolrResourceLoader JNDI not configured for solr (NoInitialContextEx) 9565 INFO (main) [ ] o.a.s.c.SolrResourceLoader solr home defaulted to 'solr/' (could not find system property or JNDI) 9582 INFO (main) [ ] o.a.s.c.SolrXmlConfig Loading container configuration from /Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr/solr.xml 9785 INFO (main) [ ] o.a.s.c.CorePropertiesLocator Config-defined core root directory: /Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr 9853 INFO (main) [ ] o.a.s.c.CoreContainer New CoreContainer 313239742 9853 INFO (main) [ ] o.a.s.c.CoreContainer Loading cores into CoreContainer [instanceDir=/Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr] 9855 WARN (main) [ ] o.a.s.c.CoreContainer Couldn't add files from /Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr/lib to classpath: /Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr/lib 9893 INFO (main) [ ] o.a.s.h.c.HttpShardHandlerFactory created with socketTimeout : 600000,connTimeout : 60000,maxConnectionsPerHost : 20,maxConnections : 10000,corePoolSize : 0,maximumPoolSize : 2147483647,maxThreadIdleTime : 5,sizeOfQueue : -1,fairnessPolicy : false, 10404 INFO (main) [ ] o.a.s.u.UpdateShardHandler Creating UpdateShardHandler HTTP client with params: 10408 INFO (main) [ ] o.a.s.l.LogWatcher SLF4J impl is org.slf4j.impl.Log4jLoggerFactory 10409 INFO (main) [ ] o.a.s.l.LogWatcher Registering Log Listener [Log4j (org.slf4j.impl.Log4jLoggerFactory)] 10415 INFO (main) [ ] o.a.s.c.CoreContainer Security conf doesn't exist. Skipping setup for authorization module. 10416 INFO (main) [ ] o.a.s.c.CoreContainer No authentication plugin used. 10615 INFO (main) [ ] o.a.s.c.CorePropertiesLocator Looking for core definitions underneath /Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/server/solr 10658 INFO (main) [ ] o.a.s.c.CorePropertiesLocator Found 0 core definitions 10746 INFO (main) [ ] o.a.s.s.SolrDispatchFilter user.dir=/Users/mazhiqiang/develop/workspace/github/lucene-solr 10746 INFO (main) [ ] o.a.s.s.SolrDispatchFilter SolrDispatchFilter.init() done 10772 INFO (main) [ ] o.e.j.s.h.ContextHandler Started o.e.j.w.WebAppContext@2e6a5539{/solr,file:///Users/mazhiqiang/develop/workspace/github/lucene-solr/solr/webapp/web/,AVAILABLE}{solr/webapp/web} 10802 INFO (main) [ ] o.e.j.s.ServerConnector Started ServerConnector@6436a7db{HTTP/1.1,[http/1.1]}{0.0.0.0:8983} 10802 INFO (main) [ ] o.e.j.s.Server Started @11904ms
如果我们之前启动的项目需要用到mmseg4j分词器,需要将这些jar包同样加入我们的classpath中,否则会爆出下面的错误:
Caused by: java.lang.ClassNotFoundException: com.chenlb.mmseg4j.solr.MMSegTokenizerFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:340) at org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:520)
将必要的jar包放到idea的classpath中之后,启动就不会出现问题,可以正常通过web端( http://localhost:8983/solr)来启动solr服务并调试了。
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