Spring Cloud學(xué)習(xí)day108:ELK

一、ELK介紹

1.ELK解決了什么問題?

示例
  • ELK的介紹:


    示例
  • ELK的架構(gòu)原理:


    示例

二、安裝ELK

1.安裝ELasticSearch:

  • 安裝準(zhǔn)備:

1.安裝elasticsearch時(shí)linux內(nèi)核必須是3.5+。
2.jdk必須是jdk1.8.0_131以上版本。
3.如果是在虛擬機(jī)上安裝,虛擬機(jī)分配至少1.5G以上的內(nèi)存。

  • Linux內(nèi)核升級(jí)步驟:

查看Linux內(nèi)核"uname -a";安裝的Linux為CentOS-7。
(1)執(zhí)行命令:

rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7
yum update nss

(2)執(zhí)行:

rpm -Uvh http://www.elrepo.org/elrepo-release-6-8.el6.elrepo.noarch.rpm

(3)執(zhí)行:

yum --enablerepo=elrepo-kernel install kernel-lt -y

(4)修改grub.conf文件,確保使用新內(nèi)核啟動(dòng):

修改的內(nèi)容:default=0
reboot重啟系統(tǒng)。

vim /etc/grub.conf
  • 安裝elasticsearch:

下載:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz
解壓:tar -zxvf elasticsearch-6.2.3.tar.gz

解壓完成后拷貝到指定目錄
  • 修改elasticsearch需要的系統(tǒng)配置:
    (1)修改文件的命令:
vi /etc/security/limits.conf

(2)增加內(nèi)容:

* soft nofile 65536
* hard nofile 65536
  • 修改線程池最低容量:
    (1)修改文件:
vi /etc/security/limits.d/90-nproc.conf

(2)修改內(nèi)容:

*          soft    nproc     4096
root       soft    nproc     unlimited

(3)新增下述內(nèi)容:

命令:vi /etc/sysctl.conf
新增下述內(nèi)容。

vm.max_map_count=655360
  • 設(shè)置可訪問的客戶端:

修改elasticsearch的配置文件,設(shè)置可訪問的客戶端。0.0.0.0代表任意客戶端訪問。
(1)修改文件:

vi config/elasticsearch.yml

(2)修改內(nèi)容:

etwork.host: 0.0.0.0
http.port: 9200
使用sysctl -p 讓配置生效
  • 創(chuàng)建用戶:

從5.0開始,ElasticSearch 安全級(jí)別提高了,不允許采用root帳號(hào)啟動(dòng),所以我們要添加一個(gè)用戶。

(1)創(chuàng)建用戶組:

groupadd elk

(2)創(chuàng)建用戶:

useradd admin
passwd admin

(3)將admin用戶添加到elk組:

usermod -G elk admin

(4)設(shè)置sudo權(quán)限:

輸入命令“visudo”。找到root ALL=(ALL) ALL一行,添加oldlu用戶,如下。

# Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
#找到后添加如下內(nèi)容
admin      ALL=(ALL)       ALL

(5)為用戶分配權(quán)限:

chown -R admin:elk /usr/local/elasticsearch
  • 切換用戶:
su admin
示例
  • ElasticSearch的啟動(dòng)和停止:
    (1)啟動(dòng):
./elasticsearch -d
示例

(2)驗(yàn)證是否啟動(dòng)成功:

#Linux服務(wù)器中測(cè)試
curl http://192.168.70.140:9200
#在瀏覽器中訪問
http://192.168.70.140:9200/
瀏覽器的信息

Linux中的信息

二、安裝Head插件

1.Head插件簡(jiǎn)介:

ElasticSearch-head是一個(gè)H5編寫的ElasticSearch集群操作和管理工具,可以對(duì)集群進(jìn)行傻瓜式操作。
它提供了:
(1)索引和節(jié)點(diǎn)級(jí)別操作。
(2)搜索接口能夠查詢集群中原始json或表格格式的檢索數(shù)據(jù)。
(3)能夠快速訪問并顯示集群的狀態(tài)。

2.安裝步驟:

  • 安裝NodeJS:

要求在root下執(zhí)行下面的命令。

curl -sL https://rpm.nodesource.com/setup_8.x | bash -
yum install -y nodejs
示例

示例
  • 安裝npm:
npm install -g cnpm --registry=https://registry.npm.taobao.org
示例
  • 使用npm安裝grunt:
npm install -g grunt
npm install -g grunt-cli --registry=https://registry.npm.taobao.org --no-proxy
示例

示例
  • 查看以上版本:

