优化统计信息获取
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
package com.genersoft.iot.vmp.conf;
|
package com.genersoft.iot.vmp.conf;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
import com.genersoft.iot.vmp.common.StatisticsInfo;
|
import com.genersoft.iot.vmp.common.StatisticsInfo;
|
||||||
|
import com.genersoft.iot.vmp.utils.DateUtil;
|
||||||
import com.genersoft.iot.vmp.utils.GitUtil;
|
import com.genersoft.iot.vmp.utils.GitUtil;
|
||||||
import com.genersoft.iot.vmp.utils.SystemInfoUtils;
|
import com.genersoft.iot.vmp.utils.SystemInfoUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
@@ -10,10 +14,17 @@ import org.springframework.data.redis.connection.RedisConnection;
|
|||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ConnectException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Order(value=100)
|
@Order(value=100)
|
||||||
|
@Slf4j
|
||||||
public class StatisticsInfoTask implements CommandLineRunner {
|
public class StatisticsInfoTask implements CommandLineRunner {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -22,23 +33,39 @@ public class StatisticsInfoTask implements CommandLineRunner {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RedisTemplate<Object, Object> redisTemplate;
|
private RedisTemplate<Object, Object> redisTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) throws Exception {
|
public void run(String... args) throws Exception {
|
||||||
StatisticsInfo statisticsInfo = new StatisticsInfo();
|
try {
|
||||||
statisticsInfo.setDeviceId(SystemInfoUtils.getHardwareId());
|
StatisticsInfo statisticsInfo = new StatisticsInfo();
|
||||||
statisticsInfo.setBranch(gitUtil.getBranch());
|
statisticsInfo.setDeviceId(SystemInfoUtils.getHardwareId());
|
||||||
statisticsInfo.setGitCommitId(gitUtil.getGitCommitId());
|
statisticsInfo.setBranch(gitUtil.getBranch());
|
||||||
statisticsInfo.setGitUrl(gitUtil.getGitUrl());
|
statisticsInfo.setGitCommitId(gitUtil.getGitCommitId());
|
||||||
|
statisticsInfo.setGitUrl(gitUtil.getGitUrl());
|
||||||
|
statisticsInfo.setVersion(gitUtil.getBuildVersion());
|
||||||
|
|
||||||
statisticsInfo.setOsName(System.getProperty("os.name"));
|
statisticsInfo.setOsName(System.getProperty("os.name"));
|
||||||
statisticsInfo.setArch(System.getProperty("os.arch"));
|
statisticsInfo.setArch(System.getProperty("os.arch"));
|
||||||
statisticsInfo.setJdkVersion(System.getProperty("java.version"));
|
statisticsInfo.setJdkVersion(System.getProperty("java.version"));
|
||||||
|
|
||||||
statisticsInfo.setDocker(new File("/.dockerenv").exists());
|
statisticsInfo.setDocker(new File("/.dockerenv").exists());
|
||||||
|
try {
|
||||||
|
statisticsInfo.setRedisVersion(getRedisVersion());
|
||||||
|
}catch (Exception ignored) {}
|
||||||
|
try {
|
||||||
|
DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
|
||||||
|
statisticsInfo.setSqlVersion(metaData.getDatabaseProductVersion());
|
||||||
|
statisticsInfo.setSqlType(metaData.getDriverName());
|
||||||
|
}catch (Exception ignored) {}
|
||||||
|
statisticsInfo.setTime(DateUtil.getNow());
|
||||||
|
sendPost(statisticsInfo);
|
||||||
|
|
||||||
statisticsInfo.setRedisVersion(getRedisVersion());
|
|
||||||
|
|
||||||
// statisticsInfo.setSqlVersion();
|
}catch (Exception e) {
|
||||||
|
log.error("[获取信息失败] ", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRedisVersion() {
|
public String getRedisVersion() {
|
||||||
@@ -51,4 +78,24 @@ public class StatisticsInfoTask implements CommandLineRunner {
|
|||||||
}
|
}
|
||||||
return connection.info().getProperty("redis_version");
|
return connection.info().getProperty("redis_version");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendPost(StatisticsInfo statisticsInfo) {
|
||||||
|
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
|
||||||
|
OkHttpClient client = httpClientBuilder.build();
|
||||||
|
|
||||||
|
RequestBody requestBodyJson = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(statisticsInfo));
|
||||||
|
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.post(requestBodyJson)
|
||||||
|
// .url("https://doc.wvp-pro.cn:11236/api/statistics/ping")
|
||||||
|
.url("http://127.0.0.1:11236/api/statistics/ping")
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
Response response = client.newCall(request).execute();
|
||||||
|
response.close();
|
||||||
|
Objects.requireNonNull(response.body()).close();
|
||||||
|
|
||||||
|
}catch (Exception ignored){}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user