Spring Cloud学习day108:ELK

一、ELK介绍

1.ELK解决了什么问题?

示例
  • ELK的介绍:


    示例
  • ELK的架构原理:


    示例

二、安装ELK

1.安装ELasticSearch:

  • 安装准备:

1.安装elasticsearch时linux内核必须是3.5+。
2.jdk必须是jdk1.8.0_131以上版本。
3.如果是在虚拟机上安装,虚拟机分配至少1.5G以上的内存。

  • Linux内核升级步骤:

查看Linux内核"uname -a";安装的Linux为CentOS-7。
(1)执行命令:

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

(2)执行:

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

(3)执行:

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

(4)修改grub.conf文件,确保使用新内核启动:

修改的内容:default=0
reboot重启系统。

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需要的系统配置:
    (1)修改文件的命令:
vi /etc/security/limits.conf

(2)增加内容:

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

(2)修改内容:

*          soft    nproc     4096
root       soft    nproc     unlimited

(3)新增下述内容:

命令:vi /etc/sysctl.conf
新增下述内容。

vm.max_map_count=655360
  • 设置可访问的客户端:

修改elasticsearch的配置文件,设置可访问的客户端。0.0.0.0代表任意客户端访问。
(1)修改文件:

vi config/elasticsearch.yml

(2)修改内容:

etwork.host: 0.0.0.0
http.port: 9200
使用sysctl -p 让配置生效
  • 创建用户:

从5.0开始,ElasticSearch 安全级别提高了,不允许采用root帐号启动,所以我们要添加一个用户。

(1)创建用户组:

groupadd elk

(2)创建用户:

useradd admin
passwd admin

(3)将admin用户添加到elk组:

usermod -G elk admin

(4)设置sudo权限:

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

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

(5)为用户分配权限:

chown -R admin:elk /usr/local/elasticsearch
  • 切换用户:
su admin
示例
  • ElasticSearch的启动和停止:
    (1)启动:
./elasticsearch -d
示例

(2)验证是否启动成功:

#Linux服务器中测试
curl http://192.168.70.140:9200
#在浏览器中访问
http://192.168.70.140:9200/
浏览器的信息

Linux中的信息

二、安装Head插件

1.Head插件简介:

ElasticSearch-head是一个H5编写的ElasticSearch集群操作和管理工具,可以对集群进行傻瓜式操作。
它提供了:
(1)索引和节点级别操作。
(2)搜索接口能够查询集群中原始json或表格格式的检索数据。
(3)能够快速访问并显示集群的状态。

2.安装步骤:

  • 安装NodeJS:

要求在root下执行下面的命令。

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插件源码:

进入/usr/local/下创建目录“mkdir es”。

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

下载
  • 使用国内镜像安装:
#进入目录
cd elasticsearch-head-master
#下载
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo cnpm install
示例

示例

3.配置:

  • 配置ElasticSearch,使得HTTP对外提供服务:

编辑文件:“vi config/elasticsearch.yml”;
添加下面的内容:

# 增加新的参数,这样head插件可以访问es。设置参数的时候:后面要有空格
        http.cors.enabled: true
        http.cors.allow-origin: "*"
示例

编辑
  • 修改Head插件配置文件:

"vi Gruntfile.js";找到connect:server,添加hostname一项,如下:

        connect: {
            server: {
                    options: {
                            hostname: '0.0.0.0',
                            port: 9100,
                            base: '.',
                            keepalive: true
                    }
            }
    }
进入文件编辑
示例
  • 启动:
    (1)重启:
./elasticsearch -d

(2)启动head:

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

浏览器访问
  • 简单应用:
    (1)创建索引:
curl -XPUT http://192.168.70.140:9200/applog

(2)查看head变化:

http://192.168.226.132:9100/
创建索引

示例

三、安装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)复制:


示例

2.测试:

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

3.修改配置:

在 logstash的主目录下,“vim config/log_to_es.conf”;添加内容如下:

# 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.  
      }  
    }  
  • 启动:
./bin/logstash -f config/log_to_es.conf 
或 后台运行守护进程 ./bin/logstash -f config/log_to_es.conf &
  • 测试:

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)复制:


示例

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"
示例
  • 启动:
./bin/kibana 
  • 测试:

http://192.168.226.132:5601/app/kibana

示例

3.Kibana操作界面:

  • Discover:

Discover 主要是做索引查询,功能非常强大 。

  • Visualize :

视图展示,支持许多风格。

  • Dashboard:

图表展示,一个仪表板显示 Kibana 保存的一系列可视化。

  • Timelion:

Timelion 是一个时间序列数据的可视化功能,可以结合在一个单一的可视化完全独立的 数据源。它是由一个简单的表达式语言驱动的,你用来检索时间序列数据,进行计算,找出 复杂的问题的答案,并可视化的结果。这个功能由一系列的功能函数组成,同样的查询的结 果,也可以通过 Dashboard 显示查看。

  • DevTools:

可以直接操作 es 中的数据,使用户方便的通过浏览器直接与 Elasticsearch 进行交互。

  • Management:

管理中的应用是在你执行你的运行时配置 kibana,包括初始设置和指标进行配置模式, 高级设置,调整自己的行为和 Kibana,各种“对象”,你可以查看保存在整个 Kibana 的内容 如发现页,可视化和仪表板。

五、Spring Cloud和ELK的集成

1.创建Sleuth-Product-Service-elk:

复制项目。

示例

2.创建Sleuth-Product-Provider-elk:

复制项目进行修改。

示例
  • 修改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 坐标 -->
        <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

#设置服务注册中心
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"?>
<!--该日志将日志级别不同的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}" />

    <!-- 控制台的日志输出样式 -->
    <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}}" />

    <!-- 控制台输出 -->
    <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>

    <!-- 日志输出级别 -->
    <root level="DEBUG">
        <appender-ref ref="console" />
        <appender-ref ref="logstash" />
    </root>
</configuration>

创建Consumer:

复制项目,添加Logback.xml文件。

示例
  • 测试:


    示例
示例

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

推荐阅读更多精彩内容