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 配置如下:
# 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,可以看到数据,并可以搜索查询,如图:
springboot视频教程:http://www.roncoo.com/course/list.html?courseName=spring+boot

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
普通程序员是如何成功转行大数据工程师?
对于程序员来说,技术进步大大超过世人的想象,如果你不跟随时代进步,就会落后于时代。 我其实已经听过很多人跟我说过类似的话。只不过不同人嘴里提到的词汇各有不同——大数据、数据挖掘、机器学习、人工智能…… 这些当前火热的概念各有不同,又有交叉,总之都是推动我们掌控好海量数据,并从中提取到有价值信息的技术。 大数据相关职位的面试邀请占比与日俱增 很多候选人对大数据相关岗位的青睐并非偶然 互联网行业的快速发展,让不少公司拥有了成千上万的用户数据,各家都想挖掘这座储量丰富的金矿,由此延伸出数据在自家业务不同应用场景中的巨大价值——京东、淘宝等电商网站利用用户画像做个性化推荐,PayPal、宜信等互联网金融公司通过识别高危行为的特征实施风险控制,滴滴、达达等出行、配送业务利用交易数据进行实时定价从而使利润最大化…… 还有一些公司,借助大数据相关技术创造出新的业务模式——比如利用算法做个性化内容推荐的今日头条、一点资讯 这些企业整体对大数据、数据挖掘相关人才的需求非常之大,导致行业内人才的供给相对不足。因而薪资通常也相对高一些。 再加上这些岗位相比于传统的软件工程,有更高的挑战空间和更大的难度,自然...
- 下一篇
Hadoop时代的大数据技术思考:数据即服务
1. Hadoop 的神话正在破灭 IBM leads BigInsights for Hadoop out behind barn. Shots heard IBM has announced the retirement of the basic plan for its data analytics software platform, BigInsights for Hadoop. The basic plan of the service will be retired in a month, on December 7 of this year. “IBM把BigInsights for Hadoop牵到牧棚后面,只听一声枪响…” 这个是前不久英国知名媒体The Register对IBM 产品BigInsights产品下线的报道。 BigInsights 是IBM在Apache Hadoop上增加了不少IBM分析技术能力后形成的一个大数据分析产品。 在面临近乎2年的前途未卜的窘境之后,IBM终于决定将其关闭。 无独有偶,前不久Gartner的一篇文章也指出 “70%以上的H...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Hadoop3单机部署,实现最简伪集群
- 设置Eclipse缩进为4个空格,增强代码规范
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7