切換到admin用戶“su admin”。

node -v
npm -v
grunt -version
示例
  • 下載head插件源碼:

進(jìn)入/usr/local/下創(chuàng)建目錄“mkdir es”。

#下載
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
#解壓
unzip master.zip 
示例

下載
  • 使用國內(nèi)鏡像安裝:
#進(jìn)入目錄
cd elasticsearch-head-master
#下載
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo cnpm install
示例

示例

3.配置:

  • 配置ElasticSearch,使得HTTP對(duì)外提供服務(wù):

編輯文件:“vi config/elasticsearch.yml”;
添加下面的內(nèi)容:

# 增加新的參數(shù),這樣head插件可以訪問es。設(shè)置參數(shù)的時(shí)候:后面要有空格
        http.cors.enabled: true
        http.cors.allow-origin: "*"
示例

編輯
  • 修改Head插件配置文件:

"vi Gruntfile.js";找到connect:server,添加hostname一項(xiàng),如下:

        connect: {
            server: {
                    options: {
                            hostname: '0.0.0.0',
                            port: 9100,
                            base: '.',
                            keepalive: true
                    }
            }
    }
進(jìn)入文件編輯
示例
  • 啟動(dòng):
    (1)重啟:
./elasticsearch -d

(2)啟動(dòng)head:

elasticsearch-head-master目錄下:
        grunt server
        或  npm run start
示例

瀏覽器訪問
  • 簡(jiǎn)單應(yīng)用:
    (1)創(chuàng)建索引:
curl -XPUT http://192.168.70.140:9200/applog

(2)查看head變化:

http://192.168.226.132:9100/
創(chuàng)建索引

示例

三、安裝Logstash

1.下載解壓:

(1)下載:

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.2.3.tar.gz

(2)解壓:

tar zxvf logstash-6.2.3.tar.gz
示例

(3)復(fù)制:


示例

2.測(cè)試:

./bin/logstash -e 'input { stdin { } } output { stdout {} }'

3.修改配置:

在 logstash的主目錄下,“vim config/log_to_es.conf”;添加內(nèi)容如下:

# For detail structure of this file  
    # Set: https://www.elastic.co/guide/en/logstash/current/configuration-file-structure.html  
    input {  
      # For detail config for log4j as input,   
      # See: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-log4j.html  
      tcp {  
        mode => "server"  
        host => "192.168.226.132"  
        port => 9250  
      }  
    }
    filter {  
      #Only matched data are send to output.  
    }  
    output {  
      # For detail config for elasticsearch as output,   
      # See: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html  
      elasticsearch {  
        action => "index"          #The operation on ES  
        hosts  => "192.168.226.132:9200"   #ElasticSearch host, can be array.  
        index  => "applog"         #The index to write data to.  
      }  
    }  
  • 啟動(dòng):
./bin/logstash -f config/log_to_es.conf 
或 后臺(tái)運(yùn)行守護(hù)進(jìn)程 ./bin/logstash -f config/log_to_es.conf &
  • 測(cè)試:

curl 'http://192.168.70.140:9200/_search?pretty'

示例

四、安裝Kibana

1.下載解壓:

(1)下載:

wget https://artifacts.elastic.co/downloads/kibana/kibana-6.2.3-linux-x86_64.tar.gz

(2)解壓:

tar zxvf kibana-6.2.3-linux-x86_64.tar.gz
示例

(3)復(fù)制:


示例

2.修改配置:

“ vim config/kibana.yml ”,把以下注釋放開,使配置起作用。

    server.port: 5601
    server.host: "0.0.0.0"
    elasticsearch.url: http://192.168.70.140:9200
    kibana.index: ".kibana"
示例
  • 啟動(dòng):
./bin/kibana 
  • 測(cè)試:

http://192.168.226.132:5601/app/kibana

示例

3.Kibana操作界面:

  • Discover:

Discover 主要是做索引查詢,功能非常強(qiáng)大 。

  • Visualize :

視圖展示,支持許多風(fēng)格。

  • Dashboard:

圖表展示,一個(gè)儀表板顯示 Kibana 保存的一系列可視化。

  • Timelion:

Timelion 是一個(gè)時(shí)間序列數(shù)據(jù)的可視化功能,可以結(jié)合在一個(gè)單一的可視化完全獨(dú)立的 數(shù)據(jù)源。它是由一個(gè)簡(jiǎn)單的表達(dá)式語言驅(qū)動(dòng)的,你用來檢索時(shí)間序列數(shù)據(jù),進(jìn)行計(jì)算,找出 復(fù)雜的問題的答案,并可視化的結(jié)果。這個(gè)功能由一系列的功能函數(shù)組成,同樣的查詢的結(jié) 果,也可以通過 Dashboard 顯示查看。

  • DevTools:

