亲宝软件园·资讯

展开

SpringBoot2.3集成ELK7.1.0 SpringBoot2.3集成ELK7.1.0的代码实例

陈汤姆 人气:0
想了解SpringBoot2.3集成ELK7.1.0的代码实例的相关内容吗,陈汤姆在本文为您仔细讲解SpringBoot2.3集成ELK7.1.0的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot2.3集成ELK7.1.0,SpringBoot集成ELK,下面大家一起来学习吧。

最近想用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、回到首页查看生成的日志信息

加载全部内容

相关教程
猜你喜欢
用户评论