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文件:
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Dalston.SR5
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.0
        
        
            mysql
            mysql-connector-java
        
        
        
            com.zlw
            springcloud-sleuth-product-service-elk
            0.0.1-SNAPSHOT
        
        
            org.springframework.cloud
            spring-cloud-starter-sleuth
        
        
            net.logstash.logback
            logstash-logback-encoder
            5.0
        
  • 修改配置文件:
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文件:



    

    

    
    

    
    

    
    
        
            INFO
        
        
        
            ${CONSOLE_LOG_PATTERN}
            utf8
        
    

    
    
        192.168.226.132:9250
        
        
            
                
                    UTC
                
                
                    
                        {
                        "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"
                        }
                    
                
            
        
    

    
    
        
        
    

创建Consumer:

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

示例
  • 测试:


    示例
示例

示例

你可能感兴趣的:(Spring Cloud学习day108:ELK)