搭建ELK日誌系統並集成到Spring Cloud

ELK分別是Elasticsearch、Logstash、Kibana三個開源框架縮寫。

下載地址:

Elasticsearch 官網elasticsearch-6.3.0.tar elasticsearch官方文檔
Kibana 官網kibana-6.3.0下載 linux64位 kibana官方文檔
Logstash 官網logstash-6.3.0.tar logstash官方文檔
Filebeat 官網filebeat-6.3.0 linux64位 beats官方文檔

下面是我分享的百度雲地址下載:

鏈接:https://pan.baidu.com/s/1SqicOuvKuzfSjAz-xKb3_g 
提取碼:uuwc

 

1、下載好了以後我們將壓縮包上傳到linux的home目錄下,我的是阿里雲的centos7

cd /home
rz

如果沒有rz的同伴也可以通過xftp上傳文件

2、上傳完畢,使用tar命令解壓:

tar -zxvf elasticsearch-6.3.0.tar.gz          
tar -zxvf kibana-6.3.0-linux-x86_64.tar.gz
tar -zxvf filebeat-6.3.0-linux-x86_64.tar.gz  
tar -zxvf logstash-6.3.0.tar.gz

3、安裝java環境1.8

      教程:https://blog.csdn.net/qq_25652213/article/details/89923127

4、安裝elasticsearch

修改配置文件

vi /home/elasticsearch-6.3.0/config/elasticsearch.yml

找到裏面的network.host和http.port解開註釋

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0           ##服務器ip 本機
#
# Set a custom port for HTTP:
#
http.port: 9200                 ##服務端口
#
# For more information, consult the network module documentation.
#

elasticsearch啓動只允許非root用戶,我們建一個用戶並且授權啓動:

groupadd elsearch         #添加用戶組 elsearch 

useradd elsearch -g elsearch -p elasticsearch  
#添加用戶 elsearch 密碼爲 elasticsearch 到用戶組 elsearch

chown -R elsearch:elsearch  elasticsearch-6.3.0   
#將elsearch安裝目錄授權給 用戶組:用戶  即 elsearch:elsearch

su elsearch  #切換 elsearch 用戶
/home/elasticsearch-6.3.0/bin/elasticsearch   #重新啓動

常見問題:elasticsearch啓動常見問題

啓動報錯:

ERROR: [2] bootstrap checks failed
[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

裏面出現的四個錯誤需要我們修改四個conf文件

exit #先退出到root賬號

問題1:
    [1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

解決方法:

編輯 /etc/security/limits.conf,追加以下內容;
* soft nofile 65536
* hard nofile 65536
此文件修改後需要重新登錄用戶,纔會生效


問題2:
    [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解決方法:

編輯 /etc/sysctl.conf,追加以下內容:
vm.max_map_count=655360
保存後,執行:
sysctl -p

問題3:
    [1]: max number of threads [1024] for user [elsearch] is too low, increase to at least [4096]

解決方法:
    切換到root賬號:su root 或者 exit
    執行命令:vi /etc/security/limits.d/90-nproc.conf
    修改前:*          soft    nproc     1024
    修改後:*          soft    nproc     4096

問題4:
    [2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
    
解決方法:
    找到config/elasticsearch.yml文件,末尾加上:
    bootstrap.system_call_filter: false

啓動elasticsearch:

/home/elasticsearch-6.3.0/bin/elasticsearch   #命令窗運行
/home/elasticsearch-6.3.0/bin/elasticsearch  -d  #後臺線程運行

關閉elasticsearch:

ctrl+c                                   #命令窗關閉

ps -ef | grep elastic                    #後臺線程關閉
kill -9 4442                             ##pid 4442爲查處線程的pid 

解決後啓動正常,訪問地址:http://101.201.140.220:9200/(ip更換爲自己系統的ip)

成功截圖:

5、安裝kibana

修改配置文件

vi /home/kibana-6.3.0-linux-x86_64/config/kibana.yml

修改代碼:

server.port: 5601       ##服務端口
server.host: "0.0.0.0"  ##服務器ip  本機
 
elasticsearch.url: "http://localhost:9200" ##elasticsearch服務地址 與elasticsearch對應

啓動kibana:

/home/kibana-6.3.0-linux-x86_64/bin/kibana       #命令窗啓動
nohup ./kibana-6.3.0-linux-x86_64/bin/kibana &   #後臺線程啓動

關閉kibana:

ctrl+c                                   #命令窗關閉
ps -ef | grep kibana                    #後臺線程關閉
kill -9 4525                             ##pid 4525 爲查處線程的pid 

訪問地址:http://101.201.140.220:5601(ip更換爲自己系統的ip)

6、安裝logstash

新建配置文件:

vi /home/logstash-6.3.0/config/logback-es.conf

內容(去除多餘空格,保持yml文件規範。):

input {
    tcp {  
        port => 9601  
        codec => json_lines         
    }
}
output {
        elasticsearch {
                hosts => "localhost:9200"
        }
        stdout { codec => rubydebug}
}

安裝logstash json插件:

/home/logstash-6.3.0/bin/logstash-plugin install logstash-codec-json_lines

啓動logstash:

/home/logstash-6.3.0/bin/logstash -f /home/logstash-6.3.0/config/logback-es.conf         ##命令窗形式

nohup /home/logstash-6.3.0/bin/logstash -f /home/logstash-6.3.0/config/logback-es.conf &  ##後臺線程形式

關閉logstash:

ctrl+c                                   #命令窗關閉

ps -ef | grep logstash                    #後臺線程關閉
kill -9 4617                              ##pid 4617 爲查處線程的pid 

7、項目中集成elk

pom文件引入:

               <dependency>
			<groupId>net.logstash.logback</groupId>
			<artifactId>logstash-logback-encoder</artifactId>
			<version>4.11</version>
		</dependency>

配置logback.xml文件,放到resources下面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>192.168.99.94:9601</destination>     <!--指定logstash ip:監聽端口 tcpAppender  可自己實現如kafka傳輸等-->
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
    </appender>

    <include resource="org/springframework/boot/logging/logback/base.xml"/>      <!--引用springboot默認配置-->

    <root level="INFO">
        <appender-ref ref="LOGSTASH" />                                           <!--使用上述訂閱logstash數據tcp傳輸 -->
        <appender-ref ref="CONSOLE" />                                            <!--使用springboot默認配置 調試窗口輸出-->
    </root>

</configuration>

代碼測試:

package com.ys.ribbon;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient //註冊服務
public class RibbonProviderApplication {

    private final static Logger logger = LoggerFactory.getLogger(RibbonProviderApplication.class);


    public static void main(String[] args) {
        logger.info("---test---"+"你好,安餘生");
        SpringApplication.run(RibbonProviderApplication.class, args);
    }


    @Autowired
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

在linux中查看logstash日誌就可以看到有了我們輸出的日誌信息,但是linux我沒有配置中文展示所以亂碼,但是並不影響:

我們去kibana頁面看一下,可以看到我們的信息已經展示在這裏,配置好索引後可以選擇生成可視化圖例。

elk的搭建使用就到這裏了,以後我會將elk的配置和使用梳理寫出來。

 

參考博客:https://blog.csdn.net/qq_22211217/article/details/8076456

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章