小编给大家分享一下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
中的output
的index
一样
9、回到首页查看生成的日志信息
以上是SpringBoot2.3集成ELK7.1.0的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。