可以直接操作 es 中的數(shù)據(jù),使用戶方便的通過瀏覽器直接與 Elasticsearch 進(jìn)行交互。

  • Management:

管理中的應(yīng)用是在你執(zhí)行你的運(yùn)行時(shí)配置 kibana,包括初始設(shè)置和指標(biāo)進(jìn)行配置模式, 高級(jí)設(shè)置,調(diào)整自己的行為和 Kibana,各種“對(duì)象”,你可以查看保存在整個(gè) Kibana 的內(nèi)容 如發(fā)現(xiàn)頁,可視化和儀表板。

五、Spring Cloud和ELK的集成

1.創(chuàng)建Sleuth-Product-Service-elk:

復(fù)制項(xiàng)目。

示例

2.創(chuàng)建Sleuth-Product-Provider-elk:

復(fù)制項(xiàng)目進(jìn)行修改。

示例
  • 修改POM文件:
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 添加 product-service 坐標(biāo) -->
        <dependency>
            <groupId>com.zlw</groupId>
            <artifactId>springcloud-sleuth-product-service-elk</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.0</version>
        </dependency>
  • 修改配置文件:
spring.application.name=sleuth-product-provider-elk
server.port=9001

#設(shè)置服務(wù)注冊(cè)中心
eureka.client.serviceUrl.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#----mysql-db-------
mybatis.type-aliases-package=com.book.product.pojo
mybatis.mapper-locations==classpath:com/book/product/mapper/*.xml

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book-product?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
  • 添加logback.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--該日志將日志級(jí)別不同的log信息保存到不同的文件中 -->
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />

    <springProperty scope="context" name="springAppName"
        source="spring.application.name" />

    <!-- 日志在工程中的輸出位置 -->
    <property name="LOG_FILE" value="${BUILD_FOLDER:-build}/${springAppName}" />

    <!-- 控制臺(tái)的日志輸出樣式 -->
    <property name="CONSOLE_LOG_PATTERN"
        value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />

    <!-- 控制臺(tái)輸出 -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
        <!-- 日志輸出編碼 -->
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>

    <!-- 為logstash輸出的JSON格式的Appender -->
    <appender name="logstash"
        class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>192.168.226.132:9250</destination>
        <!-- 日志輸出編碼 -->
        <encoder
            class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                        "severity": "%level",
                        "service": "${springAppName:-}",
                        "trace": "%X{X-B3-TraceId:-}",
                        "span": "%X{X-B3-SpanId:-}",
                        "exportable": "%X{X-Span-Export:-}",
                        "pid": "${PID:-}",
                        "thread": "%thread",
                        "class": "%logger{40}",
                        "rest": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>

    <!-- 日志輸出級(jí)別 -->
    <root level="DEBUG">
        <appender-ref ref="console" />
        <appender-ref ref="logstash" />
    </root>
</configuration>

創(chuàng)建Consumer:

復(fù)制項(xiàng)目,添加Logback.xml文件。

示例
  • 測(cè)試:


    示例
示例

示例
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、ELK平臺(tái)介紹 在搜索ELK資料的時(shí)候,發(fā)現(xiàn)這篇文章比較好,于是摘抄一小段:以下內(nèi)容來自:http://bai...
    螺旋上升的世界閱讀 3,373評(píng)論 0 20
  • 1、ES簡(jiǎn)介 ES=elaticsearch簡(jiǎn)寫, Elasticsearch是一個(gè)開源的高擴(kuò)展的分布式全文檢索引...
    運(yùn)維貓閱讀 546評(píng)論 0 0
  • 環(huán)境: 10.95.196.143 CentOS Linux release 7.4.1708 (Core) 準(zhǔn)備...
    cubotudo閱讀 404評(píng)論 0 1
  • 一、安裝kibana 下載地址https://artifacts.elastic.co/downloads/kib...
    Al_不期而遇閱讀 736評(píng)論 0 0
  • 本期簡(jiǎn)說提高軟件產(chǎn)品質(zhì)量有三個(gè)方面需要把握(本篇文章談ELK的使用): 一、持續(xù)集成 二、自動(dòng)化 三、ELK日志分...
    隔壁老田_閱讀 2,612評(píng)論 0 2

友情鏈接更多精彩內(nèi)容