Springboot集成graylog快速入门demo

一、graylog介绍

GrayLog是一个轻量型的分布式日志管理平台,一个开源的日志聚合、分析、审计、展示和预警工具。在功能上来说,和ELK类似,但又比ELK要简单轻量许多。依靠着更加简洁,高效,部署使用简单的优势很快受到许多公司的青睐。GrayLog包含Elasticsearch、MongoDb 和Graylog三个模块

二、graylog环境搭建

这里采用docker-compose来搭建graylog测试环境,具体配置如下

 
# docker-compose配置参考:https://docs.graylog.org/en/3.3/pages/installation/docker.html
version: '3'services:mongo:image: mongo:3container_name: graylog_demo_mongorestart: unless-stopped#    volumes:#      - ./graylog/mongo_data:/data/dbnetworks:- graylogelasticsearch:image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.10container_name: graylog_demo_elasticsearchrestart: unless-stopped#    volumes:#      - ./graylog/es_data:/usr/share/elasticsearch/dataenvironment:- http.host=0.0.0.0- transport.host=localhost- network.host=0.0.0.0- "ES_JAVA_OPTS=-Xms512m -Xmx512m"ulimits:memlock:soft: -1hard: -1deploy:resources:limits:memory: 1gnetworks:- grayloggraylog:image: graylog/graylog:3.3container_name: graylog_demo_graylogrestart: unless-stopped#    volumes:#      - ./graylog/graylog_data_journal:/usr/share/graylog/data/journalenvironment:- GRAYLOG_PASSWORD_SECRET=somepasswordpepper # CHANGE ME (must be at least 16 characters)!- GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 # Password: admin- GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/ # TODO 这里填写自己的ip地址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/udpnetworks:- graylogdepends_on:- mongo- elasticsearch# 网桥graylog -> 方便相互通讯
networks:graylog:driver: bridge

启动服务

 
docker-compose -f docker-compose-graylog.yml -p graylog_demo up -d

访问地址:http://ip地址:9001 默认登录账号密码:admin/admin

72fe3a5760fb53aee41f251e3aaf162b.png

2.1、新建input http input

3e2f858bf8f09368dcb6b19684d93190.png

测试是否正常发送

 
curl -XPOST http://localhost:12201/gelf -p0 -d '{"message":"hello这是一条消息", "host":"127.0.0.1", "facility":"test", "topic": "meme"}'

10c9f1d0708d4ef30a034d6bb12088de.png

2.2、创建 gref udp input

由于http发送效率比较低,所以后面springboot会采用UDP方法来采集日志

5750ca5b5f10cc5c0f94dcf067281c57.png

三、代码工程

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>grayLog</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>biz.paluch.logging</groupId><artifactId>logstash-gelf</artifactId><version>1.11.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
</project>

HelloWorldController.java

 
package com.et.graylog.controller;import com.et.graylog.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;
import java.util.Map;
@Slf4j
@Controller
public class HelloWorldController {@AutowiredDemoService demoService;@RequestMapping("/hello")@ResponseBodypublic Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");log.info("my name is HelloWorldController");demoService.hello();return map;}@GetMapping("log")public String log(){log.error("error");log.warn("warn");log.info("info");log.debug("debug");int i = 1/0;return "success";}
}

DemoApplication.java

 
package com.et.graylog;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

application.yaml

 
server:port: 8088
groylog:ip: 127.0.0.1port: 12201maximumMessageSize: 8192

logback.xml

 
<?xml version="1.0" encoding="UTF-8"?>
<configuration><appender name="GELF" class="biz.paluch.logging.gelf.logback.GelfLogbackAppender"><host>udp:127.0.0.1</host><!--graylog 服务器ip--><port>12201</port><!--graylog udp端口--><version>1.1</version><facility>springboot-with-graylog</facility><extractStackTrace>true</extractStackTrace><filterStackTrace>true</filterStackTrace><mdcProfiling>true</mdcProfiling><timestampPattern>yyyy-MM-dd HH:mm:ss,SSSS</timestampPattern><maximumMessageSize>8192</maximumMessageSize><!-- This are fields using MDC --><mdcFields>mdcField1,mdcField2</mdcFields><dynamicMdcFields>mdc.*,(mdc|MDC)fields</dynamicMdcFields><includeFullMdc>true</includeFullMdc></appender><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern></encoder></appender><root level="INFO"><appender-ref ref="GELF"/><appender-ref ref="CONSOLE"/></root></configuration>

代码参考

  • https://github.com/Harries/springboot-demo

四、测试

