springboot1.5.13集成Elasticsearch6.1

由于公司业务需要,需要springboot整合elasticsearch6.1,之前在网上费了好大的劲的才整合好,在此记录下,楼主使用的版本是springboot1.5.13和ElasticSearch6.1.1,其他版本的同学可做参考!

本文使用官方推荐使用Java High Level REST Client,楼主也集成成功,故在此分享一下!

1.核心pom的xml,其他版本的同学请根据自己情况进行修改

 1     <parent>
 2         <groupId>org.springframework.boot</groupId>
 3         <artifactId>spring-boot-starter-parent</artifactId>
 4         <version>1.5.13.RELEASE</version>
 5         <relativePath/> <!-- lookup parent from repository -->
 6     </parent>
 7 
 8     <properties>
 9         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11         <java.version>1.8</java.version>
12     </properties>
13 
14     <dependencies>
15         <dependency>
16             <groupId>org.springframework.boot</groupId>
17             <artifactId>spring-boot-starter-web</artifactId>
18         </dependency>
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-test</artifactId>
22         </dependency>
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-devtools</artifactId>
26         </dependency>
27         <!-- Elasticsearch Dependencies -->
28         <dependency>
29             <groupId>org.elasticsearch</groupId>
30             <artifactId>elasticsearch</artifactId>
31             <version>6.1.1</version>
32         </dependency>
33         <dependency>
34             <groupId>org.elasticsearch.client</groupId>
35             <artifactId>elasticsearch-rest-high-level-client</artifactId>
36             <version>6.1.1</version>
37         </dependency>
38         <dependency>
39             <groupId>org.elasticsearch.client</groupId>
40             <artifactId>elasticsearch-rest-client</artifactId>
41             <version>6.1.1</version>
42         </dependency>
43         <dependency>
44             <groupId>org.elasticsearch.client</groupId>
45             <artifactId>elasticsearch-rest-client-sniffer</artifactId>
46             <version>6.1.1</version>
47         </dependency>
48         <!--日志组件-->
49         <dependency>
50             <groupId>org.apache.logging.log4j</groupId>
51             <artifactId>log4j-api</artifactId>
52             <version>2.8.2</version>
53         </dependency>
54         <dependency>
55             <groupId>org.apache.logging.log4j</groupId>
56             <artifactId>log4j-core</artifactId>
57             <version>2.8.2</version>
58         </dependency>
59         <!--gson组件-->
60         <dependency>
61             <groupId>com.google.code.gson</groupId>
62             <artifactId>gson</artifactId>
63             <version>2.8.2</version>
64         </dependency>
65         <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
66         <dependency>
67             <groupId>javax.inject</groupId>
68             <artifactId>javax.inject</artifactId>
69             <version>1</version>
70         </dependency>
71     </dependencies>

2.elasticsearch配置类 

 1 package cn.ixan.elasticstack.configuration;
 2 
 3 import org.apache.http.HttpHost;
 4 import org.elasticsearch.client.RestClient;
 5 import org.elasticsearch.client.RestHighLevelClient;
 6 import org.slf4j.Logger;
 7 import org.slf4j.LoggerFactory;
 8 import org.springframework.beans.factory.DisposableBean;
 9 import org.springframework.beans.factory.FactoryBean;
10 import org.springframework.beans.factory.InitializingBean;
11 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.context.annotation.Configuration;
13 
14 /**
15  * 不推荐使用elasticsearch spring-data
16  * 所以需要自己注入生成客户端
17  * 这个三个接口可以使用  AbstractFactoryBean  通过继承重写方法代替
18  * Created by [email protected] on 2018/6/16.
19  */
20 @Configuration
21 public class ElasticsearchConfiguration implements FactoryBean<RestHighLevelClient>, InitializingBean, DisposableBean {
22     private final Logger logger = LoggerFactory.getLogger(this.getClass());
23 
24     private RestHighLevelClient restHighLevelClient;
25     @Value("${spring.data.elasticsearch.cluster-nodes}")
26     private String clusterNodes;
27 
28 
29     /**
30      * 控制Bean的实例化过程
31      * @return
32      * @throws Exception
33      */
34     @Override
35     public RestHighLevelClient getObject() throws Exception {
36         return restHighLevelClient;
37     }
38     /**
39      * 获取接口返回的实例的class
40      * @return
41      */
42     @Override
43     public Class<?> getObjectType() {
44         return RestHighLevelClient.class;
45     }
46 
47     @Override
48     public void destroy() throws Exception {
49         try {
50             if(null != restHighLevelClient){
51                 restHighLevelClient.close();
52             }
53         } catch (final Exception e) {
54             logger.error("Error closing ElasticSearch client: ", e);
55         }
56     }
57 
58     @Override
59     public boolean isSingleton() {
60         return false;
61     }
62 
63     @Override
64     public void afterPropertiesSet() throws Exception {
65         restHighLevelClient = buildClient();
66     }
67 
68     private RestHighLevelClient buildClient() {
69         try {
70             restHighLevelClient = new RestHighLevelClient(
71                     RestClient.builder(
72                             new HttpHost(
73                                     clusterNodes.split(":")[0],
74                                     Integer.parseInt(clusterNodes.split(":")[1]),
75                                     "http")));
76         } catch (Exception e) {
77             logger.error(e.getMessage());
78         }
79         return restHighLevelClient;
80     }
81 }
3.application.properties
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9200

 4.测试

 1 package cn.ixan.elasticstack.controller;
 2 
 3 import org.elasticsearch.action.get.GetRequest;
 4 import org.elasticsearch.action.get.GetResponse;
 5 import org.elasticsearch.client.RestHighLevelClient;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 import javax.inject.Inject;
10 import java.io.IOException;
11 
12 @RestController
13 @RequestMapping("/elastic")
14 public class ElasticController {
15     @Inject
16     private RestHighLevelClient client;
17 
18     /**
19      * 测试查询文档
20      */
21     @RequestMapping("/fetchIndex")
22     public Object getIndexTest() {
23         GetRequest request = new GetRequest("magic", "employee", "1");
24         try {
25             GetResponse response = client.get(request);
26             System.out.println(response);
27             return request;
28         } catch (IOException e) {
29             e.printStackTrace();
30             return null;
31         }
32     }
33 
34 }

5.准备测试数据,这里楼主使用postman,其他同学可选择使用其他工具,测试数据类似如下,看不懂的请移步先学习ElasticSearch服务器开发(第二版)
curl -XPUT http://localhost:9200/magic/employee/1 -d '{"title": "New version of
Elasticsearch released!", content": "Version 1.0 released today!", "tags": ["announce",
"elasticsearch", "release"] }'
6.浏览器测试
http://localhost:8080/elastic/fetchIndex
ok,如果到这里你可以查询到数据,那么你已经完成了springboot1.5集成ElasticSearch6.1

猜你喜欢

转载自www.cnblogs.com/ixan/p/9190697.html
今日推荐