本章內(nèi)容
-
-
- ES 及 x-pack 下載安裝
-
-
-
- Kibana 及 x-pack 下載安裝
-
-
-
- Spring Boot 整合 ES
-
-
-
- Spring Boot 操作 ES
-
閱讀時(shí)間:5 分鐘
摘錄:打算起手不凡寫出鴻篇巨作的,往往堅(jiān)持不了完成第一章節(jié)
原文出處:spring4all.com
1. ES 及 x-pack 下載安裝
spring-data-elasticsearch 之 ElasticSearch 架構(gòu)初探,詳細(xì)看下我另外一篇文章《深入淺出 spring-data-elasticsearch 之 ElasticSearch 架構(gòu)初探(一)》 http://www.spring4all.com/article/330
ES 三大要素:
-
文檔(Document) 文檔,在面向?qū)ο笥^念就是一個(gè)對(duì)象。在 ES 里面,是一個(gè)大 JSON 對(duì)象,是指定了唯一 ID 的最底層或者根對(duì)象。文檔的位置由 _index、_type 和 _id 唯一標(biāo)識(shí)。
-
索引(Index) 索引,用于區(qū)分文檔成組,即分到一組的文檔集合。索引,用于存儲(chǔ)文檔和使文檔可被搜索。比如項(xiàng)目存索引 project 里面,交易存索引 sales 等。
-
類型(Type) 類型,用于區(qū)分索引中的文檔,即在索引中對(duì)數(shù)據(jù)邏輯分區(qū)。比如索引 project 的項(xiàng)目數(shù)據(jù),根據(jù)項(xiàng)目類型 ui 項(xiàng)目、插畫項(xiàng)目等進(jìn)行區(qū)分。
和關(guān)系型數(shù)據(jù)庫(kù) MySQL 做個(gè)類比:
- Document 類似于 Record
- Type 類似于 Table
- Index 類似于 Database
安裝步驟如下:
1. 下載 ES 5.5.3
下載地址: https://www.elastic.co/downloads/past-releases/elasticsearch-5-5-3
2. 安裝 ES 插件 x-pack
解壓 – 然后安裝 插件 x-pack
tar -xzf elasticsearch-5.3.0.tar.gz
cd elasticsearch-5.3.0/
// 安裝 X-Pack
bin/elasticsearch-plugin install x-pack
3. 配置 ES 并啟動(dòng)
設(shè)置 Xpack 安全驗(yàn)證為 false:
vim config/elasticsearch.yml
并添加下面配置:
xpack.security.enabled: false
并啟動(dòng) ES:
./bin/elasticsearch
或者后臺(tái)啟動(dòng)
./bin/elasticsearch -d
2. Kibana 及 x-pack 下載安裝
1. 下載 Kibana 5.5.3
下載地址: https://www.elastic.co/downloads/past-releases/kibana-5-5-3
2. 安裝 Kibana 插件 x-pack
解壓 – 然后安裝 插件 x-pack
tar -zxvf kibana-5.5.3-darwin-x86_64.tar.gz
cd kibana-5.5.3-darwin-x86_64/
// 安裝 X-Pack
bin/kibana-plugin install x-pack
3. 配置 Kibana 并啟動(dòng)
設(shè)置 Xpack 安全驗(yàn)證為 false:
vim config/kibana.yml
并添加下面配置:
xpack.security.enabled: false
并啟動(dòng) Kibana:
./bin/kibanah
或者后臺(tái)啟動(dòng)
nohup ./bin/kibana &
必須注意:先啟動(dòng) ES,再啟動(dòng) Kibana。
如果后臺(tái)啟動(dòng)注意,關(guān)閉命令如下:
ps aux | grep 'elastic'
kill -9 pid
啟動(dòng)成功后,打開網(wǎng)頁(yè)訪問(wèn) 127.0.0.1:5601 , 默認(rèn)賬號(hào)為:elasti,密碼為 changeme。
3. Spring Boot 整合 ES
1. pom.xml 依賴
代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring4all</groupId>
<artifactId>bysocket</artifactId>
<version>1.0.0</version>
<description>bysocket :: AI For All</description>
<properties>
<lombok.version>1.16.18</lombok.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M7</version>
</parent>
<dependencies>
<!-- web 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 日志 log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- 容器 undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- 簡(jiǎn)化 lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- ES -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Spring Data
要了解 spring-data-elasticsearch 是什么,首先了解什么是 Spring Data。 Spring Data 基于 Spring 為數(shù)據(jù)訪問(wèn)提供一種相似且一致性的編程模型,并保存底層數(shù)據(jù)存儲(chǔ)的。
Spring Data Elasticsearch
spring-data-elasticsearch 是 Spring Data 的 Community modules 之一,是 Spring Data 對(duì) Elasticsearch 引擎的實(shí)現(xiàn)。 Elasticsearch 默認(rèn)提供輕量級(jí)的 HTTP Restful 接口形式的訪問(wèn)。相對(duì)來(lái)說(shuō),使用 HTTP Client 調(diào)用也很簡(jiǎn)單。但 spring-data-elasticsearch 可以更快的支持構(gòu)建在 Spring 應(yīng)用上,比如在 application.properties 配置 ES 節(jié)點(diǎn)信息和 spring-boot-starter-data-elasticsearch 依賴,直接在 Spring Boot 應(yīng)用上使用。
這里依賴的 spring-boot-starter-data-elasticsearch 版本是 2.0,對(duì)應(yīng)的 spring-data-elasticsearch 版本是 5.5.3.RELEASE。 對(duì)應(yīng) ES 盡量安裝成版本一致 5.5.3。
2. application.properties 配置 ES 地址
application.properties 配置如下:
# ES
spring:
data:
elasticsearch:
repositories:
enabled: true
cluster-name: elasticsearch
cluster-nodes: 120.132.29.37:9300
默認(rèn) 9300 是 Java 客戶端的端口。9200 是支持 Restful HTTP 的接口。 更多配置:
- spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默認(rèn)值: elasticsearch)
- spring.data.elasticsearch.cluster-nodes 集群節(jié)點(diǎn)地址列表,用逗號(hào)分隔。如果沒有指定,就啟動(dòng)一個(gè)客戶端節(jié)點(diǎn)。
- spring.data.elasticsearch.propertie 用來(lái)配置客戶端的額外屬性。
- spring.data.elasticsearch.repositories.enabled 開啟 Elasticsearch 倉(cāng)庫(kù)。(默認(rèn)值:true。)
3. ES 數(shù)據(jù)操作層
文章實(shí)體類代碼如下:
/**
* 文章
*/
@Document(indexName = "elasticsearch", type = "article")
@Data
public class ArticleEntity implements Serializable {
// 作者信息
private String writer;
// 文章信息
@Id
private Long id;
private String title;
private String content;
// 歸屬信息
private Long admin;
}
- City 屬性名不支持駝峰式。
- indexName 配置必須是全部小寫,不然會(huì)出異常。 org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase
ES 數(shù)據(jù)操作層實(shí)現(xiàn)代碼如下:
@Repository
public interface ArticleRepository extends ElasticsearchRepository<ArticleEntity, Long> {
}
接口只要繼承 ElasticsearchRepository 接口類即可,具體使用的是該接口的方法:
<S extends T> S save(S var1);
<S extends T> Iterable<S> saveAll(Iterable<S> var1);
Optional<T> findById(ID var1);
boolean existsById(ID var1);
Iterable<T> findAll();
Iterable<T> findAllById(Iterable<ID> var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable<? extends T> var1);
void deleteAll();
<S extends T> S index(S var1);
Iterable<T> search(QueryBuilder var1);
Page<T> search(QueryBuilder var1, Pageable var2);
Page<T> search(SearchQuery var1);
Page<T> searchSimilar(T var1, String[] var2, Pageable var3);
void refresh();
Class<T> getEntityClass();
增刪改查、搜索都有了,不需要我們具體實(shí)現(xiàn),只需我們傳入對(duì)應(yīng)的參數(shù)即可。
4. 文章搜索 ES 業(yè)務(wù)邏輯實(shí)現(xiàn)類
實(shí)現(xiàn)了批量保存到 ES 的接口,代碼如下:
@Service
@Primary
@AllArgsConstructor
@Log4j2
public class ArticleServiceImpl implements ArticleService {
private final ArticleRepository articleRepository;
@Override
public boolean saveArticles(List<ArticleBean> articleBeanList) {
List articleEntityList = transferToArticleEntityList(articleBeanList);
articleRepository.saveAll(articleEntityList);
return true;
}
... 省略
}
4. Spring Boot 操作 ES
用 Postman 工具新增文章:
POST /api/articles HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: d6288a82-b98f-1c1e-6dad-15b0223102ab
[
{
"id":2,
"title":"文章題目",
"writer":"溫嶺",
"content":"溫嶺是個(gè)沿海城市",
"admin":1
},
{
"id":3,
"title":"文章題目文章題目文章題目",
"writer":"溫1嶺",
"content":"溫2嶺是個(gè)沿海城市",
"admin":1
}
]
然后在 Kibana 設(shè)置 indexName 為 “elasticsearch”,并打開 Discover tab,可以看到數(shù)據(jù),并可以搜索查詢,如圖:
更多 ES 文章:
- 《Spring Boot 整合 Elasticsearch》
- 《深入淺出 spring-data-elasticsearch 之 ElasticSearch 架構(gòu)初探(一)》
- 《深入淺出 spring-data-elasticsearch 系列 – 概述及入門(二)》
- 《深入淺出 spring-data-elasticsearch – 基本案例詳解(三)》
- 《深入淺出 spring-data-elasticsearch – 實(shí)戰(zhàn)案例詳解(四)》
關(guān)注即可得系列教程文章哦!
作者:SpringForAll文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-830701.html
原文地址:https://my.oschina.net/u/3677020/blog/1586271文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-830701.html
到了這里,關(guān)于Spring Boot 2.0 M7 整合 ES 5 、Kibana 和 X-pack的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!