SpringBoot2.3集成ELK7.1.0的示例代碼
最近想用ELK做日志分析,所以先寫(xiě)了Demo來(lái)實(shí)驗(yàn)一下!
1、安裝ELK(Elasticsearch+Logstash+Kibana),具體安裝教程百度
2、查看是否安裝成功,輸入localhost:9200
,localhost:5601
,如下頁(yè)面則安裝成功
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目錄下創(chuàng)建logstash.conf配置文件,啟動(dòng)logstash時(shí)要依賴這個(gè)配置文件
logstash.conf
input { tcp { mode => "server" port => 4560 codec => json_lines } } output { elasticsearch { action => "index" hosts => "127.0.0.1:9200" index => "applog" } }
6、在項(xiàng)目中創(chuàng)建logback-spring.xml
< destination>localhost:4560</ destination>
中的地址為logstash.conf設(shè)置的端口號(hào)
<?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、創(chuàng)建controller類(lèi)設(shè)定測(cè)試數(shù)據(jù)
/** * @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測(cè)試數(shù)據(jù)"; } }
8、打開(kāi)localhost:5601,創(chuàng)建索引值,索引值跟logstash.conf
中的output
的index
一樣
9、回到首頁(yè)查看生成的日志信息
到此這篇關(guān)于SpringBoot2.3集成ELK7.1.0的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot2.3集成ELK7.1.0內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)最小生成樹(shù)算法詳解
這篇文章主要介紹了如何在Java中實(shí)現(xiàn)最小生成樹(shù)算法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-04-04Java定義棧結(jié)構(gòu),并實(shí)現(xiàn)入棧、出棧操作完整示例
這篇文章主要介紹了Java定義棧結(jié)構(gòu),并實(shí)現(xiàn)入棧、出棧操作,結(jié)合完整實(shí)例形式分析了java數(shù)據(jù)結(jié)構(gòu)中棧的定義、以及入棧、出棧、棧是否為空判斷、棧大小計(jì)算、打印棧元素等相關(guān)操作技巧,需要的朋友可以參考下2020-02-02Java負(fù)載均衡算法實(shí)現(xiàn)之輪詢和加權(quán)輪詢
網(wǎng)上找了不少負(fù)載均衡算法的資源,都不夠全面,后來(lái)自己結(jié)合了網(wǎng)上的一些算法實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Java負(fù)載均衡算法實(shí)現(xiàn)之輪詢和加權(quán)輪詢的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04java去除字符串中的空格、回車(chē)、換行符、制表符的小例子
java去除字符串中的空格、回車(chē)、換行符、制表符的小例子,需要的朋友可以參考一下2013-06-06Mybatis動(dòng)態(tài)SQL之IF語(yǔ)句詳解
這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)SQL之IF語(yǔ)句的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05基于hashmap 的擴(kuò)容和樹(shù)形化全面分析
這篇文章主要介紹了hashmap 的擴(kuò)容和樹(shù)形化的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例
這篇文章主要介紹了Springboot WebFlux集成Spring Security實(shí)現(xiàn)JWT認(rèn)證的示例,幫助大家更好的理解和學(xué)習(xí)使用springboot框架,感興趣的朋友可以了解下2021-04-04