本章内容html
ES 及 x-pack 下载安装java
Kibana 及 x-pack 下载安装node
Spring Boot 整合 ESweb
Spring Boot 操做 ESspring
1. ES 及 x-pack 下载安装数据库
spring-data-elasticsearch 之 ElasticSearch 架构初探,详细看下我另一篇文章《深刻浅出 spring-data-elasticsearch 之 ElasticSearch 架构初探(一)》 http://www.spring4all.com/article/330apache
ES 三大要素:编程
文档(Document) 文档,在面向对象观念就是一个对象。在 ES 里面,是一个大 JSON 对象,是指定了惟一 ID 的最底层或者根对象。文档的位置由 index、type 和 _id 惟一标识json
索引(Index) 索引,用于区分文档成组,即分到一组的文档集合。索引,用于存储文档和使文档可被搜索。好比项目存索引 project 里面,交易存索引 sales 等。vim
类型(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 配置以下:
# ES spring: 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") @Data public 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 数据操做层实现代码以下:
@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();
增删改查、搜索都有了,不须要咱们具体实现,只需咱们传入对应的参数便可。
4. 文章搜索 ES 业务逻辑实现类
实现了批量保存到 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":"温岭是个沿海城市", "admin":1 }, { "id":3, "title":"文章题目文章题目文章题目", "writer":"温1岭", "content":"温2岭是个沿海城市", "admin":1 } ]
而后在 Kibana 设置 indexName 为 "elasticsearch",并打开 Discover tab,能够看到数据,并能够搜索查询,如图:
文章来源:http://mp.weixin.qq.com/s/t82YJgMmFoBeQaWzPJmRIQ
springboot视频教程:http://www.roncoo.com/course/list.html?courseName=spring+boot