Integrate InfluxDB with Spring Boot: A Step-by-Step Guide to Time‑Series Data
Learn how to configure and use the open‑source time‑series database InfluxDB in a Spring Boot application, covering essential concepts, dependency setup, property configuration, scheduled data writing, and verification via InfluxDB CLI, with complete code examples.
After covering relational databases, caches, MongoDB and LDAP, this article introduces another special database: the time‑series database InfluxDB and shows how to use it in a Spring Boot project.
InfluxDB Introduction
A time‑series database stores data that is indexed by timestamps. It is ideal for high‑frequency, time‑ordered data such as industrial monitoring, where data arrives quickly, each point has a unique time, and many measurement points generate large volumes of data.
InfluxDB is a popular open‑source time‑series database, commonly used for storing and querying high‑frequency monitoring data.
Key terminology:
database – the database instance
measurement – similar to a table
points – similar to a row
A point consists of three parts:
time – timestamp
fields – recorded values
tags – indexed attributes
Hands‑On Example
We will build a simple scheduled task that reports monitoring data to InfluxDB.
Step 1 : Create a basic Spring Boot project (refer to a quick‑start guide if needed).
Step 2 : Add the official InfluxDB Java SDK to pom.xml:
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
</dependency>Step 3 : Configure the connection properties in application.properties (or application.yml).
spring.influx.url=http://localhost:8086
spring.influx.user=admin
spring.influx.password=These properties represent the URL, username, and password of the InfluxDB instance. Spring Boot 2.x auto‑configures InfluxDB, so no additional version specification is required.
Step 4 : Create a scheduled service that writes a point to InfluxDB every five seconds:
@Service
@AllArgsConstructor
@Slf4j
public class Monitor {
private InfluxDB influxDB;
@Scheduled(fixedRate = 5000)
public void writeQPS() {
// Simulate a metric value
int count = (int) (Math.random() * 100);
Point point = Point.measurement("ApiQPS")
.tag("url", "/hello")
.addField("count", count)
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.build();
// Write to the "test" database
influxDB.write("test", "autogen", point);
log.info("Reported metric: " + count);
}
}Testing and Verification
Step 1 : Start InfluxDB and create the database used in the code:
$ influx
> show databases
> create database "test"Step 2 : Run the Spring Boot application. The scheduled task will produce log entries such as:
2021-08-03 01:52:47.764 INFO --- [ scheduling-1] com.didispace.chapter63.Monitor : 上报统计数据:25
2021-08-03 01:52:52.736 INFO --- [ scheduling-1] com.didispace.chapter63.Monitor : 上报统计数据:30
...Step 3 : Query InfluxDB to confirm the data was written:
> select * from ApiQPS order by time desc;
name: ApiQPS
time count url
---- ----- ---
1627926787730000000 31 /hello
1627926782730000000 51 /hello
...The query output shows the same metric values that appeared in the application logs, confirming successful integration.
This tutorial demonstrates how to set up InfluxDB with Spring Boot, write time‑series data programmatically, and verify the results using the InfluxDB CLI.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
