Skip to content

Commit a8948d9

Browse files
committed
使用时序数据库 InfluxDB
1 parent 6275fa4 commit a8948d9

File tree

8 files changed

+148
-1
lines changed

8 files changed

+148
-1
lines changed

2.x/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
- [Spring Boot 2.x基础教程:使用MongoDB](http://blog.didispace.com/spring-boot-learning-24-6-1/)
7272
- [Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据](http://blog.didispace.com/spring-boot-learning-24-6-2/)
73+
- [Spring Boot 2.x基础教程:使用时序数据库InfluxDB](http://blog.didispace.com/spring-boot-learning-2-6-3/)
7374

7475
### Web开发
7576

2.x/README_zh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171

7272
- [Spring Boot 2.x基础教程:使用MongoDB](http://blog.didispace.com/spring-boot-learning-24-6-1/)
7373
- [Spring Boot 2.x基础教程:使用LDAP来管理用户与组织数据](http://blog.didispace.com/spring-boot-learning-24-6-2/)
74+
- [Spring Boot 2.x基础教程:使用时序数据库InfluxDB](http://blog.didispace.com/spring-boot-learning-2-6-3/)
75+
7476

7577
### Web开发
7678

2.x/chapter6-3/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>chapter6-3</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
<description>使用时序数据库InfluxDB</description>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.5.1</version>
16+
<relativePath/> <!-- lookup parent from repository -->
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.influxdb</groupId>
33+
<artifactId>influxdb-java</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.projectlombok</groupId>
38+
<artifactId>lombok</artifactId>
39+
<scope>provided</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter63;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@EnableScheduling
8+
@SpringBootApplication
9+
public class Chapter63Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter63Application.class, args);
13+
}
14+
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.didispace.chapter63;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.influxdb.InfluxDB;
6+
import org.influxdb.dto.Point;
7+
import org.springframework.scheduling.annotation.Scheduled;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.Random;
11+
import java.util.concurrent.TimeUnit;
12+
13+
/**
14+
* Created by 程序猿DD on 2021/8/2.
15+
* <p>
16+
* Blog: http://blog.didispace.com/
17+
* Github: https://github.com/dyc87112/
18+
*/
19+
@Service
20+
@AllArgsConstructor
21+
@Slf4j
22+
public class Monitor {
23+
24+
private InfluxDB influxDB;
25+
26+
@Scheduled(fixedRate = 5000)
27+
public void writeQPS() {
28+
// 模拟要上报的统计数据
29+
int count = (int) (Math.random() * 100);
30+
31+
Point point = Point.measurement("ApiQPS") // ApiQPS表
32+
.tag("url", "/hello") // url字段
33+
.addField("count", count) // 统计数据
34+
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) // 时间
35+
.build();
36+
37+
// 往test库写数据
38+
influxDB.write("test", "autogen", point);
39+
40+
log.info("上报统计数据:" + count);
41+
}
42+
43+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
spring.influx.url=http://localhost:8086
3+
spring.influx.user=admin
4+
spring.influx.password=
5+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace.chapter63;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
@Slf4j
9+
@SpringBootTest
10+
public class ApplicationTests {
11+
12+
@Test
13+
public void findAll() {
14+
15+
}
16+
17+
@Test
18+
public void save() {
19+
20+
}
21+
22+
}

2.x/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<!-- 非关系型的数据储存的使用 -->
6666
<module>chapter6-1</module> <!-- 6-1 使用MongoDB -->
6767
<module>chapter6-2</module> <!-- 6-2 使用轻量级树状存储 LDAP -->
68-
<!-- 6-3 使用时序数据库 InfluxDB -->
68+
<module>chapter6-3</module> <!-- 6-3 使用时序数据库 InfluxDB -->
6969
<!-- 6-4 使用Elasticsearch -->
7070

7171
<!-- 任务管理 -->

0 commit comments

Comments
 (0)