对接InfluxDB实现历史数据保存
This commit is contained in:
parent
4bb27f6d5c
commit
e01b4704ad
@ -94,6 +94,12 @@
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<!-- InfluxDB数据库 -->
|
||||
<dependency>
|
||||
<groupId>com.influxdb</groupId>
|
||||
<artifactId>influxdb-client-java</artifactId>
|
||||
<version>6.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
@ -0,0 +1,30 @@
|
||||
package com.example.entity.dto;
|
||||
|
||||
import com.influxdb.annotations.Column;
|
||||
import com.influxdb.annotations.Measurement;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Data
|
||||
@Measurement(name = "runtime")
|
||||
public class RuntimeData {
|
||||
@Column(tag = true)
|
||||
int clientId;
|
||||
@Column
|
||||
double cpuUsage;
|
||||
@Column
|
||||
double memoryUsage;
|
||||
@Column
|
||||
double diskUsage;
|
||||
@Column
|
||||
double networkUpload;
|
||||
@Column
|
||||
double networkDownload;
|
||||
@Column
|
||||
double diskRead;
|
||||
@Column
|
||||
double diskWrite;
|
||||
@Column(timestamp = true)
|
||||
Instant timestamp;
|
||||
}
|
@ -29,7 +29,7 @@ public class RequestLogFilter extends OncePerRequestFilter {
|
||||
@Resource
|
||||
SnowflakeIdGenerator generator;
|
||||
|
||||
private final Set<String> ignores = Set.of("/swagger-ui", "/v3/api-docs");
|
||||
private final Set<String> ignores = Set.of("/swagger-ui", "/v3/api-docs", "/monitor/runtime");
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
|
@ -8,6 +8,7 @@ import com.example.entity.vo.request.RuntimeDetailVO;
|
||||
import com.example.mapper.ClientDetailMapper;
|
||||
import com.example.mapper.ClientMapper;
|
||||
import com.example.service.ClientService;
|
||||
import com.example.utils.InfluxDbUtils;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -29,6 +30,9 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
@Resource
|
||||
private ClientDetailMapper detailMapper;
|
||||
|
||||
@Resource
|
||||
private InfluxDbUtils influx;
|
||||
|
||||
@PostConstruct
|
||||
public void initClientCache() {
|
||||
this.list().forEach(this::addClientCache);
|
||||
@ -76,11 +80,13 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Client, RuntimeDetailVO> test = new HashMap<>();
|
||||
private final Map<Client, RuntimeDetailVO> lastRuntime = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void updateRuntimeDetail(RuntimeDetailVO vo, Client client) {
|
||||
test.put(client, vo);
|
||||
RuntimeDetailVO oldData = lastRuntime.put(client, vo);
|
||||
if(oldData != null)
|
||||
influx.writeRuntimeData(client.getId(), oldData);
|
||||
}
|
||||
|
||||
private void addClientCache(Client client) {
|
||||
|
@ -0,0 +1,44 @@
|
||||
package com.example.utils;
|
||||
|
||||
import com.example.entity.dto.RuntimeData;
|
||||
import com.example.entity.vo.request.RuntimeDetailVO;
|
||||
import com.influxdb.client.InfluxDBClient;
|
||||
import com.influxdb.client.InfluxDBClientFactory;
|
||||
import com.influxdb.client.WriteApiBlocking;
|
||||
import com.influxdb.client.domain.WritePrecision;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
public class InfluxDbUtils {
|
||||
|
||||
@Value("${spring.influx.url}")
|
||||
String url;
|
||||
@Value("${spring.influx.user}")
|
||||
String user;
|
||||
@Value("${spring.influx.password}")
|
||||
String password;
|
||||
|
||||
private final String BUCKET = "monitor";
|
||||
private final String ORG = "itbaima";
|
||||
|
||||
private InfluxDBClient client;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
client = InfluxDBClientFactory.create(url, user, password.toCharArray());
|
||||
}
|
||||
|
||||
public void writeRuntimeData(int clientId, RuntimeDetailVO vo) {
|
||||
RuntimeData data = new RuntimeData();
|
||||
BeanUtils.copyProperties(vo, data);
|
||||
data.setTimestamp(new Date(vo.getTimestamp()).toInstant());
|
||||
data.setClientId(clientId);
|
||||
WriteApiBlocking writeApi = client.getWriteApiBlocking();
|
||||
writeApi.writeMeasurement(BUCKET, ORG, WritePrecision.NS, data);
|
||||
}
|
||||
}
|
@ -4,6 +4,10 @@ springdoc:
|
||||
swagger-ui:
|
||||
operations-sorter: alpha
|
||||
spring:
|
||||
influx:
|
||||
url: http://localhost:8086
|
||||
user: admin
|
||||
password: 12345678
|
||||
mail:
|
||||
host: smtp.163.com
|
||||
username: javastudy111@163.com
|
||||
|
@ -8,6 +8,10 @@ mybatis-plus:
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
spring:
|
||||
influx:
|
||||
url: http://localhost:8086
|
||||
user: admin
|
||||
password: 12345678
|
||||
mail:
|
||||
host: smtp.163.com
|
||||
username: javastudy111@163.com
|
||||
|
@ -2,7 +2,9 @@
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.vscode" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="index" level="application" />
|
||||
|
@ -4,6 +4,7 @@
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/config" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/log" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user