Authored by FengRuwei

修改influxdb 读写

... ... @@ -40,29 +40,22 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
public void insertJavaApiStatics(String influxDBName, JSONObject statics) {
BatchPoints batchPoints = BatchPoints
.database(InfluxDBContants.APP_ALARM).retentionPolicy("default")
.build();
// BatchPoints batchPoints = BatchPoints
// .database(InfluxDBContants.APP_ALARM).retentionPolicy("default")
// .build();
Point.Builder pointBuilder = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI)
.tag("api_id", "api_id")
.tag("mobj_id", "mobj_id")
.tag("api_name", "api_name")
.tag("api_url", "api_url")
.tag("mobj_ip", "mobj_ip")
.tag("is_exception", "is_exception")
.addField("api_id", statics.getIntValue("api_id"))
.addField("api_name", statics.getString("api_name"))
.addField("api_type", statics.getIntValue("api_type"))
.tag("api_id", statics.getString("api_id"))
.tag("mobj_id", statics.getString("mobj_id"))
.tag("api_name", statics.getString("api_name"))
.addField("api_url", statics.getString("api_url"))
.tag("mobj_ip", statics.getString("mobj_ip"))
.tag("is_exception", statics.getString("is_exception"))
.addField("api_type", statics.getIntValue("api_type"))
.addField("mobj_name", statics.getString("mobj_name"))
.addField("mobj_ip", statics.getString("mobj_ip"))
.addField("mobj_id", statics.getIntValue("mobj_id"))
.addField("mobj_port", statics.getString("mobj_port"))
.addField("start_time", statics.getLongValue("start"))
.addField("end_time", statics.getLongValue("end"))
.addField("is_exception", statics.getBooleanValue("is_exception"))
.time(System.currentTimeMillis() * 1000000 + random.nextInt(999999), TimeUnit.NANOSECONDS);
if (statics.getBooleanValue("is_exception") == true) {
pointBuilder.addField("exception", statics.getString("exception"));
} else {
... ... @@ -70,10 +63,10 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
}
Point point = pointBuilder.build();
batchPoints.point(point);
// batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB()
.write(batchPoints);
.write(InfluxDBContants.APP_ALARM, "default", point);
}
... ... @@ -87,9 +80,13 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
sql += " and mobj_id=" + mobj_id;
sql += " order by time desc limit 1";
Query query = new Query(sql, InfluxDBContants.APP_ALARM);
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
QueryResult result = null;
try {
result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
} catch (Exception e) {
log.warn("influxdb query error ", e);
return null;
}
JavaApiStaticsModel javaApiStaticsModel = new JavaApiStaticsModel();
javaApiStaticsModel.setServiceId(api_id);
javaApiStaticsModel.setMObjectId(mobj_id);
... ... @@ -137,6 +134,28 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
public List<JavaApiStaticsModel> selectlatestJavaApiStaticsList(String influxDBName, List<JavaApiStatusReq> paramList) {
List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<JavaApiStaticsModel>();
List<JavaApiStatusReq> splitReqList = new ArrayList<JavaApiStatusReq>();
for (JavaApiStatusReq param : paramList) {
splitReqList.add(param);
if (splitReqList.size() >= 20) {
javaApiStaticsModels.addAll(selectlatestJavaApiStaticsList_batch_split(influxDBName, splitReqList));
splitReqList.clear();
}
}
if (splitReqList.size() > 0) {
javaApiStaticsModels.addAll(selectlatestJavaApiStaticsList_batch_split(influxDBName, splitReqList));
}
return javaApiStaticsModels;
}
private List<JavaApiStaticsModel> selectlatestJavaApiStaticsList_batch_split(String influxDBName, List<JavaApiStatusReq> paramList) {
List<JavaApiStaticsModel> javaApiStaticsModels = new ArrayList<JavaApiStaticsModel>();
StringBuilder sb = new StringBuilder();
for (JavaApiStatusReq param : paramList) {
int api_id = param.getServiceId();
... ... @@ -146,9 +165,15 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
sb.append(" and mobj_id=" + mobj_id);
sb.append(" order by time desc limit 1;");
}
log.info(sb.toString());
Query query = new Query(sb.toString(), InfluxDBContants.APP_ALARM);
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
QueryResult result = null;
try {
result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
} catch (Exception e) {
log.warn("influx db select error ,", e);
return javaApiStaticsModels;
}
for (QueryResult.Result queryResult : result.getResults()) {
if (queryResult.getSeries() == null)
... ... @@ -187,11 +212,10 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
log.warn("batch select latest java api info failed ", e);
}
}
return javaApiStaticsModels;
}
@Override
public List<JavaApiStaticsModel> selectJavaApiStaticHis(String influxDBName, JavaApiHisReq req) {
int api_id = req.getApiId();
... ... @@ -225,9 +249,14 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
log.info("sql:{}", sql);
Query query = new Query(sql, InfluxDBContants.APP_ALARM);
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
QueryResult result = null;
List<JavaApiStaticsModel> list = new ArrayList<JavaApiStaticsModel>();
try {
result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
} catch (Exception e) {
log.warn("influxdb query error ", e);
return list;
}
QueryResult.Result rel = result.getResults().get(0);
... ... @@ -314,9 +343,13 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
Query query = new Query(sql, InfluxDBContants.APP_ALARM);
QueryResult result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
QueryResult result = null;
try {
result = inluxDBSingle.getInfluxDBByName(InfluxDBContants.ALARM).getInfluxDB().query(query);
} catch (Exception e) {
log.warn("influxdb query error ", e);
return 0;
}
return QueryResultUtil.getCount(result);
}
... ...
... ... @@ -181,7 +181,6 @@ public class JavaApiClient {
}
}
for (IJavaApiHadnler javaApiHadnler : javaApiHadnlerList) {
if (javaApiHadnler instanceof PreProcessJavaApiHandler)
continue;
... ...
... ... @@ -52,6 +52,7 @@ public class JavaAppInfoStaticsCtrl {
@RequestMapping("/query")
@ResponseBody
public BaseResponse getJavaApiStatus(@RequestBody JavaApiStatusReq[] req) {
log.info("get api status.");
BaseResponse rep = new BaseResponse();
try {
List<JavaApiStatusReq> list = Arrays.asList(req);
... ...
... ... @@ -2,7 +2,8 @@
influxdb.num=2
influxdb.name=test;alarm
influxdb.ip=http://192.168.102.162:8086;http://192.168.102.162:8086
influxdb.ip=http://192.168.102.162:8086;http://123.206.79.151:18086
#influxdb.ip=http://192.168.102.162:8086;http://192.168.99.100:32768
influxdb.user=yoho;root
influxdb.pwd=Yoho_9646;root
influxdb.connect.timeout=15;15
... ...