启动服务

 
"C:\Program Files\Java\jdk1.8.0_251\bin\java.exe" -Dvisualvm.id=505582826333400 "-javaagent:D:\huiyida\develop\IntelliJ IDEA Community Edition 2022.1.3\lib\idea_rt.jar=57262:D:\huiyida\develop\IntelliJ IDEA Community Edition 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_251\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_251\jre\lib\rt.jar;D:\IdeaProjects\ETFramework\grayLog\target\classes;D:\jar_repository\org\springframework\boot\spring-boot-starter-web\2.2.5.RELEASE\spring-boot-starter-web-2.2.5.RELEASE.jar;D:\jar_repository\org\springframework\boot\spring-boot-starter\2.2.5.RELEASE\spring-boot-starter-2.2.5.RELEASE.jar;D:\jar_repository\org\springframework\boot\spring-boot-starter-logging\2.2.5.RELEASE\spring-boot-starter-logging-2.2.5.RELEASE.jar;D:\jar_repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\jar_repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\jar_repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\jar_repository\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;D:\jar_repository\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;D:\jar_repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\jar_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\jar_repository\org\springframework\spring-core\5.2.4.RELEASE\spring-core-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-jcl\5.2.4.RELEASE\spring-jcl-5.2.4.RELEASE.jar;D:\jar_repository\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;D:\jar_repository\org\springframework\boot\spring-boot-starter-json\2.2.5.RELEASE\spring-boot-starter-json-2.2.5.RELEASE.jar;D:\jar_repository\com\fasterxml\jackson\core\jackson-databind\2.10.2\jackson-databind-2.10.2.jar;D:\jar_repository\com\fasterxml\jackson\core\jackson-annotations\2.10.2\jackson-annotations-2.10.2.jar;D:\jar_repository\com\fasterxml\jackson\core\jackson-core\2.10.2\jackson-core-2.10.2.jar;D:\jar_repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.2\jackson-datatype-jdk8-2.10.2.jar;D:\jar_repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.2\jackson-datatype-jsr310-2.10.2.jar;D:\jar_repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.2\jackson-module-parameter-names-2.10.2.jar;D:\jar_repository\org\springframework\boot\spring-boot-starter-tomcat\2.2.5.RELEASE\spring-boot-starter-tomcat-2.2.5.RELEASE.jar;D:\jar_repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.31\tomcat-embed-core-9.0.31.jar;D:\jar_repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.31\tomcat-embed-el-9.0.31.jar;D:\jar_repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.31\tomcat-embed-websocket-9.0.31.jar;D:\jar_repository\org\springframework\boot\spring-boot-starter-validation\2.2.5.RELEASE\spring-boot-starter-validation-2.2.5.RELEASE.jar;D:\jar_repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;D:\jar_repository\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;D:\jar_repository\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;D:\jar_repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\jar_repository\org\springframework\spring-web\5.2.4.RELEASE\spring-web-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-beans\5.2.4.RELEASE\spring-beans-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-webmvc\5.2.4.RELEASE\spring-webmvc-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-aop\5.2.4.RELEASE\spring-aop-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-context\5.2.4.RELEASE\spring-context-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\spring-expression\5.2.4.RELEASE\spring-expression-5.2.4.RELEASE.jar;D:\jar_repository\org\springframework\boot\spring-boot-autoconfigure\2.2.5.RELEASE\spring-boot-autoconfigure-2.2.5.RELEASE.jar;D:\jar_repository\org\springframework\boot\spring-boot\2.2.5.RELEASE\spring-boot-2.2.5.RELEASE.jar;D:\jar_repository\biz\paluch\logging\logstash-gelf\1.11.1\logstash-gelf-1.11.1.jar;D:\jar_repository\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar" com.et.graylog.DemoApplication. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.2.5.RELEASE)10:07:22.906 INFO com.et.graylog.DemoApplication - Starting DemoApplication on BLHHUAPC with PID 30200 (D:\IdeaProjects\ETFramework\grayLog\target\classes started by Dell in D:\IdeaProjects\ETFramework)
10:07:22.918 INFO com.et.graylog.DemoApplication - No active profile set, falling back to default profiles: default
10:07:23.801 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8088 (http)
10:07:23.811 INFO o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8088"]
10:07:23.813 INFO o.a.catalina.core.StandardService - Starting service [Tomcat]
10:07:23.813 INFO o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.31]
10:07:23.885 INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
10:07:23.887 INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 930 ms
10:07:24.025 INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
10:07:24.124 INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8088"]
10:07:24.143 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8088 (http) with context path ''
10:07:24.147 INFO com.et.graylog.DemoApplication - Started DemoApplication in 1.564 seconds (JVM running for 2.09)

登录graylog,查看日志是否已经发送过去

bbcf80c98d674d61e6eb925bb722c360.png

