fix: influxdb数据库

This commit is contained in:
2026-03-24 14:47:52 +08:00
parent ae95d4dfa3
commit de0c031b2b
9 changed files with 310 additions and 16 deletions

View File

@@ -19,6 +19,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -33,6 +34,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {
/* 解决druid 日志报错discard long time none received connection:xxx */

View File

@@ -0,0 +1,116 @@
package org.nl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.nl.iot.core.driver.bo.SiteBO;
import org.nl.iot.core.driver.entity.RValue;
import org.nl.iot.modular.influxdb.service.PlcSignalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
*
* @Author: liyongde
* @Date: 2026/3/24 13:58
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class AsyncInfluxDBTest {
@Autowired
private PlcSignalService plcSignalService;
/**
* 测试异步批量保存
*/
@Test
public void testAsyncBatchSave() {
log.info("开始测试异步批量保存...");
// 构造测试数据
List<RValue> testValues = createTestData();
// 记录开始时间
long startTime = System.currentTimeMillis();
// 异步保存
CompletableFuture<Void> future = plcSignalService.batchSaveSignalToInfluxDBAsync(testValues);
// 主线程继续执行其他任务
log.info("异步任务已提交,主线程继续执行其他任务...");
doOtherWork();
// 等待异步任务完成(可选)
future.whenComplete((result, throwable) -> {
long duration = System.currentTimeMillis() - startTime;
if (throwable != null) {
log.error("异步保存失败,耗时: {}ms", duration, throwable);
} else {
log.info("异步保存成功,耗时: {}ms", duration);
}
});
log.info("测试方法执行完成");
}
/**
* 对比同步和异步性能
*/
@Test
public void comparePerformance() {
List<RValue> testValues = createTestData();
// 测试同步方式
long syncStart = System.currentTimeMillis();
plcSignalService.batchSaveSignalToInfluxDB(testValues);
long syncDuration = System.currentTimeMillis() - syncStart;
log.info("同步保存耗时: {}ms", syncDuration);
// 测试异步方式
long asyncStart = System.currentTimeMillis();
CompletableFuture<Void> future = plcSignalService.batchSaveSignalToInfluxDBAsync(testValues);
long asyncSubmitDuration = System.currentTimeMillis() - asyncStart;
log.info("异步任务提交耗时: {}ms", asyncSubmitDuration);
// 等待异步任务完成
future.join();
long asyncTotalDuration = System.currentTimeMillis() - asyncStart;
log.info("异步任务总耗时: {}ms", asyncTotalDuration);
}
private List<RValue> createTestData() {
List<RValue> testValues = new ArrayList<>();
for (int i = 0; i < 100; i++) {
SiteBO siteBO = SiteBO.builder()
.deviceCode("TEST_DEVICE_" + (i % 10))
.alias("TEST_TAG_" + i)
.build();
RValue rValue = RValue.builder()
.siteBO(siteBO)
.value("TEST_VALUE_" + i)
.build();
testValues.add(rValue);
}
return testValues;
}
private void doOtherWork() {
// 模拟其他业务逻辑
try {
Thread.sleep(100);
log.info("其他业务逻辑执行完成");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

View File

@@ -0,0 +1,76 @@
# 测试环境配置
spring:
profiles:
active: test
main:
allow-circular-references: true
datasource:
dynamic:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.81.251:3306/acs3.0?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&useInformationSchema=true&rewriteBatchedStatements=true
username: root
password: "P@ssw0rd."
strict: true
public-key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMWiTVtdXFVrgFHDDKELZM0SywkWY3KjugN90eY5Sogon1j8Y0ClPF7nx3FuE7pAeBKiv7ChIS0vvx/59WUpKmUCAwEAAQ==
data:
redis:
database: 1
host: 127.0.0.1
port: 6379
password: ""
timeout: 10s
jackson:
time-zone: GMT+8
date-format: "yyyy-MM-dd HH:mm:ss"
locale: zh_CN
serialization:
write-dates-as-timestamps: false
# InfluxDB 2.x 测试配置
influx:
# 服务地址
url: http://117.72.202.142:15964/
# 初始化生成的All-Access令牌
token: mr8DZXza-WvBtHhK51Jnfh0tNlq_rLyVYW4D9Zyz0IN66RM9KqgS8agV4XWtXekCgGhG866Dtb9Jv0t-N5D0Gw==
# 组织名称
org: acs
# 数据桶名称替代1.x的database
bucket: signals
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
jdbc-type-for-null: "null"
global-config:
banner: false
enable-sql-runner: true
db-config:
id-type: ASSIGN_ID
logic-delete-field: DELETE_FLAG
logic-delete-value: DELETED
logic-not-delete-value: NOT_DELETE
mapper-locations: classpath*:org/nl/**/mapping/*.xml
type-handlers-package: org.nl.common.handler
# sa-token configuration
sa-token:
token-name: token
timeout: 2592000
active-timeout: -1
is-concurrent: true
is-share: false
max-login-count: -1
token-style: random-32
is-log: false
is-print: false
#########################################
# easy-trans configuration
#########################################
easy-trans:
is-enable-redis: true
is-enable-global: true
is-enable-tile: true
is-enable-cloud: false