仍然使用Solr-4.9.1版本,由于在eclipse中操作比较方便,所以将solr在eclipse中发布为web项目,便于后续添加代码,创建solr的web项目步骤如下:
1、Solr-demo:在eclipse中新建web项目,命名为solr-demo
2、Solr_home:将solr.war(在solr解压目录的example/webapps下)放在tomcat的webapps下,然后启动tomcat,可以解压solr.war,取得solr为名的项目目录,然后将solr目录下的所有文件拷贝到solr-demo\WebContent下,命名为solr_home(注意有相同文件METE-INF和WEB-INF,WEB-INF下包含solr需要的jar包和web.xml中solr的配置,需要保留)
切记:在tomcat启动之前不要创建其他的core文件夹,尤其注意除了默认的collection1,在其他文件夹下不要包含core.properties文件,否则会导致tomcat启动失败
配置文件中配置找寻solr_home的路径(我在此处在solr_home下又加了一层collection1路径(此路径不作为core的名称),在collection1下可以创建collection1、user等core)
配置寻找solr_home路径两种方式:
i、web.xml中配置
<env-entry> <env-entry-name>solr/home</env-entry-name> <env-entry-value>D:/workspace/solr-demo/WebContent/solr_home/collection1</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry> |
ii、listener配置
在web.xml中加入监听器配置
<listener> <listener-class> solr.common.SolrListener </listener-class> </listener> |
编写SolrListener,目录在solr.common(src/main/java)
package solr.common; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * 通过监听器设置solr.solr.home,需要在web.xml配置 * @author limanman * */ public class SolrListener implements ServletContextListener { /* * (non-Javadoc) * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet. * ServletContextEvent) */ public void contextDestroyed(ServletContextEvent sce) { } /* * (non-Javadoc) * @see * javax.servlet.ServletContextListener#contextInitialized(javax.servlet * .ServletContextEvent) */ public void contextInitialized(ServletContextEvent sce) { String path = this.getClass().getResource("/").getPath(); path = "D:/workspace/solr-demo/WebContent/solr_home/collection1";//jetty会报错 所以我直接写死了这个路径 System.setProperty("solr.solr.home", path); } } |
3、补充:然后按照单机版的tomcat服务配置补充文件,将solr-4.9.1\example\lib\ext文件夹下的jar包拷贝到WEB-INF的lib目录下(注:必须放在此目录下,否则找不到jar包),将example/resource下的log4j.properties复制到项目的resource目录中
4、启动tomcat,运行URL:http://localhost:8080/solr-demo/admin.html