java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot2.3集成ELK7.1.0

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

作者:陈汤姆

这篇文章主要介绍了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、回到首页查看生成的日志信息

到此这篇关于SpringBoot2.3集成ELK7.1.0的示例代码的文章就介绍到这了,更多相关SpringBoot2.3集成ELK7.1.0内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文