ELK整合springboot(第二课)

一、创建一个springboot的项目

ELK整合springboot(第二课)_第1张图片
pom文件如下:


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.7.13version>
		<relativePath/> 
	parent>
	<groupId>com.tulinggroupId>
	<artifactId>elk-demoartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>elk-demoname>
	<description>elk-demodescription>
	<properties>
		<java.version>1.8java.version>
	properties>
	<dependencies>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>net.logstash.logbackgroupId>
			<artifactId>logstash-logback-encoderartifactId>
			<version>5.3version>
		dependency>
		<dependency>
			<groupId>org.projectlombokgroupId>
			<artifactId>lombokartifactId>
			<optional>trueoptional>
		dependency>

	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

创建一个最简单的控制层,输出日志
ELK整合springboot(第二课)_第2张图片
修改application.properties默认配置,给这个项目起个名字
ELK整合springboot(第二课)_第3张图片
创建logback-spring.xml文件,修改destination这个改成自己的服务器地址,端口是logstash同步es的端口,可以看第一节课


<configuration debug="false">
    
    <springProperty scope="context" name="springApplicationName" source="spring.application.name" />
    <property name="LOG_HOME" value="logs/demo.log" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>

    <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFOlevel>
        filter>
        <destination>192.168.85.200:4560destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
        <providers>
            <timestamp>
                <timeZone>Asia/ShanghaitimeZone>
            timestamp>
            
            <pattern>
                <pattern>
                    {
                    "project": "elk",
                    "level": "%level",
                    "service": "${springApplicationName:-}",
                    "pid": "${PID:-}",
                    "thread": "%thread",
                    "class": "%logger",
                    "message": "%message",
                    "stack_trace": "%exception"
                    }
                pattern>
            pattern>
        providers>
        encoder>
    appender>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="logstash" />
    root>
configuration>

二、创建索引模式

ELK整合springboot(第二课)_第4张图片
匹配上之后点击下一步
ELK整合springboot(第二课)_第5张图片
ELK整合springboot(第二课)_第6张图片

3、测试

如图,就有了日志
ELK整合springboot(第二课)_第7张图片
测试我们写的接口是否有日志,代码在最上面
ELK整合springboot(第二课)_第8张图片
ELK整合springboot(第二课)_第9张图片
以上,就完成了最简单的elk日志搭建,但是还会有更复杂的情况,比如日志如何删除?
多个springboot如何搭建?
如果springboot部署在容器上配置是否一致?

结语:技术的道路艰难又不艰难,贵在坚持!

你可能感兴趣的:(elasticSearch,spring,boot,elk,java)