1. 程式人生 > >springboot1.5.13集成Elasticsearch6.1

springboot1.5.13集成Elasticsearch6.1

pat 學習 配置 nal pack 其他 localhost ogg 工具

由於公司業務需要,需要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

springboot1.5.13集成Elasticsearch6.1