Spring Boot 2.0 M7 整合 ES 5 、Kibana 和 X-pack
本章内容
-
ES 及 x-pack 下载安装
-
Kibana 及 x-pack 下载安装
-
Spring Boot 整合 ES
-
Spring Boot 操作 ES
1. ES 及 x-pack 下载安装
spring-data-elasticsearch 之 ElasticSearch 架构初探,详细看下我另外一篇文章《深入浅出 spring-data-elasticsearch 之 ElasticSearch 架构初探(一)》 http://www.spring4all.com/article/330
ES 三大要素:
-
文档(Document) 文档,在面向对象观念就是一个对象。在 ES 里面,是一个大 JSON 对象,是指定了唯一 ID 的最底层或者根对象。文档的位置由 index、type 和 _id 唯一标识。
-
索引(Index) 索引,用于区分文档成组,即分到一组的文档集合。索引,用于存储文档和使文档可被搜索。比如项目存索引 project 里面,交易存索引 sales 等。
-
类型(Type) 类型,用于区分索引中的文档,即在索引中对数据逻辑分区。比如索引 project 的项目数据,根据项目类型 ui 项目、插画项目等进行区分。
和关系型数据库 MySQL 做个类比:
-
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 并启动
设置 Xpack 安全验证为 false:
vim config/elasticsearch.yml
并添加下面配置:
xpack.security.enabled: false
并启动 ES:
./bin/elasticsearch 或者后台启动 ./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 并启动
设置 Xpack 安全验证为 false:
vim config/kibana.yml
并添加下面配置:
xpack.security.enabled: false
并启动 Kibana:
./bin/kibanah 或者后台启动 nohup ./bin/kibana &
必须注意:先启动 ES,再启动 Kibana。
如果后台启动注意,关闭命令如下:
ps aux | grep 'elastic' kill -9 pid
启动成功后,打开网页访问 127.0.0.1:5601 , 默认账号为: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>
<!-- 简化 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 为数据访问提供一种相似且一致性的编程模型,并保存底层数据存储的。
Spring Data Elasticsearch
spring-data-elasticsearch 是 Spring Data 的 Community modules 之一,是 Spring Data 对 Elasticsearch 引擎的实现。 Elasticsearch 默认提供轻量级的 HTTP Restful 接口形式的访问。相对来说,使用 HTTP Client 调用也很简单。但 spring-data-elasticsearch 可以更快的支持构建在 Spring 应用上,比如在 application.properties 配置 ES 节点信息和 spring-boot-starter-data-elasticsearch 依赖,直接在 Spring Boot 应用上使用。
这里依赖的 spring-boot-starter-data-elasticsearch 版本是 2.0,对应的 spring-data-elasticsearch 版本是 5.5.3.RELEASE。 对应 ES 尽量安装成版本一致 5.5.3。
2. application.properties 配置 ES 地址
application.properties 配置如下:
# ESspring: data: elasticsearch: repositories: enabled: true cluster-name: elasticsearch cluster-nodes: 120.132.29.37:9300
默认 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。 更多配置:
-
spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默认值: elasticsearch)
-
spring.data.elasticsearch.cluster-nodes 集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。
-
spring.data.elasticsearch.propertie 用来配置客户端的额外属性。
-
spring.data.elasticsearch.repositories.enabled 开启 Elasticsearch 仓库。(默认值:true。)
3. ES 数据操作层
文章实体类代码如下:
/** * 文章 */@Document(indexName = "elasticsearch", type = "article")@Datapublic class ArticleEntity implements Serializable { // 作者信息 private String writer; // 文章信息 @Id private Long id; private String title; private String content; // 归属信息 private Long admin;}
-
City 属性名不支持驼峰式。
-
indexName 配置必须是全部小写,不然会出异常。 org.elasticsearch.indices.InvalidIndexNameException: Invalid index name [provinceIndex], must be lowercase
ES 数据操作层实现代码如下:
@Repositorypublic 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();
增删改查、搜索都有了,不需要我们具体实现,只需我们传入对应的参数即可。
4. 文章搜索 ES 业务逻辑实现类
实现了批量保存到 ES 的接口,代码如下:
@Service@Primary@AllArgsConstructor@Log4j2public 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.1Host: 127.0.0.1:8080Content-Type: application/jsonCache-Control: no-cachePostman-Token: d6288a82-b98f-1c1e-6dad-15b0223102ab[ { "id":2, "title":"文章题目", "writer":"温岭", "content":"温岭是个沿海城市", "admin":1 }, { "id":3, "title":"文章题目文章题目文章题目", "writer":"温1岭", "content":"温2岭是个沿海城市", "admin":1 }]
然后在 Kibana 设置 indexName 为 "elasticsearch",并打开 Discover tab,可以看到数据,并可以搜索查询,如图:
本文转自yushiwh 51CTO博客,原文链接:http://blog.51cto.com/yushiwh/2048183,如需转载请自行联系原作者