How to Set Up the ELK Stack with Spring Boot 2.4.12
Learn step‑by‑step how to install and configure Elasticsearch, Kibana, and Logstash, integrate them with a Spring Boot 2.4.12 application, and use Kibana to view logs, covering environment setup, service startup, configuration files, and sample code.
Environment: Spring Boot 2.4.12, Elasticsearch 7.8.0, Logstash 7.8.0, Kibana 7.8.0.
Elasticsearch Installation and Configuration
Download the package.
Start Elasticsearch with %elasticsearch_home%\bin\elasticsearch.bat .
Check the running status (default port 9200, adjust in %elasticsearch_home%\config\elasticsearch.yml if needed).
Kibana Installation and Configuration
Download the package.
Edit %kibana_home%\config\kibana.yml (e.g., set elasticsearch.hosts: ["http://localhost:9201"] , kibana.index: ".kibana" , i18n.locale: "zh-CN" ).
Start Kibana and verify the success message.
Logstash Installation and Configuration
Download the package.
Create %logstash_home%\bin\logstash.conf with the following content:
<code>input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
type => "es-demo"
}
}
output {
if [type] == "es-demo" {
elasticsearch {
hosts => "localhost:9201"
index => "elk-demo-%{+YYYY.MM.dd}"
}
}
}
</code>Start Logstash with logstash -f logstash.conf .
At this point the ELK integration is complete.
Spring Boot Project
Add Maven dependencies for spring-boot-starter-web and logstash-logback-encoder (version 6.6).
Configure logback-spring.xml as needed.
Set application properties (logging levels, active profile).
Create a test controller:
<code>@RestController
@RequestMapping("/elk")
public class ElkController {
private static Logger logger = LoggerFactory.getLogger(ElkController.class);
@GetMapping("/index")
public Object index(String info, Integer a) {
logger.info("You entered: {}", info);
if (a == 0) {
logger.error("Error occurred: {}", new RuntimeException("Invalid number"));
}
return "success";
}
}
</code>After running the application, use Kibana to create an index pattern, explore the logs in the Discover view, and perform various operations.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.