源代码下载:http://download.csdn.net/detail/geloin/6644097
步骤一:创建web项目,集成Spring
1. 创建一个web项目,并使其web.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>esserver</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.XmlWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/spring/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
需要注意的地方为“contextConfigLocation”配置,指的是spring配置文件所在的位置,按实现情况配置即可。
2. 创建spring/applicationContext.xml文件,并使其内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true" default-autowire="byName">
<!-- 注解Bean导入时,自定义名称为全类名 -->
<context:component-scan base-package="com.geloin" />
<!-- 注入properties配置到Spring中 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"></property>
<property name="locations">
<list>
<value>classpath:/profile/config.properties</value>
</list>
</property>
</bean>
</beans>
其中,context:component-scan配置适用注解,base-package指定你要使用注解的java类的包名。
配置propertyConfigurer后允许在程序中使用@Value获取配置文件的配置住处,locations指向配置文件的位置,可以有多个配置文件。
3. 创建profile/config.properties文件,使其内容为空。
4. 导入jar包,项目最终结果如下图所示:
5. 此时,启动程序,未报错。
步骤二:集成ES服务端
1. 导入jar包,导入后结果如下图所示:
elasticsearch-0.20.6.jar是es的核心类,elasticsearch-analysis-paoding-1.0.0.jar允许es集成paoding,其他几个lucene包是es必须要使用的jar文件。
2. 建立config/paoding文件夹,将paoding分词器的dic文件夹复制到此文件夹下(到网上下载paoding分词器的dic文件夹),然后建立config/paoding/paoding-analyzer.properties文件,使其内容如下所示:
paoding.analyzer.mode=most-words
paoding.analyzer.dictionaries.compiler=net.paoding.analysis.analyzer.impl.MostWordsModeDictionariesCompiler
paoding.dic.home=classpath:config/paoding/dic
paoding.dic.detector.interval=60
paoding.knife.class.letterKnife=net.paoding.analysis.knife.LetterKnife
paoding.knife.class.numberKnife=net.paoding.analysis.knife.NumberKnife
paoding.knife.class.cjkKnife=net.paoding.analysis.knife.CJKKnife
需要注意的是paoding.dic.home,即dic文件夹所在的位置,如果直接放在config/paoding下,则不需要改变。
3. 填充config.properties文件,使其内容如下所示:
# 集群名称
esserver.cluster.name = elasticsearchclustername
# paoding配置位置
esserver.path.home = classpath:
# 索引文件存储路径
esserver.path.data = D:/work/proTmp/gsearch/indexPath
esserver.cluster.name为集群名称,随机即可;esserver.path.home为paoding-analyzer.properties文件所在位置;esserver.path.data为索引文件位置,随机即可。
4. 添加ManagerConfiguration.java文件,用于获取配置文件内容:
/**
*
*/
package com.geloin.esserver.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* @author Geloin
*
*/
@Service("com.geloin.esserver.config.ManagerConfiguration")
public class ManagerConfiguration {
@Value("${esserver.cluster.name}")
private String clusterName;
@Value("${esserver.path.home}")
private String pathHome;
@Value("${esserver.path.data}")
private String pathData;
public String getClusterName() {
return clusterName;
}
public String getPathData() {
return pathData;
}
public String getPathHome() {
return pathHome;
}
}
具体代码含义请参见Spring开发过程。
5. 添加NodeListener.java文件,用于启动ES服务:
package com.geloin.esserver.listener;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.geloin.esserver.config.ManagerConfiguration;
/**
* @author tangl
*
*/
public class NodeListener implements ServletContextListener {
private Node node;
/*
* (non-Javadoc)
*
* @see
* javax.servlet.ServletContextListener#contextInitialized(javax.servlet
* .ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
// 获取Spring的bean
ServletContext servletContext = sce.getServletContext();
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
ManagerConfiguration config = (ManagerConfiguration) context
.getBean("com.geloin.esserver.config.ManagerConfiguration");
// 设置setting
Map<String, String> settingMap = new HashMap<String, String>();
String clusterName = config.getClusterName();
String pathData = config.getPathData();
String pathHome = config.getPathHome();
settingMap.put("cluster.name", clusterName);
settingMap.put("path.data", pathData);
settingMap.put("path.home", pathHome);
Settings settings = ImmutableSettings.settingsBuilder().put(settingMap)
.build();
// 创建并启动节点
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
nodeBuilder.settings(settings);
node = nodeBuilder.node();
node.start();
}
/*
* (non-Javadoc)
*
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
* ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {
if (null != node) {
// 关闭节点
node.stop();
}
}
}
6. 在web.xml中添加NodeListener的监听:
<listener>
<listener-class>com.geloin.esserver.listener.NodeListener</listener-class>
</listener>
7. 此时项目结构如下图所示:
8. 启动程序。
集成成功标志: