完成InfluxDB历史数据存储
This commit is contained in:
parent
13a15db7d2
commit
e1e31c1413
@ -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(timestamp = true)
|
||||
Instant timestamp;
|
||||
@Column
|
||||
double cpuUsage;
|
||||
@Column
|
||||
double memoryUsage;
|
||||
@Column
|
||||
double diskUsage;
|
||||
@Column
|
||||
double networkUpload;
|
||||
@Column
|
||||
double networkDownload;
|
||||
@Column
|
||||
double diskRead;
|
||||
@Column
|
||||
double diskWrite;
|
||||
}
|
@ -44,6 +44,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
Client client = service.findClientByToken(authorization);
|
||||
if(client == null) {
|
||||
response.setStatus(401);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.getWriter().write(RestBean.failure(401, "未注册").asJsonString());
|
||||
return;
|
||||
} else {
|
||||
|
@ -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 org.springframework.beans.BeanUtils;
|
||||
@ -31,6 +32,9 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
@Resource
|
||||
ClientDetailMapper detailMapper;
|
||||
|
||||
@Resource
|
||||
InfluxDbUtils influx;
|
||||
|
||||
@PostConstruct
|
||||
public void initClientCache() {
|
||||
this.list().forEach(this::addClientCache);
|
||||
@ -77,16 +81,17 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Integer, RuntimeDetailVO> currentRuntime = new ConcurrentHashMap<>();
|
||||
private final Map<Integer, RuntimeDetailVO> currentRuntime = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void updateRuntimeDetail(RuntimeDetailVO vo, Client client) {
|
||||
currentRuntime.put(client.getId(), vo);
|
||||
System.out.println(vo);
|
||||
influx.writeRuntimeData(client.getId(), vo);
|
||||
}
|
||||
|
||||
private void addClientCache(Client client) {
|
||||
clientIdCache.put(client.getId(), client);
|
||||
clientTokenCache.put(client.getToken(), client);
|
||||
}
|
||||
|
||||
private int randomClientId() {
|
||||
|
@ -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
|
||||
public 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user