SpringBoot2.3集成ELK7.1.0的示例代码
程序员文章站
2022-09-25 15:10:48
最近想用elk做日志分析,所以先写了demo来实验一下!1、安装elk(elasticsearch+logstash+kibana),具体安装教程百度2、查看是否安装成功,输入localhost:92...
最近想用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
中的output
的index
一样
9、回到首页查看生成的日志信息
到此这篇关于springboot2.3集成elk7.1.0的示例代码的文章就介绍到这了,更多相关springboot2.3集成elk7.1.0内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!