ELK相关TODO

  • 快速开始文档(https://www.cnblogs.com/lbhym/p/15934416.html)
  • SpringBoot整合ELK
  • ELK接入Kafka(待Kafka快速开始文档完成之后)

Kafka相关TODO

  • Kafka快速开始文档,包含下载、配置、启动、Java Client等
  • 管理Kafka及常见问题解决,包含Kafka Manager、AdminClient、Kafka命令行说明(Shell脚本)

前言

​ 快速开始文档中,讲解了ELK三个组件的下载、安装、配置、启动等过程。只要按照文章走一下,就可以看到一个单机版的ELK三件套。本文会带你整合SpringBoot、ELK、Kafka,组成最常见的日志系统。当然,这套组合不仅能作为日志系统,也能作为大数据流处理的前半部分(数据的收集)。后面也会带来大数据相关的随笔文章。本文也会附带相关源码,链接如下:

Github:https://github.com/MrLing1997/elasticsearch-study

依赖导入

​ 虽然整合的是SpringBoot,但是为了方便前期学习、理解,我们就不用SpringData Elasticsearch的starter了。在熟悉了ES官方提供的Java客户端后,可以再使用SpringData Elasticsearch,其提供了很多非常方便的注解。除了注解,还有starter提供的自动配置等功能。更多相关功能和用法可以自行去查看Spring Data官方文档或相关博客。

​ 这里我们只导入ES提供的Java客户端,然后手动去初始化ES。注意导入的版本,最好和服务器的ES版本保持一致。但是由于前段时间log4j的漏洞,导致7.14之前的版本全都被遗弃了,所以这里最低只能导入7.14版本。不过只要版本差距不大,一般不会有问题。

<!-- Elasticsearch服务 -->
<!-- 生成环境中,依赖版本最好和服务器的ES的版本保持一致,因为log4j的漏洞,7.14.0之前的部分依赖被遗弃无法成功下载-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
<!-- Elasticsearch Java高级客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>

初始化ES客户端

​ 首先去yml或properties添加配置信息。这个配置key不是上面的jar包提供的,而是我们自己自定义的,然后通过@Value注解获取值。所以你的key不一定要和我一样。配置如下:

elasticsearch:
host: ip
port: port

​ 然后初始化RestHighLevelClient即可:

@Configuration
@Slf4j
public class ElasticSearchConfig{
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port; @Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient restHighLevelClient = null;
try {
log.info("elasticsearch start init...");
restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
log.info("elasticsearch init success!");
}catch (Exception e){
log.error("elasticsearch init had exception:{}", ExceptionUtils.getStackTrace(e));
}
return restHighLevelClient;
}
}

造数据

​ 自己手动编两个数据总觉得不带劲,一是数据量太少,二是太麻烦。我这里推荐一个开源的,自动生成数据的工具,依赖如下,记得排除snakeyaml,可能会和你的springboot中的yaml产生冲突。

				<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
<exclusions>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
</exclusions>
</dependency>

​ 造数据的工具类和实体类很简单,直接贴一下代码:

@Data
@Accessors(chain = true)
public class User {
private Long id;
private String traceId;
private String name;
private String birthday;
private String job;
private String address;
private String company;
}
public class GenerateUserUtil {
private static final Faker faker = new Faker(); public static User generate(){
return new User()
.setId(System.currentTimeMillis())
.setTraceId(UUID.randomUUID().toString())
.setName(faker.name().name())
.setBirthday(DateFormat.format(faker.date().birthday(18,60)))
.setJob(faker.job().title())
.setAddress(faker.address().fullAddress())
.setCompany(faker.company().name());
}
}

往ES写数据

​ 往ES写数据之前,需要新建索引、定义mapping。根据你的实体类然后定义mapping即可。下面一共有三个类,ESConstant中定义了索引常量字符串和mapping。ESUtil封装了RestHighLevelClient,向外提供了创建索引和添加文档两个方法。WriteLogService模拟业务的服务类,不停产生数据和写日志。

public class ESConstant {
public static final String ES_USER_INDEX_PREFIX = "user"; public static final String MAPPING ="{\n" +
" \"properties\": {\n" +
" \"id\":{\n" +
" \"type\": \"long\"\n" +
" },\n" +
" \"traceId\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\"\n" +
" , \"analyzer\": \"standard\"\n" +
" },\n" +
" \"birthday\":{\n" +
" \"type\": \"date\"\n" +
" },\n" +
" \"job\":{\n" +
" \"type\": \"text\"\n" +
" , \"analyzer\": \"standard\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"text\"\n" +
" , \"analyzer\": \"standard\"\n" +
" },\n" +
" \"company\":{\n" +
" \"type\": \"text\"\n" +
" , \"analyzer\": \"standard\"\n" +
" }\n" +
" }\n" +
" }"; }
@Component
public class ESUtil {
@Autowired
RestHighLevelClient restHighLevelClient; public void createIndex(String indexName,String mapping,int shards,int replicas) throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
//设置索引的配置,1个分片1个副本。由于我们是单机ES,这个配置无关紧要,正式的线上环境记得要配置
HashMap<String,String> indexOption = new HashMap<>();
indexOption.put("index.number_of_shards",String.valueOf(shards));
indexOption.put("index.number_of_replicas",String.valueOf(replicas));
createIndexRequest.settings(indexOption);
//设置索引mapping,即字段的定义
createIndexRequest.mapping(mapping, XContentType.JSON);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
} public void addDocument(String document,String indexName) throws IOException {
IndexRequest indexRequest = new IndexRequest(indexName);
indexRequest.source(document,XContentType.JSON);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
}
@Service
@Slf4j
public class WriteLogService implements CommandLineRunner{
@Autowired
RestHighLevelClient restHighLevelClient;
@Autowired
ESUtil esUtil; private static final Gson gson = new GsonBuilder().serializeNulls().create(); @Override
public void run(String... args) {
try {
//运行前检查索引是否存在,不存在就新建一个
if (!restHighLevelClient.indices().exists(new GetIndexRequest(ES_USER_INDEX_PREFIX), RequestOptions.DEFAULT)) {
esUtil.createIndex(ES_USER_INDEX_PREFIX, MAPPING, 1, 1);
}
while (true) {
String user = gson.toJson(GenerateUserUtil.generate());
log.info("generate user:{}", user);
esUtil.addDocument(user, ES_USER_INDEX_PREFIX);
Thread.sleep(1000);
}
}catch (Exception e){
log.error("service had exception:{}", ExceptionUtils.getStackTrace(e));
}
}
}

