对接InfluxDB实现历史数据保存
This commit is contained in:
parent
4bb27f6d5c
commit
e01b4704ad
@ -94,6 +94,12 @@
|
|||||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
<version>2.1.0</version>
|
<version>2.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- InfluxDB数据库 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.influxdb</groupId>
|
||||||
|
<artifactId>influxdb-client-java</artifactId>
|
||||||
|
<version>6.6.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<profiles>
|
<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
|
@Resource
|
||||||
SnowflakeIdGenerator generator;
|
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
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
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.ClientDetailMapper;
|
||||||
import com.example.mapper.ClientMapper;
|
import com.example.mapper.ClientMapper;
|
||||||
import com.example.service.ClientService;
|
import com.example.service.ClientService;
|
||||||
|
import com.example.utils.InfluxDbUtils;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -29,6 +30,9 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
|||||||
@Resource
|
@Resource
|
||||||
private ClientDetailMapper detailMapper;
|
private ClientDetailMapper detailMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private InfluxDbUtils influx;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void initClientCache() {
|
public void initClientCache() {
|
||||||
this.list().forEach(this::addClientCache);
|
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
|
@Override
|
||||||
public void updateRuntimeDetail(RuntimeDetailVO vo, Client client) {
|
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) {
|
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:
|
swagger-ui:
|
||||||
operations-sorter: alpha
|
operations-sorter: alpha
|
||||||
spring:
|
spring:
|
||||||
|
influx:
|
||||||
|
url: http://localhost:8086
|
||||||
|
user: admin
|
||||||
|
password: 12345678
|
||||||
mail:
|
mail:
|
||||||
host: smtp.163.com
|
host: smtp.163.com
|
||||||
username: javastudy111@163.com
|
username: javastudy111@163.com
|
||||||
|
@ -8,6 +8,10 @@ mybatis-plus:
|
|||||||
configuration:
|
configuration:
|
||||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||||
spring:
|
spring:
|
||||||
|
influx:
|
||||||
|
url: http://localhost:8086
|
||||||
|
user: admin
|
||||||
|
password: 12345678
|
||||||
mail:
|
mail:
|
||||||
host: smtp.163.com
|
host: smtp.163.com
|
||||||
username: javastudy111@163.com
|
username: javastudy111@163.com
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
<module type="WEB_MODULE" version="4">
|
<module type="WEB_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.vscode" />
|
||||||
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="index" level="application" />
|
<orderEntry type="library" name="index" level="application" />
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/config" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/log" />
|
<excludeFolder url="file://$MODULE_DIR$/log" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user