springboot集成elk 一: springboot + Elasticsearch
1.ELK介绍
1> Elasticsearch是实时全文搜索和分析引擎,
提供搜集、分析、存储数据三大功能;
是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。
2> Logstash是一个用来搜集、分析、过滤日志的工具。
它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。
它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。
3> Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch
指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据
下载Elasticsearch 、Kibana、Logstash
2.ELK安装
https://www.elastic.co/cn/downloads/

2.1启动Elasticsearch
进入bin目录cd elasticsearch / bin 执行.\elasticsearch
可以修改集群或节点名称。在启动 Elasticsearch 时从命令行完成,
./elasticsearch -Ecluster.name = my_cluster_name -Enode.name = my_node_name
测试:http://localhost:9200/

备注:关闭Elasticsearch 快捷键ctrl+c,然后输入y
2.2 启动kibana
修改配置 config/kibana.yml

进入bin目录 cd kibana\bin 执行kibana
http://localhost:5601

备注:关闭kibana快捷键ctrl+c,然后输入y
3.springboot + Elasticsearch
注意:springboot集成elasticsearch时,版本上必须一致才行
例如 我的springboot使用的时1.5.9版本,因此下载elasticsearch2.0 logstash-2.0.0 kibana4.2.0进行使用才行。
SpringBoot和Elasticsearch 之间对应版本的关系。
| Spring Boot Version (x) | Spring Data Elasticsearch Version (y) | Elasticsearch Version (z) |
|---|---|---|
| x <= 1.3.5 | y <= 1.3.4 | z <= 1.7.2* |
| x >= 1.4.x | 2.0.0 <=y < 5.0.0** | 2.0.0 <= z < 5.0.0** |
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
添加配置:
##es地址 spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
实体类:
@Document(indexName = "test",type = "goods")
//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
public class GoodsInfo implements Serializable {
private static final long serialVersionUID = -7682211945335253642L;
private Long id;
private String name;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public GoodsInfo(Long id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
public GoodsInfo() {
}
}
DAO:
创建GoodsRepository,继承ElasticsearchRepository,代码如下:
import com.lolaage.system.model.GoodsInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component; @Component
public interface GoodsDao extends ElasticsearchRepository<GoodsInfo,Long> {
}
最后新建一个controller进行测试,包含简单的增删改查
@RequestMapping("/goods")
@RestController
public class GoodsController {
@Autowired
private GoodsDao goodsDao;
//http://localhost:8888/save
@GetMapping("/save")
public String save(){
GoodsInfo goodsInfo = new GoodsInfo(123l,
"商品"+123,"这是一个测试商品");
goodsDao.save(goodsInfo);
return "success";
}
//http://localhost:8888/delete?id=1525415333329
@GetMapping("/delete")
public String delete(long id){
goodsDao.delete(id);
return "success";
}
//http://localhost:8888/update?id=1525417362754&name=修改&description=修改
@GetMapping("/update")
public String update(long id,String name,String description){
GoodsInfo goodsInfo = new GoodsInfo(id,
name,description);
goodsDao.save(goodsInfo);
return "success";
}
//http://localhost:8888/getOne?id=1525417362754
@GetMapping("/getOne")
public GoodsInfo getOne(long id){
GoodsInfo goodsInfo = goodsDao.findOne(id);
return goodsInfo;
}
//每页数量
private Integer PAGESIZE=10;
//http://localhost:8888/getGoodsList?query=商品
//http://localhost:8888/getGoodsList?query=商品&pageNumber=1
//根据关键字"商品"去查询列表,name或者description包含的都查询
@GetMapping("/getGoodsList")
public List<GoodsInfo> getList(Integer pageNumber, String query){
if(pageNumber==null) pageNumber = 0;
//es搜索默认第一页页码是0
SearchQuery searchQuery=getEntitySearchQuery(pageNumber,PAGESIZE,query);
Page<GoodsInfo> goodsPage = goodsDao.search(searchQuery);
return goodsPage.getContent();
}
private SearchQuery getEntitySearchQuery(int pageNumber, int pageSize, String searchContent) {
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
.add(QueryBuilders.matchPhraseQuery("name", searchContent),
ScoreFunctionBuilders.weightFactorFunction(100))
.add(QueryBuilders.matchPhraseQuery("description", searchContent),
ScoreFunctionBuilders.weightFactorFunction(100))
//设置权重分 求和模式
.scoreMode("sum")
//设置权重分最低分
.setMinScore(10);
// 设置分页
Pageable pageable = new PageRequest(pageNumber, pageSize);
return new NativeSearchQueryBuilder()
.withPageable(pageable)
.withQuery(functionScoreQueryBuilder).build();
}
}
测试
添加数据:http://localhost:8093/goods/save

查询数据:http://localhost:8093/goods/getOne?id=123

在kibana上进行查看elasticsearch中的数据
根据测试代码中IndexName : test创建一个模式匹配
点击Settings-->Indices->去掉Index contains
time-based events 的勾,

点击Discover

springboot集成elk 一: springboot + Elasticsearch的更多相关文章
- springboot集成elk 三:springboot + Elasticsearch Rest-Client
注: 该集成方式,对Elasticsearch无版本限制,但是需要自行封装请求,解析结果等. <dependency> <groupId>org.elasticsearch. ...
- springboot 集成 elk 日志收集功能
Lilishop 技术栈 官方公众号 & 开源不易,如有帮助请点Star 介绍 官网:https://pickmall.cn Lilishop 是一款Java开发,基于SpringBoot研发 ...
- 【Elastic-2】SpringBoot整合ELK、SpringBoot写ES
ELK相关TODO 快速开始文档(https://www.cnblogs.com/lbhym/p/15934416.html) SpringBoot整合ELK ELK接入Kafka(待Kafka快速开 ...
- SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源
SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...
- springboot集成elk 四:springboot + Elasticsearch+Jest
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- springboot集成elk实现分布式日志管理
1.安装elk https://www.cnblogs.com/xuaa/p/10769759.html 2.idea创建springboot项目 File -> New -> Proje ...
- springboot集成elk 二:springboot + elk 采集日志
到logstash-2.0.0\bin下新建文件 logstash.conf input { tcp { mode => "server" host => " ...
- springboot集成Guava缓存
很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...
- SpringBoot图文教程14—SpringBoot集成EasyExcel「上」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
随机推荐
- leetcode解题报告(17):Missing Number
描述 Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mis ...
- hihoCoder 2 * problem
1792 模拟,转化为二进制后逐位比较 1819 线段树维护区间加 维护每个数加了多少 每次弹出栈顶元素后栈顶位置注意清空 1792 #include <iostream> #includ ...
- ubuntu中防火墙iptables配置
特别说明:此文章完全转载于https://www.cnblogs.com/EasonJim/p/6851007.html 1.查看系统是否安装防火墙 root@localhost:/usr# whic ...
- mac 安装 mysql 5.7
下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下一步,经过一系列安装步骤后,会跳出一个这样的界面,请注意!!! 上面红框中是你 ...
- c++ rapidjson读取json文件 解析
库:链接:https://pan.baidu.com/s/1UChrgqLPJxKopyqShDCHjg 密码:3yhz #include <iostream> #include < ...
- Arrays.toString的作用
Arrays.toString()的作用是用来很方便地输出数组,而不用一个一个地输出数组中的元素. 这个方法是是用来将数组转换成String类型输出的,入参可以是long,float,double,i ...
- 7个最好的Java机器学习开发库
摘要:现如今,拥有深度学习和机器学习领域的技术是科技界的趋势之一,并且企业则希望雇佣一些拥有良好的机器学习知识背景的程序开发工程师.本文将介绍一些目前流行的.强大的基于Java的机器学习库,希望给大家 ...
- BitmapShader填充图形
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.*; imp ...
- Carve Visual Studio2015编译
下载Carve库 https://github.com/folded/carve 目录结构如下: 用Visual Studio2015打开,点击右键,生成即可 在bin目录下生成了 .lib文件 ...
- CentOS7下搭建zabbix监控(一)——Zabbix监控端配置
zabbix 是一个基于 WEB 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix 能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位 ...