Spring Boot 两步集成 日志收集ELK与分布式系统监控CAT

时间:2020-01-09
本文章向大家介绍Spring Boot 两步集成 日志收集ELK与分布式系统监控CAT,主要包括Spring Boot 两步集成 日志收集ELK与分布式系统监控CAT使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

日志收集ELK与分布式系统监控CAT


Spring Boot项目集成方法

一. pom.xml引入starter依赖
<dependency>
    <groupId>com.louis</groupId>
    <artifactId>ylog-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
二. bootstrap.yml配置文件开启日志
ylog:
  enable-y-log: ${YLog_Enable:true}
  app-name: ${spring.application.name}
  env: ${spring.profiles.active}
  cat:
    enable-cat: ${YLog_Enable_Cat:true}
    http-port: ${YLog_Cat_HttpPort:8081}
    port: ${YLog_Cat_Port:2280}
    cat-servers: ${YLog_Cat_Servers:192.168.102.212}
    enable-spring-bean-aop: true
  elk:
    enable-elk: ${YLog_Enable_Elk:true}
    logstash-host: ${YLog_Enable_elk:192.168.102.130:30309}

附录:服务安装


一. ELK安装

1. ElasticSearch安装
  • 下载所需版本压缩包并解压

  • 修改/config/elasticsearch.yml

    network.host=127.0.0.1
    network.port=9200
  • 启动/bin/elasticsearch.bat

  • 浏览器访问http://localhost:9200/,正常返回ES服务器json格式信息,即为安装成功

2. Logstash安装
  • 下载所需版本压缩包并解压

  • 修改/config/logstash.yml

    xpack.monitoring.elasticsearch.hosts: ["http://localhost:9200"]
  • 新建/config/log4j_to_es.conf文件,文件内容如下:

    input {
        tcp {
            port => 4560
            codec => json_lines
        }
    } 
    output {
            elasticsearch {
                    hosts => "127.0.0.1:9200"
                    index => "applog"
            }
            stdout { codec => rubydebug}
    }
    • 新建/bin/run_default.bat文件,文件内容如下:

      D:\ELK\logstash-7.4.0\bin\logstash.bat -f D:\ELK\logstash-7.4.0\bin\log4j_to_es.conf
      pause
    • 启动/bin/run_default.bat

    • 查看控制台输出,没有ERROR。浏览器访问http://localhost:9600/, 正常返回服务器json格式信息,即为安装成功

3. Kibana安装
  • 下载所需版本压缩包并解压

  • 修改/config/kibana.yml

    elasticsearch.hosts: ["http://localhost:9200"]
    elasticsearch.requestTimeout: 90000
  • 运行/bin/kibana.bat

  • 访问http://localhost:5601 正常展示kibana页面,即为安装成功

官方文档: gitbook

二. CAT安装

  • 下载官方开源源码: https://github.com/dianping/cat.git

  • 切换到2.0.0版本(一部分cat插件在3.0.0的版本中移除了,需要先在2.0.0的版本上安装到本地maven库):

    git checkout v2.0.0
  • 执行mvn命令安装插件

    mvn clean install -Dmaven.test.skip=true
  • 切换到3.0.0版本

    git checkout v3.0.0
  • 新建mysql数据库cat, 执行3.0.0分支下的/script/CatApplication.sql脚本完成初始化

  • 新增配置文件client.xml到tomcat安装盘,如文件目录:C:\data\appdatas\cat\client.xml,配置内容:

    <?xml version="1.0" encoding="utf-8"?>
    <config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
      <servers>
          <!-- Local mode for development -->
          <server ip="10.0.75.1" port="2280" http-port="8080" />
          <!-- If under production environment, put actual server address as list. -->
          <!-- <server ip="192.168.7.71" port="2280" /> -->
      </servers>
    </config>

    本地调试时ip为本机内网地址,写127.0.0.1可能导致服务启动失败

    • 新增配置文件server.xml到tomcat安装盘,如文件目录C:\data\appdatas\cat\server.xml,配置内容:

      <?xml version="1.0" encoding="utf-8"?>
      
      <!-- Configuration for development environment-->
      <config local-mode="false" hdfs-machine="false" job-machine="true" alert-machine="false">
      
          <storage  local-base-dir="/data/appdatas/cat/bucket/" max-hdfs-storage-time="15" local-report-storage-time="7" local-logivew-storage-time="7">
      
          </storage>
      
          <console default-domain="Cat" show-cat-domain="true">
              <remote-servers>10.0.75.1:8080</remote-servers>     
          </console>
      
      </config>

      本地调试时ip为本机内网地址,写127.0.0.1可能导致服务启动失败

    • 新增配置文件datasources.xml到tomcat安装盘,如文件目录C:\data\appdatas\cat\datasources.xml,配置内容:

      <?xml version="1.0" encoding="utf-8"?>
      
      <data-sources>
          <data-source id="cat">
              <maximum-pool-size>3</maximum-pool-size>
              <connection-timeout>1s</connection-timeout>
              <idle-timeout>10m</idle-timeout>
              <statement-cache-size>1000</statement-cache-size>
              <properties>
                  <driver>com.mysql.jdbc.Driver</driver>
                  <url><![CDATA[jdbc:mysql://127.0.0.1:3306/cat]]></url>
                  <user>root</user>
                  <password>123456</password>
                  <connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
              </properties>
          </data-source>
          <data-source id="app">
              <maximum-pool-size>3</maximum-pool-size>
              <connection-timeout>1s</connection-timeout>
              <idle-timeout>10m</idle-timeout>
              <statement-cache-size>1000</statement-cache-size>
              <properties>
                  <driver>com.mysql.jdbc.Driver</driver>
                  <url><![CDATA[jdbc:mysql://127.0.0.1:3306/cat]]></url>
                  <user>root</user>
                  <password>123456</password>
                  <connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
              </properties>
          </data-source>
      </data-sources>
  • 执行mvn命令打包源码

    mvn clean install -Dmaven.test.skip=true
  • 拷贝/cat-home/target/cat-home-3.0.0.jar到tomcat的安装目录下,并重命名为car.jar: /webapps/cat.jar

  • 启动tomcat

  • 访问http://localhost:8080/cat, 正常展示cat主页,即为安装成功

ylog-spring-boot-starter源码: github

原文地址:https://www.cnblogs.com/LouisGuo/p/12170608.html