您好,登錄后才能下訂單哦!
小編給大家分享一下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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。