​ 写入成功之后,就可以去Kibana的index Manager中添加user索引了。

【Elastic-2】SpringBoot整合ELK、SpringBoot写ES的更多相关文章

  1. ELK教程3:logstash的部署、SpringBoot整合ELK+Filebeat

    本篇文章主要讲解如下安装Logstash,logstash依赖于Java环境,首先安装Java,安装脚本如下: yum install java logstash安装 Logstash的安装脚本如下: ...

  2. 【Spring Cloud & Alibaba全栈开源项目实战】:SpringBoot整合ELK实现分布式登录日志收集和统计

    一. 前言 其实早前就想计划出这篇文章,但是最近主要精力在完善微服务.系统权限设计.微信小程序和管理前端的功能,不过好在有群里小伙伴的一起帮忙反馈问题,基础版的功能已经差不多,也在此谢过,希望今后大家 ...

  3. 8、SpringBoot整合之SpringBoot整合MongoDB

    SpringBoot整合MongoDB 一.创建项目,选择依赖 仅选择Spring Web.Spring Data MongoDB即可 二.引入相关依赖(非必要) 这里只是为了实体类的创建方便而引入l ...

  4. 3、SpringBoot整合之SpringBoot整合JDBC

    SpringBoot整合JDBC 一.创建SpringBoot项目 选择Spring Web.JDBC API.MySQL Driver 二.在pom配置文件中修改JDBC版本,导入lombok &l ...

  5. 2、SpringBoot整合之SpringBoot整合servlet

    SpringBoot整合servlet 一.创建SpringBoot项目,仅选择Web模块即可 二.在POM文件中添加依赖 <!-- 添加servlet依赖模块 --> <depen ...

  6. 1、SpringBoot整合之SpringBoot整合JSP

    SpringBoot整合JSP 一.创建SpringBoot项目,仅选择Web模块即可 二.在POM文件中添加依赖 <!-- 添加servlet依赖模块 --> <dependenc ...

  7. 5、SpringBoot整合之SpringBoot整合MybatisPlus

    SpringBoot整合MybatisPlus 目录(可点击直接跳转,但还是建议按照顺序观看,四部分具有一定的关联性): 实现基础的增删改查 实现自动填充功能 实现逻辑删除 实现分页 首先给出四部分完 ...

  8. 9、SpringBoot整合之SpringBoot整合SpringSecurity

    SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...

  9. SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...

随机推荐

  1. C 库函数 - pow()

    1.C 标准库 - <math.h> 2.C 库函数 double pow(double x, double y) 返回 x 的 y 次幂,即 xy. 3.pow() 函数的声明. dou ...

  2. 听不懂x86,arm?

    之前听别人讲x86或者ARM,我心里有一些疑惑,为什么他们不考虑32位还是64位的? 直到和师傅交流了一下: I:32位机是不是不支持部署k3os? T:这个年头哪里还有32位机? T:现在说x86, ...

  3. Java基础-JNI入门示例

    1.JNI是什么? JNI(Java Native Interface) Java本地接口,又叫Java原生接口.它允许Java调用C/C++的代码,同时也允许在C/C++中调用Java的代码. 可以 ...

  4. winfrom 双缓冲

    在窗体load函数中 this.DoubleBuffered = true; //控件,需要反射的方式设置 Type dgvType = this.dgv.GetType(); PropertyInf ...

  5. vscode控制台中文乱码

    原因 vscode中文控制台乱码原因是调用的cmd的显示. 所以问题实际上是cmd的显示中文乱码问题.当然还有其他方法仅仅修改vscode的显示,这里不在说明. cmd中国版本windows默认是93 ...

  6. DispatcherServlet的init源代码

    springmvc执行过程源代码分析 1,tomcat启动,创建容器的过程 通过load-on-start标签指定的1,创建DispatcherServlet对象, DispatcherServlet ...

  7. iptables匹配条件总结1

    源地址 -s选项除了指定单个IP,还可以一次指定多个,用"逗号"隔开即可 [root@web-1 ~]# iptables -I INPUT -s 172.16.0.116,172 ...

  8. 在海外上传文件到中国AWS S3

    s3cmd --access_key= --secret_key=xxxx --region=cn-north-1 --host=s3.cn-north-1.amazonaws.com.cn --ho ...

  9. 使用ajax登录验证,第一次点击登录按钮无反应,第二次点击才能正常运行。

    问题描述: 使用ajax进行登录验证时,第一次点击登录按钮无反应,第二次点击才能进去. 解决方法: 原来的代码 <form action="" method="po ...

  10. dubbo框架的使用方法。。。

    图解. 一.dubbo使用须知. 1.所有的service层必须要使用service注解(之前用的spring框架的,现在用dubbo框架所提供的@Service注解) // @Service(tim ...