集成Spring、Elasticsearch、paoding,将ES服务嵌入到Web程序

77 篇文章 2 订阅
19 篇文章 0 订阅

        源代码下载:集成Spring、Elasticsearch、paoding,将ES服务嵌入到Web程序-Java代码类资源-CSDN下载

        步骤一:创建web项目,集成Spring

        1. 创建一个web项目,并使其web.xml文件如下所示:

  1. < xmlversion=""1.0" encoding="UTF-8"?<  
  2. "http://www.w3.org/2001/XMLSchema-instance"
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5.     id="WebApp_ID" version="2.5"<  
  6.     esserver  
  7.       
  8.         contextClass  
  9.         org.springframework.web.context.support.XmlWebApplicationContext  
  10.       
  11.       
  12.         contextConfigLocation  
  13.             
  14.             classpath:/spring/applicationContext.xml  
  15.           
  16.       
  17.       
  18.         classclass<  
  19.       
  20.       
  21.         index.html  
  22.       
  23.   

        需要注意的地方为“contextConfigLocation”配置,指的是spring配置文件所在的位置,按实现情况配置即可。

        2. 创建spring/applicationContext.xml文件,并使其内容如下所示:

  1. < xmlversion=""1.0" encoding="UTF-8"?<  
  2. "http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.                         http://www.springframework.org/schema/beans/spring-beans.xsd
  6.                         http://www.springframework.org/schema/context
  7.                         http://www.springframework.org/schema/context/spring-context.xsd"
  8. default-lazy-init="true"default-autowire="byName"<  
  9.       
  10.     package="com.geloin" /<  
  11.       
  12.     "propertyConfigurer"
  13. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"<  
  14.         "ignoreResourceNotFound" value="true"<  
  15.         "locations"<  
  16.               
  17.                 classpath:/profile/config.properties  
  18.               
  19.           
  20.       
  21.   

        其中,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文件,使其内容如下所示:

  1. paoding.analyzer.mode=most-words  
  2. paoding.analyzer.dictionaries.compiler=net.paoding.analysis.analyzer.impl.MostWordsModeDictionariesCompiler  
  3. paoding.dic.home=classpath:config/paoding/dic  
  4. paoding.dic.detector.interval=60
  5. paoding.knife.class.letterKnife=net.paoding.analysis.knife.LetterKnife  
  6. paoding.knife.class.numberKnife=net.paoding.analysis.knife.NumberKnife  
  7. paoding.knife.class.cjkKnife=net.paoding.analysis.knife.CJKKnife  

        需要注意的是paoding.dic.home,即dic文件夹所在的位置,如果直接放在config/paoding下,则不需要改变。

        3. 填充config.properties文件,使其内容如下所示:

  1. # 集群名称  
  2. esserver.cluster.name = elasticsearchclustername  
  3. # paoding配置位置  
  4. esserver.path.home = classpath:  
  5. # 索引文件存储路径  
  6. esserver.path.data = D:/work/proTmp/gsearch/indexPath  

        esserver.cluster.name为集群名称,随机即可;esserver.path.home为paoding-analyzer.properties文件所在位置;esserver.path.data为索引文件位置,随机即可。

        4. 添加ManagerConfiguration.java文件,用于获取配置文件内容:

  1. /**
  2.  * 
  3.  */
  4. package com.geloin.esserver.config;  
  5. import org.springframework.beans.factory.annotation.Value;  
  6. import org.springframework.stereotype.Service;  
  7. /**
  8.  * @author Geloin
  9.  * 
  10.  */
  11. @Service("com.geloin.esserver.config.ManagerConfiguration")  
  12. publicclass ManagerConfiguration {  
  13. @Value("${esserver.cluster.name}")  
  14. private String clusterName;  
  15. @Value("${esserver.path.home}")  
  16. private String pathHome;  
  17. @Value("${esserver.path.data}")  
  18. private String pathData;  
  19. public String getClusterName() {  
  20. return clusterName;  
  21.     }  
  22. public String getPathData() {  
  23. return pathData;  
  24.     }  
  25. public String getPathHome() {  
  26. return pathHome;  
  27.     }  
  28. }  

        具体代码含义请参见Spring开发过程。

        5. 添加NodeListener.java文件,用于启动ES服务:

  1. package com.geloin.esserver.listener;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import javax.servlet.ServletContext;  
  5. import javax.servlet.ServletContextEvent;  
  6. import javax.servlet.ServletContextListener;  
  7. import org.elasticsearch.common.settings.ImmutableSettings;  
  8. import org.elasticsearch.common.settings.Settings;  
  9. import org.elasticsearch.node.Node;  
  10. import org.elasticsearch.node.NodeBuilder;  
  11. import org.springframework.context.ApplicationContext;  
  12. import org.springframework.web.context.support.WebApplicationContextUtils;  
  13. import com.geloin.esserver.config.ManagerConfiguration;  
  14. /**
  15.  * @author tangl
  16.  * 
  17.  */
  18. publicclass NodeListener implements ServletContextListener {  
  19. private Node node;  
  20. /*
  21.      * (non-Javadoc)
  22.      * 
  23.      * @see
  24.      * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
  25.      * .ServletContextEvent)
  26.      */
  27. publicvoid contextInitialized(ServletContextEvent sce) {  
  28. // 获取Spring的bean
  29.         ServletContext servletContext = sce.getServletContext();  
  30.         ApplicationContext context = WebApplicationContextUtils  
  31.                 .getWebApplicationContext(servletContext);  
  32.         ManagerConfiguration config = (ManagerConfiguration) context  
  33.                 .getBean("com.geloin.esserver.config.ManagerConfiguration");  
  34. // 设置setting
  35.         Map settingMap = new HashMap();  
  36.         String clusterName = config.getClusterName();  
  37.         String pathData = config.getPathData();  
  38.         String pathHome = config.getPathHome();  
  39.         settingMap.put("cluster.name", clusterName);  
  40.         settingMap.put("path.data", pathData);  
  41.         settingMap.put("path.home", pathHome);  
  42.         Settings settings = ImmutableSettings.settingsBuilder().put(settingMap)  
  43.                 .build();  
  44. // 创建并启动节点
  45.         NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();  
  46.         nodeBuilder.settings(settings);  
  47.         node = nodeBuilder.node();  
  48.         node.start();  
  49.     }  
  50. /*
  51.      * (non-Javadoc)
  52.      * 
  53.      * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
  54.      * ServletContextEvent)
  55.      */
  56. publicvoid contextDestroyed(ServletContextEvent sce) {  
  57. if (null != node) {  
  58. // 关闭节点
  59.             node.stop();  
  60.         }  
  61.     }  
  62. }  

        6. 在web.xml中添加NodeListener的监听:

        7. 此时项目结构如下图所示:

 

        8. 启动程序。

        集成成功标志:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值