fix: influxdb数据库
This commit is contained in:
56
nl-iot/src/main/java/org/nl/iot/config/AsyncConfig.java
Normal file
56
nl-iot/src/main/java/org/nl/iot/config/AsyncConfig.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package org.nl.iot.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.AsyncConfigurer;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* 异步任务配置
|
||||
*
|
||||
* @author lyd
|
||||
* @date 2026/03/24
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class AsyncConfig implements AsyncConfigurer {
|
||||
|
||||
/**
|
||||
* InfluxDB异步写入线程池
|
||||
*/
|
||||
@Bean("influxDbAsyncExecutor")
|
||||
public Executor influxDbAsyncExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
// 核心线程数
|
||||
executor.setCorePoolSize(2);
|
||||
// 最大线程数
|
||||
executor.setMaxPoolSize(8);
|
||||
// 队列容量
|
||||
executor.setQueueCapacity(200);
|
||||
// 线程名前缀
|
||||
executor.setThreadNamePrefix("InfluxDB-Async-");
|
||||
// 线程空闲时间
|
||||
executor.setKeepAliveSeconds(60);
|
||||
// 拒绝策略:调用者运行策略
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
// 等待所有任务结束后再关闭线程池
|
||||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||||
// 等待时间
|
||||
executor.setAwaitTerminationSeconds(60);
|
||||
executor.initialize();
|
||||
|
||||
log.info("InfluxDB异步线程池初始化完成 - 核心线程数: {}, 最大线程数: {}, 队列容量: {}",
|
||||
executor.getCorePoolSize(), executor.getMaxPoolSize(), executor.getQueueCapacity());
|
||||
|
||||
return executor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Executor getAsyncExecutor() {
|
||||
return influxDbAsyncExecutor();
|
||||
}
|
||||
}
|
||||
@@ -230,8 +230,14 @@ public class MetadataCacheManager implements CommandLineRunner {
|
||||
needSaveValues.add(newRValue);
|
||||
}
|
||||
}
|
||||
// 批量存储到influxDB
|
||||
plcSignalService.batchSaveSignalToInfluxDB(needSaveValues);
|
||||
// 异步批量存储到influxDB
|
||||
if (!needSaveValues.isEmpty()) {
|
||||
plcSignalService.batchSaveSignalToInfluxDBAsync(needSaveValues)
|
||||
.exceptionally(throwable -> {
|
||||
log.error("异步保存信号到InfluxDB失败,设备: {}, 数量: {}", deviceKey, needSaveValues.size(), throwable);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.nl.iot.core.influxDB;
|
||||
|
||||
import com.influxdb.client.*;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -13,7 +12,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @Date: 2026/3/24 10:38
|
||||
* @Modified By:
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class InfluxDB2Config {
|
||||
|
||||
@@ -29,6 +27,8 @@ public class InfluxDB2Config {
|
||||
@Value("${influx.bucket}")
|
||||
private String bucket;
|
||||
|
||||
private InfluxDBClient influxDBClient;
|
||||
|
||||
/**
|
||||
* 初始化InfluxDB 2.x客户端
|
||||
* 修复:通过OkHttpClient配置超时,替代不存在的set超时方法
|
||||
@@ -37,7 +37,7 @@ public class InfluxDB2Config {
|
||||
public InfluxDBClient influxDBClient() {
|
||||
|
||||
// 创建客户端,传入自定义OkHttp配置
|
||||
InfluxDBClient client = InfluxDBClientFactory.create(
|
||||
this.influxDBClient = InfluxDBClientFactory.create(
|
||||
url,
|
||||
token.toCharArray(),
|
||||
org,
|
||||
@@ -45,9 +45,8 @@ public class InfluxDB2Config {
|
||||
);
|
||||
|
||||
// 测试连接
|
||||
client.ping();
|
||||
log.info("InfluxDB 2.x 连接成功");
|
||||
return client;
|
||||
influxDBClient.ping();
|
||||
return influxDBClient;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +76,8 @@ public class InfluxDB2Config {
|
||||
*/
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
log.info("InfluxDB 2.x 连接关闭");
|
||||
influxDBClient().close();
|
||||
if (influxDBClient != null) {
|
||||
influxDBClient.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import com.influxdb.client.WriteApi;
|
||||
import com.influxdb.client.domain.WritePrecision;
|
||||
import com.influxdb.query.FluxRecord;
|
||||
import com.influxdb.query.FluxTable;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.nl.iot.modular.influxdb.entity.PlcSignal;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -28,10 +28,10 @@ public class InfluxDB2Util {
|
||||
@Value("${influx.org}")
|
||||
private String org;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private WriteApi writeApi;
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private QueryApi queryApi;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package org.nl.iot.modular.influxdb.service;
|
||||
|
||||
import org.nl.iot.core.driver.entity.RValue;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* influxdb存储信号变化值
|
||||
@@ -21,8 +23,16 @@ public interface PlcSignalService {
|
||||
void savePlcChangeSignal(String device, String tag, String newValue);
|
||||
|
||||
/**
|
||||
* 批量存储
|
||||
* 批量存储(同步)
|
||||
* @param needSaveValues
|
||||
*/
|
||||
void batchSaveSignalToInfluxDB(List<RValue> needSaveValues);
|
||||
|
||||
/**
|
||||
* 批量存储(异步)
|
||||
* @param needSaveValues
|
||||
* @return CompletableFuture<Void>
|
||||
*/
|
||||
@Async("influxDbAsyncExecutor")
|
||||
CompletableFuture<Void> batchSaveSignalToInfluxDBAsync(List<RValue> needSaveValues);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
package org.nl.iot.modular.influxdb.service.impl;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.iot.core.driver.bo.SiteBO;
|
||||
import org.nl.iot.core.driver.entity.RValue;
|
||||
import org.nl.iot.core.influxDB.InfluxDB2Util;
|
||||
import org.nl.iot.modular.influxdb.entity.PlcSignal;
|
||||
import org.nl.iot.modular.influxdb.service.PlcSignalService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
*
|
||||
* @Author: liyongde
|
||||
* @Date: 2026/3/24 13:11
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PlcSignalServiceImpl implements PlcSignalService {
|
||||
|
||||
@Resource
|
||||
@Autowired
|
||||
private InfluxDB2Util influxDB2Util;
|
||||
|
||||
@Override
|
||||
@@ -36,6 +40,10 @@ public class PlcSignalServiceImpl implements PlcSignalService {
|
||||
|
||||
@Override
|
||||
public void batchSaveSignalToInfluxDB(List<RValue> needSaveValues) {
|
||||
if (needSaveValues == null || needSaveValues.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<PlcSignal> plcSignals = new ArrayList<>();
|
||||
for (RValue needSaveValue : needSaveValues) {
|
||||
SiteBO siteBO = needSaveValue.getSiteBO();
|
||||
@@ -47,6 +55,26 @@ public class PlcSignalServiceImpl implements PlcSignalService {
|
||||
.build();
|
||||
plcSignals.add(plcSignal);
|
||||
}
|
||||
|
||||
try {
|
||||
influxDB2Util.insertBatch(plcSignals);
|
||||
log.debug("批量保存信号到InfluxDB成功,数量: {}", plcSignals.size());
|
||||
} catch (Exception e) {
|
||||
log.error("批量保存信号到InfluxDB失败,数量: {}", plcSignals.size(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("influxDbAsyncExecutor")
|
||||
public CompletableFuture<Void> batchSaveSignalToInfluxDBAsync(List<RValue> needSaveValues) {
|
||||
try {
|
||||
batchSaveSignalToInfluxDB(needSaveValues);
|
||||
log.debug("异步批量保存信号到InfluxDB成功,数量: {}", needSaveValues.size());
|
||||
return CompletableFuture.completedFuture(null);
|
||||
} catch (Exception e) {
|
||||
log.error("异步批量保存信号到InfluxDB失败,数量: {}", needSaveValues.size(), e);
|
||||
return CompletableFuture.failedFuture(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user