一.服务安装
docker search influxdb
docker pull influxdb
docker run -dit --name influxdb --restart always -p 8086:8086 -v /dp/docker/file/influxdb:/var/lib/influxdb influxdb
访问8086
初始化 账号组织和新建bucket 创建密钥 这些豆记录下来
二.项目配置
引入依赖
<dependency><groupId>com.influxdb</groupId><artifactId>influxdb-spring</artifactId><version>7.1.0</version></dependency>
yml 配置
influx:url: http://localhost:8086/username: influxdbpassword: xjaq1234token: wOzhZxDpKrH0lyNcCCy3tG3U582ie_mrgk_UGu4Eb2UgzrhhVN0O0xyvifloxjaKzHiENIbQO7IlaxreIXnYDA==org: xjbucket: newPowerlogLevel: BODYreadTimeout: 5swriteTimeout: 5sconnectTimeout: 5smanagement:influx:metrics:export:api-version: v2enabled: truestep: 10suri: http://localhost:8086auto-create-db: trueuser-name: influxdbpassword: xjaq1234org: xjbucket: newPowertoken: wOzhZxDpKrH0lyNcCCy3tG3U582ie_mrgk_UGu4Eb2UgzrhhVN0O0xyvifloxjaKzHiENIbQO7IlaxreIXnYDA==
influx 是配置influx 配置健康检测数据会不端上传到influxdb(不配置会一直报错 因为依赖中自动帮你引入了,要么手动把他排除掉
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-influx</artifactId><version>${micrometer.version}</version></dependency>
)
代码书写
package com.chenkang.demo.controller;import com.chenkang.demo.entity.DeviceInfo;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxQLQueryApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.InfluxQLQuery;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.query.InfluxQLQueryResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;/*** @author chenkang* @since 2024/7/13 9:33*/
@AllArgsConstructor
@RestController
@RequestMapping("influxdb")
public class InfluxdbController {private final InfluxDBClient influxDBClient;/*** 写入数据*/@GetMapping("writeData")public void test() {WriteApiBlocking writeApiBlocking = influxDBClient.getWriteApiBlocking();IntStream.range(0, 100).forEach(i -> {DeviceInfo deviceInfo = new DeviceInfo();deviceInfo.setKey("wendu");deviceInfo.setValue(ThreadLocalRandom.current().nextDouble());//The format or precision of time series timestamps.writeApiBlocking.writeMeasurement(WritePrecision.NS, deviceInfo);});}/*** 写入数据*/@GetMapping("queryData")public void queryData() {InfluxQLQueryApi influxQLQueryApi = influxDBClient.getInfluxQLQueryApi();String query = "SELECT * from deviceInfo";InfluxQLQueryResult qlQueryResult = influxQLQueryApi.query(new InfluxQLQuery(query, "newPower").setPrecision(InfluxQLQuery.InfluxQLPrecision.SECONDS));List<InfluxQLQueryResult.Result> results = qlQueryResult.getResults();for (InfluxQLQueryResult.Result r : results) {for (InfluxQLQueryResult.Series series : r.getSeries()) {List<InfluxQLQueryResult.Series.Record> values1 = series.getValues();Map<String, Integer> columns1 = series.getColumns();System.out.println(values1);System.out.println(columns1);}}}
}
mport java.time.Instant;/*** @author chenkang* @since 2024/7/13 10:04*/
@Data
@Measurement(name = "deviceInfo")
public class DeviceInfo {@Column(timestamp = true)Instant time;/*** tag 类似索引 要求不能超过10万*/@Column(tag = true, name = "id")private String deviceId;@Column(tag = true, name = "key")private String key;@Column(name = "value")private double value;}