Skip to content

Commit

Permalink
使用时序数据库 InfluxDB
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc87112 committed Aug 2, 2021
1 parent 6275fa4 commit a8948d9
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions 2.x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

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

### Web开发

Expand Down
2 changes: 2 additions & 0 deletions 2.x/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@

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


### Web开发

Expand Down
59 changes: 59 additions & 0 deletions 2.x/chapter6-3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.didispace</groupId>
<artifactId>chapter6-3</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<description>使用时序数据库InfluxDB</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.didispace.chapter63;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class Chapter63Application {

public static void main(String[] args) {
SpringApplication.run(Chapter63Application.class, args);
}

}
43 changes: 43 additions & 0 deletions 2.x/chapter6-3/src/main/java/com/didispace/chapter63/Monitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.didispace.chapter63;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
* Created by 程序猿DD on 2021/8/2.
* <p>
* Blog: http://blog.didispace.com/
* Github: https://github.com/dyc87112/
*/
@Service
@AllArgsConstructor
@Slf4j
public class Monitor {

private InfluxDB influxDB;

@Scheduled(fixedRate = 5000)
public void writeQPS() {
// 模拟要上报的统计数据
int count = (int) (Math.random() * 100);

Point point = Point.measurement("ApiQPS") // ApiQPS表
.tag("url", "/hello") // url字段
.addField("count", count) // 统计数据
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) // 时间
.build();

// 往test库写数据
influxDB.write("test", "autogen", point);

log.info("上报统计数据:" + count);
}

}
5 changes: 5 additions & 0 deletions 2.x/chapter6-3/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

spring.influx.url=http://localhost:8086
spring.influx.user=admin
spring.influx.password=

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.didispace.chapter63;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@Slf4j
@SpringBootTest
public class ApplicationTests {

@Test
public void findAll() {

}

@Test
public void save() {

}

}
2 changes: 1 addition & 1 deletion 2.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<!-- 非关系型的数据储存的使用 -->
<module>chapter6-1</module> <!-- 6-1 使用MongoDB -->
<module>chapter6-2</module> <!-- 6-2 使用轻量级树状存储 LDAP -->
<!-- 6-3 使用时序数据库 InfluxDB -->
<module>chapter6-3</module> <!-- 6-3 使用时序数据库 InfluxDB -->
<!-- 6-4 使用Elasticsearch -->

<!-- 任务管理 -->
Expand Down

0 comments on commit a8948d9

Please sign in to comment.