一、配置
系统: Windows 8.1
elasticsearch:5.5.1
logstash:2.0.0
kibana:5.5.1
注:由于实验性搭建,选择windows系统,但选择Linux系统效果更佳
二、部署方案
1.ELK+Redis
2.ELK+Kafka
注:本次搭建选用第一种方案
三、安装
前提:下载nssm
1. Elasticsearch
下载: download
2. logstash
下载: download
3. kibana
下载: download
注册为windows服务
(a) 将下载的nssm.exe分别拷贝到Elasticsearch、logstash和kibana解压后的bin目录下,然后CMD进入bin执行nssm install 服务名,例如Elasticsearch 的执行nssm install elasticsearch-service..
(b) 分析选择path为各压缩包的bin目录下的elasticsearch.bat、logstash.bat和kibana.bat
(c) Details选项卡设置显示名为Windows名
(d) 最后选择Install service
四、部署
1. 创建Maven项目elk-log(可另外取名),pom文件为:
1 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
2. 配置logback,logback.xml文件为:
1 | xml version="1.0" encoding="UTF-8" |
3.设置项目定时任务(打日志)
定时任务类LogProducer:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25package com.suncj.elk;
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 日志生成器<br>
* 版权:Copyright (c) 2015-2016<br>
* 创建日期:2017年8月5日<br>
*/
public class LogProducer {
private static final Logger log = LoggerFactory.getLogger(LogProducer.class);
private Random rand = new Random();
private static int logId = 0;
public void produce() {
log.info("log_id: {} , content:{}", logId, String.format("I am %s", logId + rand.nextInt(100000)));
logId++;
}
}
项目启动类:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24package com.suncj.elk;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
private static Logger logger = LoggerFactory.getLogger(Application.class);
public static ApplicationContext appContext;
public static void main(String[] args) {
try {
logger.info("准备加载程序");
appContext = new ClassPathXmlApplicationContext("app-*.xml");
logger.info("加载完成");
} catch (Exception e) {
logger.error("主程序出错:", e);
}
}
}
其他配置文件:app-task.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16xml version="1.0" encoding="UTF-8"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="logProducer" class="com.suncj.elk.LogProducer"></bean>
<task:scheduled-tasks>
<task:scheduled ref="logProducer" method="produce"
cron="0/5 * * * * *" />
</task:scheduled-tasks>
</beans>
2. logstash配置
(a) run_es.bat,run_redis.bat
1 | logstash.bat agent -f logstash_es.conf |
(b) logstash_redis.conf
1 | input { |
(c) logstash_es.conf
1 | input { |
注: logstash注册为windows服务时需要
创建两个bat文件,一个用于项目日志存储到redis;另外一个用户读取redis,输出到elasticsearch,因此需要注册两个服务名不同的windows服务
参考资料
https://kibana.logstash.es/content/kibana/index.html
http://blog.csdn.net/tulizi/article/details/52972824
http://udn.yyuap.com/doc/logstash-best-practice-cn/input/redis.html
https://www.elastic.co/guide/en/logstash/current/codec-plugins.html
