本地通過源碼方式啓動solr

 
 
解壓完成後,分爲幾個目錄,然而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

 

 
  • 0abf5e66-6fdf-31c7-9215-3483011f1544-thumb.png
  • 大小: 90.7 KB
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章