五、参考资料

  • http://blog.healerjean.com/docker/log/2018/03/16/2_Docker%E5%AE%89%E8%A3%85graylog%E5%92%8C%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/

  • https://archivedocs.graylog.org/en/3.3/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/448509.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

10个React状态管理库推荐

本文将为您推荐十款实用的React状态管理库&#xff0c;帮助您打造出高效、可维护的前端应用。让我们一起看看这些库的魅力所在&#xff01; 在前端开发中&#xff0c;状态管理是至关重要的一环。React作为一款流行的前端框架&#xff0c;其强大的状态管理功能备受开发者青睐。…

Spring Boot导出EXCEL 文件

主要功能:实现java导出excel到本地 JDK版本&#xff1a;openJDK 20.0.1 依赖pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchem…

拓展欧几里得法求逆元

板子&#xff1a; x即为最终答案&#xff0c;x可能为负数&#xff0c;加模数即可 乘法逆元 - OI Wiki (oi-wiki.org) void exgcd(int a, int b, int& x, int& y) {if (b 0) {x 1, y 0;return;}exgcd(b, a % b, y, x);y - a / b * x; } 使用: exgcd(a, n 1, x,…

【全网最全】2024美赛ABCDEF题思路模型全解(后续会更新)

欲获取更多资料&#xff0c;一定要点击这里并关注文末的公众号&#xff01;&#xff01;&#xff01; 最新更新&#xff1a;我们团队不仅在第一时间更新了24美赛全题目的深度翻译和深入分析&#xff0c;经过爆肝奋战&#xff0c;我们在第一时间给出了ABCDEF全题目的完整建模过程…

JavaScript基础第二课

目录 一.JavaScript变量 1.理解变量是计算机存储数据的“容器” 2.变量的基本使用 3. 变量的本质 4. 变量命名规则 二. 变量拓展-数组 1. 数组的基本使用 2. 取值语法 三.JavaScript常量 四.JavaScript数据类型 ①基本数据类型 ②引用数据类型 ③NaN ④通过 type…

函数重载你真的了解吗?

1.什么叫函数重载&#xff1f; 函数重载&#xff08;Function Overloading&#xff09;是指在同一个作用域内&#xff0c;允许定义多个具有相同名称但参数列表不同的函数。具体而言&#xff0c;函数重载允许你定义同名的函数&#xff0c;但这些函数应该有不同的参数类型、参数个…

【网络安全】2024年暗网威胁分析及发展预测

暗网因其非法活动而臭名昭著&#xff0c;现已发展成为一个用于各种非法目的的地下网络市场。 它是网络犯罪分子的中心&#xff0c;为被盗数据交易、黑客服务和邪恶活动合作提供了机会。为了帮助企业组织更好地了解暗网发展形势&#xff0c;近日&#xff0c;卡巴斯基的安全研究…

QT 使用 QWebChannel 与 Web 端通信展示文件信息

前言 本文将展示如何使用 QWebChannel 来实现 Web 端与 QT 端之间的交互&#xff0c;同时会通过一个在浏览器端展示文件夹信息的简单例子来展示其具体使用&#xff0c;其功能如下&#xff1a; 获取指定文件夹下的文件信息。通过使用 QT 的 QFileSystemWatcher 对指定文件夹进…

应该怎样选择最适合的微信管理系统?

许多企业老板面对各种各样的微信管理系统&#xff0c;都不知从何下手&#xff0c;感到非常迷茫。关于如何选择微信管理系统&#xff0c;是需要根据你的需求来决定选择合适的。比方说能把微信号登录到一起就可以&#xff0c;或者说能自动加人&#xff0c;或者定时发圈&#xff0…

计算机设计大赛 深度学习 YOLO 实现车牌识别算法

文章目录 0 前言1 课题介绍2 算法简介2.1网络架构 3 数据准备4 模型训练5 实现效果5.1 图片识别效果5.2视频识别效果 6 部分关键代码7 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 基于yolov5的深度学习车牌识别系统实现 该项目较…

element-ui button 组件源码分享

element-ui button 源码分享&#xff0c;基于对源码的理解&#xff0c;编写一个简单的 demo&#xff0c;主要分三个模块来分享&#xff1a; 一、button 组件的方法。 1.1 在方法这块&#xff0c;button 组件内部通过暴露 click 方法实现&#xff0c;具体如下&#xff1a; 二、…

用户体验优化:HubSpot的秘密武器

在当今数字化市场中&#xff0c;提升用户体验已经成为企业成功的关键因素之一。HubSpot&#xff0c;作为一款领先的营销自动化工具&#xff0c;不仅在推动销售业绩上表现出色&#xff0c;同时通过其独特的策略也致力于提升用户体验。运营坛将深入探讨HubSpot是如何通过个性化推…