最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

本文介紹SpringBoot如何使用Graylog日志收集。

創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括普寧網(wǎng)站建設(shè)、普寧網(wǎng)站制作、普寧網(wǎng)頁制作以及普寧網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,普寧網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到普寧省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

1.Graylog介紹

Graylog是一個(gè)生產(chǎn)級別的日志收集系統(tǒng),集成Mongo和Elasticsearch進(jìn)行日志收集。其中Mongo用于存儲Graylog的元數(shù)據(jù)信息和配置信息,ElasticSearch用于存儲數(shù)據(jù)。

架構(gòu)圖如下:

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

生產(chǎn)環(huán)境配置圖如下:

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

2.安裝Graylog

在官方文檔上推薦了很多種安裝的方式,這里以docker-compose的方式為例,進(jìn)行安裝Graylog,mongo,elasticsearch。

docker-compose.yml內(nèi)容如下(這里是在官網(wǎng)的基礎(chǔ)上改了一下):

version: '2'
services:
 # MongoDB: https://hub.docker.com/_/mongo/
 mongodb:
  image: mongo:3
 # Elasticsearch: https://www.elastic.co/guide/en/elasticsearch/reference/6.6/docker.html
 elasticsearch:
  image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1
  environment:
   - http.host=0.0.0.0
   - transport.host=localhost
   - network.host=0.0.0.0
   - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
  ulimits:
   memlock:
    soft: -1
    hard: -1
  mem_limit: 512m
 # Graylog: https://hub.docker.com/r/graylog/graylog/
 graylog:
  image: graylog/graylog:3.0
  environment:
   # CHANGE ME (must be at least 16 characters)!
   - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
   # Password: admin
   - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
   - GRAYLOG_HTTP_EXTERNAL_URI=http://106.13.35.42:9000/
  links:
   - mongodb:mongo
   - elasticsearch
  depends_on:
   - mongodb
   - elasticsearch
  ports:
   # Graylog web interface and REST API
   - 9000:9000
   # Syslog TCP
   - 1514:1514
   # Syslog UDP
   - 1514:1514/udp
   # GELF TCP
   - 12201:12201
   # GELF UDP
   - 12201:12201/udp

其中106.13.35.42是我的外網(wǎng)ip,本地服務(wù)使用127.0.0.1即可。

其他方式可以查看官方文檔,https://docs.graylog.org/en/3.0/pages/installation.html

3.配置Graylog

在瀏覽器訪問http://ip:9000,

這里默認(rèn)用戶名密碼都是admin,進(jìn)入后如圖所示。

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

選擇System按鈕中的input,錄入一個(gè)輸入源,如圖

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

這里以GELF UDP為例,在圖中位置選擇GELF UDP,選擇完成后點(diǎn)擊Launch new input,如圖

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

在Node處選擇自己安裝的,剩下的就根據(jù)需要填寫即可,如圖

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

保存完成后如圖,到這里就已經(jīng)配置完成了。

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

4.SpringBoot日志輸出到Graylog

這里分別舉例Logback日志和Log4j2日志。

4.1 Logback日志

這里使用的logback-gelf向Graylog輸出日志,在github上有對logback-gelf的詳細(xì)使用介紹,這里只是簡單舉例。Github地址:https://github.com/osiegmar/logback-gelf。

新建項(xiàng)目,加入logback-gelf依賴,pom文件如下:



  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE
     
  
  com.dalaoyang
  springboot2_graylog
  0.0.1-SNAPSHOT
  springboot2_graylog
  springboot2_graylog

  
    1.8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    

    
      de.siegmar
      logback-gelf
      2.0.0
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  


加入logback日志配置,新建logback-spring.xml,內(nèi)容如下:



  
  
  
  

  
    
      ${CONSOLE_LOG_PATTERN}
      UTF-8
    
  

  
    106.13.35.42
    12201
  

  
  
    
    
  



啟動項(xiàng)目,當(dāng)前項(xiàng)目端口是8081,查看Graylog控制臺如圖:

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

4.2 Log4j2日志

log4j2日志使用的是log4j2-gelf依賴,github上面也有對應(yīng)的介紹,pom文件如下:



  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE
     
  
  com.dalaoyang
  springboot2_graylog_log4j
  0.0.1-SNAPSHOT
  springboot2_graylog_log4j
  springboot2_graylog_log4j

  
    1.8
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    
    
      org.springframework.boot
      spring-boot-starter
      
        
          spring-boot-starter-logging
          org.springframework.boot
        
      
    
    
      org.springframework.boot
      spring-boot-starter-log4j2
    

    
      org.graylog2.log4j2
      log4j2-gelf
      1.3.1
    

  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

創(chuàng)建log4j2-spring.xml進(jìn)行配置輸出日志信息,如下:



  
    %d{yyyy-MM-dd HH:mm:ss:SSS} - %-5level - %pid - %t - %c{1.}:%L - %m%n
  
  
    
      
      
    
    
      
      
        
        
      
      
      
      
    
  
  
    
      
      
    
  

這個(gè)項(xiàng)目使用的端口號是8888,可以在日志中清晰的看到。

SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例

5. ELK vs Graylog

這里僅以日志收集為例,簡單說一下二者之間的選擇,我個(gè)人的建議就是取決于現(xiàn)有技術(shù)棧,比如現(xiàn)在就有現(xiàn)成的Mongodb,那么選擇Graylog可以節(jié)省不少成本,ELK類似,不要盲目的追求技術(shù)而選擇。

6. 源碼

springboot2_graylog源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog

springboot2_graylog_log4j源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_graylog_log4j

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


文章題目:SpringBoot使用Graylog日志收集的實(shí)現(xiàn)示例
文章起源:http://fisionsoft.com.cn/article/ijcsdd.html