分布式日志收集ELK

SpringBoot集成ELK实现分布式日志收集

作者:Bob Zhang01
前两天项目中想用ELK收集日志,用了一天时间查了很多资料,踩了很多坑,一步步找到了解决办法,本文通过安装和集成springboot,主要解决ElasticSearc、Logstash、Kibana配置及集成到springboot中的一些设置问题,分为安装和代码案例两部分(代码在最后),供初学者参考(不足的地方请指教);
一. ELK简介

由于框架是java写的所以依赖jdk,安装前先安装jdk,我的版本是jdk 1.8.0_191

Elasticsearch:简称ES,是一个基于Lucene、分布式、RESTful web接口的全文搜索引擎,同时也是一个面向文档的数据库,用于存储日志信息。

安装:我是用的mac,直接用brew安装的:

$ brew install elasticsearch

linux和windows安装也很简单,主要是修改配置文件:找到安装目录,下的/config/elasticsearch.yml配置文件,主要修改四个地方:也就是没有被#注释的地方

1.cluster.name: elasticsearch_toudaizhi 此处名字默认就行,我的默认就是elasticsearch_toudaizhi

2.path.data: /usr/local/var/lib/elasticsearch_toudaizhi/

3.path.logs: /usr/local/var/log/elasticsearch/

4.network.host: 192.168.0.1,此处替换自己的ip

# ==================Elasticsearch Configuration =====================

#

# NOTE: Elasticsearch comes with reasonable defaults for most settings.

#      Before you set out to tweak and tune the configuration, make sure you

#      understand what are you trying to accomplish and the consequences.

#

# The primary way of configuring a node is via this file. This template lists

# the most important settings you may want to configure for a production cluster.

#

# Please consult the documentation for further information on configuration options:

# https://www.elastic.co/guide/en/elasticsearch/reference/index.html

#

# ---------------------------------- Cluster -----------------------------------

#

# Use a descriptive name for your cluster:

#

cluster.name: elasticsearch_toudaizhi

#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /usr/local/var/lib/elasticsearch_toudaizhi/
#
# Path to log files:
#
path.logs: /usr/local/var/log/elasticsearch/
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

修改完成保存,启动:

./elasticsearch

浏览器访问:http://自己的ip:9200 (自己的ip,一定要访问成功,后面很多地方要使用),显示下图表示安装成功

image

网上说不能以root权限启动,我没有遇到,解决办法很多,这里省略。

Logstash:是一个开源的服务器端数据处理管道,可以同时从多个数据源获取数据,并对其进行转换。

安装:$ brew install logstash

安装完成不用修改配置文件,需要添加一个配置文件,在安装目录下的/bin/目录下新建conf目录,在conf目录下创建logstash-indexer.conf文件,文件名可以自己设置。添加如下配置:

input {

  file {

    path =>["/users/toudaizhi/log/logstash/info.*.log","/users/toudaizhi/log/logstash/error.*.log"]

  }

}

output {

  elasticsearch {

    hosts => ["自己的ip:9200"]

  }

  stdout {

    codec => rubydebug

  }

}

其中,需要替换 path =>["/users/toudaizhi/log/logstash/info..log","/users/toudaizhi/log/logstash/error..log"]

因为我的日志是根据时间滚动存储的,所以设置的logstash的input是file,也可以设置tcp,

tcp {

    port => 4560

    codec => json_lines

  }

注意:文件位置和名称要和springboot中的日志目录和名称一致。

启动:

./logstash -f conf/logstash-indexer.conf

终端显示:Successfully started Logstash API endpoint {:port=>9600}表示启动成功;

找到logstash-indexer.conf 文件中path=>对应的文件,在其中添加一些内容:例如添加:Hi,What are you doing?

logstash会打印出:

image

Kibana:提供可搜索的Web可视化界面,查看数据简单、方便

安装:

$ brew install kibana

设置配置文件:找到安装目录下的/config/kibana.yml

修改

1.server.port: 5601,去掉注释

2.server.host: 自己的ip,去掉注释,替换成自己的ip

3.elasticsearch.hosts: "http://自己的ip:9200" ,去掉注释,替换成自己的ip

# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: 自己的ip

# Enables you to specify a path to mount Kibana at if you are running behind a proxy.
# Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
# from requests it receives, and to prevent a deprecation warning at startup.
# This setting cannot end in a slash.
#server.basePath: ""

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0.
#server.rewriteBasePath: false

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URLs of the Elasticsearch instances to use for all your queries.
elasticsearch.hosts: "http://自己的ip:9200"

# When this setting's value is true Kibana uses the hostname specified in the server.host
# setting. When the value of this setting is false, Kibana uses the hostname of the host
# that connects to this Kibana instance.
#elasticsearch.preserveHost: true

# Kibana uses an index in Elasticsearch to store saved searches, visualizations and
# dashboards. Kibana creates a new index if the index doesn't already exist.
#kibana.index: ".kibana"

# The default application to load.
#kibana.defaultAppId: "home"

# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
#elasticsearch.username: "user"
#elasticsearch.password: "pass"

# Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
# These settings enable SSL for outgoing requests from the Kibana server to the browser.
#server.ssl.enabled: false
#server.ssl.certificate: /path/to/your/server.crt
#server.ssl.key: /path/to/your/server.key

# Optional settings that provide the paths to the PEM-format SSL certificate and key files.
# These files validate that your Elasticsearch backend uses the same key files.
#elasticsearch.ssl.certificate: /path/to/your/client.crt
#elasticsearch.ssl.key: /path/to/your/client.key

# Optional setting that enables you to specify a path to the PEM file for the certificate
# authority for your Elasticsearch instance.
#elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]

# To disregard the validity of SSL certificates, change this setting's value to 'none'.
#elasticsearch.ssl.verificationMode: full

# Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
# the elasticsearch.requestTimeout setting.

修改完成,保存后,浏览器访问http://自己的ip:5601,

第一次登录

image

点击第一个按钮,进入

image

第一次,进入会提示

In order to visualize and explore data in Kibana, you'll need to create an index pattern to retrieve data from Elasticsearch.

就是需要创建一个索引

image

我用的是第二个,logstash-2019.04.29,是logstash默认的

填写logstash-2019.04.29后,Next step变为可点击

image

下一步,

image

点击Create index pattern,

image

点击Discover,在message中查看到自己的日志

image

二.代码部分

第一次用简书,格式控制不好,所以把代码传到了github上了,地址:

https://github.com/ZhangBob01/demo.git

其中把logback-spring.xml文件中的自己的ip替换一下

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,558评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,002评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,024评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,144评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,255评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,295评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,068评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,478评论 1 305
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,789评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,965评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,649评论 4 336
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,267评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,982评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,800评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,847评论 2 351