ELK模块封装starter

文章目录

    • 1.combinations-elk-starter
        • 1.目录结构
        • [2.log4j2-spring.xml 从环境变量读取host和port](#2.log4j2-spring.xml 从环境变量读取host和port)
        • [3.ELKProperties.java 两个属性](#3.ELKProperties.java 两个属性)
        • [4.ELKAutoConfiguration.java 启用配置类](#4.ELKAutoConfiguration.java 启用配置类)
        • [5.ELKEnvironmentPreparedListener.java 监听器从application.yml中获取属性值](#5.ELKEnvironmentPreparedListener.java 监听器从application.yml中获取属性值)
        • [6.spring.factories 注册监听器](#6.spring.factories 注册监听器)
    • 2.combinations-elk-starter-demo

1.combinations-elk-starter

1.目录结构
2.log4j2-spring.xml 从环境变量读取host和port
xml 复制代码
    <!-- logstash 的 SocketAppender 配置 -->
    <Property name="socket.host">${sys:socket.host}</Property>
    <Property name="socket.port">${sys:socket.port}</Property>
3.ELKProperties.java 两个属性
java 复制代码
package com.sunxiansheng.elk.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Description: ELK 属性
 *
 * @Author sun
 * @Create 2025/1/10 22:32
 * @Version 1.0
 */
@ConfigurationProperties(prefix = "sun-rays.elk.logstash")
@Data
public class ELKProperties {

    /**
     * LogStash的主机
     */
    private String host;

    /**
     * LogStash的端口
     */
    private Integer port;
}
4.ELKAutoConfiguration.java 启用配置类
java 复制代码
package com.sunxiansheng.elk.config;

import com.sunxiansheng.elk.config.properties.ELKProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * Description: ELK自动配置类
 *
 * @Author sun
 * @Create 2024/12/31 13:14
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties({ELKProperties.class}) // 启用配置类
@Slf4j
public class ELKAutoConfiguration {

    /**
     * 自动配置成功日志
     */
    @PostConstruct
    public void logConfigSuccess() {
        log.info("ELKAutoConfiguration has been loaded successfully!");
    }
}
5.ELKEnvironmentPreparedListener.java 监听器从application.yml中获取属性值
java 复制代码
package com.sunxiansheng.elk.listener;

import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * Description: 配置文件已加载,Environment 对象已初始化
 * 使用 ApplicationEnvironmentPreparedEvent 读取 application.yml 文件中的 sun-rays.elk.logstash.host 和 sun-rays.elk.logstash.port 配置
 *
 * @Author sun
 * @Create 2025/1/10 22:46
 * @Version 1.0
 */
public class ELKEnvironmentPreparedListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();

        // 获取 socket.host 属性并提供默认值
        String socketHost = environment.getProperty("sun-rays.elk.logstash.host", "127.0.0.1");
        if ("127.0.0.1".equals(socketHost)) {
            System.err.println("WARNING: sun-rays.elk.logstash.host 属性未设置,使用默认值: " + socketHost);
        }

        // 获取 socket.port 属性并提供默认值
        String socketPort = environment.getProperty("sun-rays.elk.logstash.port", "8080");
        if ("8080".equals(socketPort)) {
            System.err.println("WARNING: sun-rays.elk.logstash.port 属性未设置,使用默认值: " + socketPort);
        }

        // 将属性设置为系统属性
        System.setProperty("socket.host", socketHost);
        System.setProperty("socket.port", socketPort);
    }

    /**
     * 当前监听器的启动顺序需要在日志配置监听器的前面,保证在日志文件初始化之前读取 application.yml 的配置。
     *
     * @return
     */
    @Override
    public int getOrder() {
        return LoggingApplicationListener.DEFAULT_ORDER - 1;
    }
}
6.spring.factories 注册监听器
java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunxiansheng.elk.config.ELKAutoConfiguration
org.springframework.context.ApplicationListener=\
com.sunxiansheng.elk.listener.ELKEnvironmentPreparedListener

2.combinations-elk-starter-demo

1.目录结构
2.application.yml 配置elk属性
yaml 复制代码
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-combinations-demo/combinations-elk-starter-demo/logs # 日志根目录(默认./logs)
    module: sunrays-combinations-demo/combinations-elk-starter-demo # 模块根目录从仓库根目录开始(默认defaultModule)
  env:
    path: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-combinations-demo/combinations-elk-starter-demo # .env文件的绝对路径
  elk:
    logstash:
      host: ${ELK_LOGSTASH_HOST} # LogStash的主机
      port: ${ELK_LOGSTASH_PORT} # LogStash的端口
3...env文件配置属性值
java 复制代码
ELK_LOGSTASH_HOST=host
ELK_LOGSTASH_PORT=port
相关推荐
未若君雅裁4 天前
日志采集与ELK:从本地日志到集中检索分析
运维·elk·jenkins
_codemonster4 天前
Prometheus + Grafana + Alertmanager和ELK 栈(Elasticsearch + Logstash + Kibana)
elk·grafana·prometheus
Sean‘5 天前
在隔离内网机器上使用 Filebeat 全量采集日志并推送到 ELK 的实战
运维·服务器·elk
Michaelwubo8 天前
ELK案例
elk
YDS8299 天前
DeepSeek RAG&MCP + Agent智能体项目 —— 集成ELK日志管理系统和Prometheus监控系统
java·elk·ai·springboot·agent·prometheus·deepseek
爱吃龙利鱼11 天前
docker-compose一键部署ELK+Filbeat
elk·docker
小旭952713 天前
Spring Cloud 集成分布式日志 ELK+Swagger 接口文档实战
java·分布式·后端·elk·spring cloud
绝知此事17 天前
ELK 从入门到精通:Spring Boot 实战三部曲(三)—— 高级应用与架构设计
spring boot·后端·elk
绝知此事18 天前
ELK 从入门到精通:Spring Boot 实战三部曲(二)—— 进阶特性与性能优化
spring boot·elk·性能优化
绝知此事18 天前
ELK 从入门到精通:Spring Boot 实战三部曲(一)—— 基础核心与快速上手
spring boot·后端·elk