SpringBoot2.3集成ELK7.1.0

最近想用ELK做日志分析,所以先写了Demo来实验一下!

1、安装ELK(Elasticsearch+Logstash+Kibana),具体安装教程百度

2、查看是否安装成功,输入localhost:9200,localhost:5601,如下页面则安装成功
在这里插入图片描述
在这里插入图片描述
3、pom包依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.modules</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elk</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--集成logstash-->
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

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

</project>

4、配置文件

server:
  port: 8087
spring:
  application:
    name: search-service
  elasticsearch:
    rest:
      uris: http://localhost:9200


5、在logstash的bin目录下创建logstash.conf配置文件,启动logstash时要依赖这个配置文件
logstash.conf

input {
  tcp {
    mode => "server"
    port => 4560
    codec => json_lines
  }
}
output {
  elasticsearch {
    action => "index"
    hosts  => "127.0.0.1:9200"
    index  => "applog"
  }
}

6、在项目中创建logback-spring.xml

< destination>localhost:4560</ destination>
中的地址为logstash.conf设置的端口号

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <destination>localhost:4560</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" />
    </appender>

    <root level="INFO">
        <appender-ref ref="LOGSTASH" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

7、创建controller类设定测试数据

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/elastic")
public class ElkController {

    Logger logger = LoggerFactory.getLogger(ElkController.class);

    @Autowired
    private ElkService elkService;

    @PostMapping
    public void create(){
        elkService.createIndex();
    }

    @RequestMapping("/test")
    public String test2(){
        logger.info("你好啊e");
        logger.warn("This is a warn message!");
        logger.error("This is error message!");
        return "ELK测试数据";
    }

  




}

8、打开localhost:5601,创建索引值,索引值跟logstash.conf中的outputindex一样
在这里插入图片描述

9、回到首页查看生成的日志信息
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值