Authored by FengRuwei

java api 服务信息dashboard

... ... @@ -62,7 +62,7 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
if (statics.getBooleanValue("is_exception") == true) {
pointBuilder.addField("exception", statics.getString("exception"));
} else {
pointBuilder.addField("response", statics.getString("response"));
// pointBuilder.addField("response", statics.getString("response"));
}
Point point = pointBuilder.build();
... ...
... ... @@ -38,6 +38,11 @@
</dependency>
<!--项目内部依赖-->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
... ...
package com.monitor.javaserver.client;
import com.model.HostInfo;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import com.monitor.cmdb.service.IHostInfoService;
import com.monitor.cmdb.service.IJavaApiInfoService;
import com.monitor.cmdb.service.IMObjectInfoService;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.common.JavaApiStatus;
import com.monitor.javaserver.common.JavaApiTask;
import com.monitor.javaserver.common.JavaApiThreadFactory;
import com.monitor.javaserver.handle.IJavaApiHadnler;
import com.monitor.model.request.HostInfoReq;
import com.monitor.model.response.PageResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
... ... @@ -17,6 +24,7 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
... ... @@ -36,6 +44,9 @@ public class JavaApiClient {
private IMObjectInfoService mObjectInfoService;
@Autowired
private IHostInfoService hostInfoService;
@Autowired
@Qualifier("javaapiRestTemplate")
private RestTemplate restTemplate;
//
... ... @@ -46,11 +57,14 @@ public class JavaApiClient {
@Autowired
private List<IJavaApiHadnler> javaApiHadnlerList;
@Autowired
JavaApiStatus javaApiStatus;
//TODO 需要 线程安全 处理 不过一般不会出现并发,任务周期比较长,一般周期内任务能够完成
private Map<String, JavaApiInfo> javaApimap;
private MultiValueMap<Integer, MObjectInfo> mObjInfoMap;
private MultiValueMap<Integer, MObjectDetails> mObjInfoMap;
private ExecutorService executorService;
... ... @@ -64,21 +78,48 @@ public class JavaApiClient {
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectDetails>();
//清理本次统计信息
javaApiStatus.clear();
}
public void initApiMonitor() {
//获取JAVA服务信息
List<JavaApiInfo> apiInfos = javaApiInfoService.queryJavaApiInfo();
for (JavaApiInfo javaApiInfo : apiInfos) {
javaApimap.put(javaApiInfo.getApiName(), javaApiInfo);
}
//获取云类型
HostInfoReq req = new HostInfoReq();
req.setPageSize(1000);
req.setCurrentPage(0);
PageResponse<HostInfo> hostInfos = hostInfoService.getHostInfos(req);
Map<String, Integer> ipCloudMap = new HashMap<String, Integer>();
if (hostInfos.getTotal() > 0) {
for (HostInfo hostInfo : hostInfos.getRows()) {
ipCloudMap.put(hostInfo.getHostIp().trim(), hostInfo.getCloudType());
}
}
//获取部署java服务的服务对象
//TODO 需要删选java服务
List<MObjectInfo> mObjectInfos = mObjectInfoService.queryMObjectsInfo();
for (MObjectInfo mObjInfo : mObjectInfos) {
mObjInfoMap.add(mObjInfo.getMoTypeId(), mObjInfo);
MObjectDetails mObjectDetails = new MObjectDetails();
BeanUtils.copyProperties(mObjInfo, mObjectDetails);
if (ipCloudMap.containsKey(mObjInfo.getMoHostIp())) {
mObjectDetails.setCloudType(ipCloudMap.get(mObjInfo.getMoHostIp().trim()));
} else {
mObjectDetails.setCloudType(0);
}
mObjInfoMap.add(mObjInfo.getMoTypeId(), mObjectDetails);
}
}
... ... @@ -89,12 +130,12 @@ public class JavaApiClient {
int tastNum = 0;
for (JavaApiInfo javaApiInfo : javaApimap.values()) {
List<MObjectInfo> mObjectInfos = mObjInfoMap.get(javaApiInfo.getServiceType());
List<MObjectDetails> mObjectInfos = mObjInfoMap.get(javaApiInfo.getServiceType());
if (mObjectInfos == null) {
log.warn("type {} has no service object", javaApiInfo.getServiceType());
continue;
}
for (MObjectInfo mObjectInfo : mObjectInfos) {
for (MObjectDetails mObjectInfo : mObjectInfos) {
completionService.submit(new JavaApiTask(restTemplate, javaApiInfo, mObjectInfo));
tastNum++;
}
... ... @@ -135,9 +176,9 @@ public class JavaApiClient {
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectDetails>();
MObjectInfo mObj1 = new MObjectInfo();
MObjectDetails mObj1 = new MObjectDetails();
mObj1.setMoId(1);
mObj1.setMoHostIp("192.168.102.205");
mObj1.setMoTags("8080");
... ... @@ -155,7 +196,7 @@ public class JavaApiClient {
//
// mObjInfoMap.add(1, mObj2);
MObjectInfo mObj2 = new MObjectInfo();
MObjectDetails mObj2 = new MObjectDetails();
mObj2.setMoId(0);
mObj2.setMoHostIp("localhost");
mObj2.setMoTags("10080");
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.javaserver.common;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import lombok.Data;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
... ... @@ -15,7 +16,7 @@ public class JavaApiStatics{
private JavaApiInfo javaApiInfo;
private MObjectInfo mObjectInfo;
private MObjectDetails mObjectDetails;
private long startTime;
... ...
package com.monitor.javaserver.common;
import com.monitor.model.response.JavaApiStaticsRep;
import com.monitor.model.response.JavaApiStatusRep;
import org.apache.commons.collections.map.MultiKeyMap;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
/**
* 记录当前java服务概况
* Created by fruwei on 2016/6/27.
*/
@Component
public class JavaApiStatus {
/**
* key: app_id
* val: error_num
*/
private ConcurrentHashMap<String, AtomicInteger> mapStaticsErr = new ConcurrentHashMap<String, AtomicInteger>();
private ConcurrentHashMap<String, AtomicInteger> mapStaticsOk = new ConcurrentHashMap<String, AtomicInteger>();
private ConcurrentHashMap<String, Long> mapUpdateTime = new ConcurrentHashMap<String, Long>();
public void addError(int apiType, int cloud) {
String key = cloud + "_" + apiType;
mapStaticsErr.putIfAbsent(key, new AtomicInteger(0));
mapStaticsErr.get(key).getAndIncrement();
}
public void addSuccess(int apiType, int cloud) {
String key = cloud + "_" + apiType;
mapStaticsOk.putIfAbsent(key, new AtomicInteger(0));
mapStaticsOk.get(key).getAndIncrement();
}
public void clear() {
mapStaticsErr.clear();
mapStaticsOk.clear();
}
public List<JavaApiStaticsRep> getStatusList() {
List<JavaApiStaticsRep> rel = new ArrayList<JavaApiStaticsRep>();
MultiKeyMap multiKeyMap = new MultiKeyMap();
Enumeration<String> errkeys = mapStaticsErr.keys();
while (errkeys.hasMoreElements()) {
String key = errkeys.nextElement();
JavaApiStaticsRep statics = new JavaApiStaticsRep();
String[] strKeys = key.split("_");
statics.setCloudType(Integer.parseInt(strKeys[0]));
statics.setServiceType(Integer.parseInt(strKeys[1]));
statics.setErrNum(mapStaticsErr.getOrDefault(key, new AtomicInteger(0)).intValue());
multiKeyMap.put(statics.getServiceType(), statics.getServiceType(), statics);
rel.add(statics);
}
Enumeration<String> okeys = mapStaticsOk.keys();
while (okeys.hasMoreElements()) {
String key = okeys.nextElement();
JavaApiStaticsRep statics = new JavaApiStaticsRep();
String[] strKeys = key.split("_");
statics.setCloudType(Integer.parseInt(strKeys[0]));
statics.setServiceType(Integer.parseInt(strKeys[1]));
if (multiKeyMap.containsKey(statics.getCloudType(), statics.getServiceType())) {
statics = (JavaApiStaticsRep) multiKeyMap.get(statics.getCloudType(), statics.getServiceType());
} else {
multiKeyMap.put(statics.getServiceType(), statics.getServiceType(), statics);
}
statics.setOkNum(mapStaticsOk.getOrDefault(key, new AtomicInteger(0)).intValue());
rel.add(statics);
}
return rel;
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.monitor.javaserver.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -18,12 +19,12 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
Logger log = LoggerFactory.getLogger(JavaApiTask.class);
private JavaApiInfo javaApiInfo;
private MObjectInfo mObjectInfo;
private MObjectDetails mObjectInfo;
private RestTemplate restTemplate;
public JavaApiTask(RestTemplate restTemplate, JavaApiInfo javaApiInfo, MObjectInfo mObjectInfo) {
public JavaApiTask(RestTemplate restTemplate, JavaApiInfo javaApiInfo, MObjectDetails mObjectInfo) {
this.javaApiInfo = javaApiInfo;
this.mObjectInfo = mObjectInfo;
this.restTemplate = restTemplate;
... ... @@ -42,7 +43,7 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
apiStatics.setStartTime(System.currentTimeMillis());
apiStatics.setHasException(false);
apiStatics.setJavaApiInfo(this.javaApiInfo);
apiStatics.setMObjectInfo(this.mObjectInfo);
apiStatics.setMObjectDetails(this.mObjectInfo);
//TODO JSON解析异常
try {
if (javaApiInfo.getApiReqMethod() == 0) {
... ...
... ... @@ -6,6 +6,7 @@ import com.monitor.model.page.PageResponse;
import com.monitor.model.request.JavaApiHisReq;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.BaseResponse;
import com.monitor.model.response.JavaApiStaticsRep;
import com.monitor.model.response.JavaApiStatusRep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -105,7 +106,7 @@ public class JavaAppInfoStaticsCtrl {
}
if (req.getEndTime() == 0)
req.setEndTime( System.currentTimeMillis());
req.setEndTime(System.currentTimeMillis());
BaseResponse baseResponse = new BaseResponse();
... ... @@ -130,4 +131,23 @@ public class JavaAppInfoStaticsCtrl {
}
@RequestMapping("/queryAll")
@ResponseBody
public BaseResponse getAllJavaApiStatus() {
BaseResponse baseResponse = new BaseResponse();
try {
List<JavaApiStaticsRep> staticsReps = javaApiStatusService.getAllJavaApiStatus();
baseResponse.setData(staticsReps);
} catch (Exception e) {
log.warn("query his failed req", e);
baseResponse.setCode(400);
baseResponse.setMessage(e.getMessage());
}
return baseResponse;
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.javaserver.handle.impl;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.common.JavaApiStatics;
... ... @@ -34,7 +35,7 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler {
jsonObject.put("api_type", javaApiInfo.getServiceType());
jsonObject.put("api_url", javaApiInfo.getApiUrl());
MObjectInfo mobjInfo = javaApiStatics.getMObjectInfo();
MObjectDetails mobjInfo = javaApiStatics.getMObjectDetails();
jsonObject.put("mobj_name", mobjInfo.getMoName());
jsonObject.put("mobj_ip", mobjInfo.getMoHostIp());
... ... @@ -52,8 +53,8 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler {
jsonObject.put("exception", ExceptionUtils.getStackTrace(javaApiStatics.getException()));
} else {
JSONObject rep = javaApiStatics.getResponse();
jsonObject.put("response", javaApiStatics.getResponse());
// JSONObject rep = javaApiStatics.getResponse();
// jsonObject.put("response", javaApiStatics.getResponse());
}
javaApiStaticsMapper.insertJavaApiStatics(null, jsonObject);
... ...
package com.monitor.javaserver.handle.impl;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import com.monitor.common.config.SnsMobileConfig;
import com.monitor.common.service.AlarmMsgService;
... ... @@ -26,7 +27,9 @@ public class MsgJavaApiHandler implements IJavaApiHadnler {
public AlarmMsgService alarmMsgService;
@Autowired
private SnsMobileConfig snsMobileConfig;
/**
* 应该不会出现并发情况
*/
private ConcurrentHashMap<String, AtomicInteger> mapStatics = new ConcurrentHashMap<String, AtomicInteger>();
... ... @@ -42,13 +45,8 @@ public class MsgJavaApiHandler implements IJavaApiHadnler {
}
int num = 0;
if (mapStatics.containsKey(key)) {
AtomicInteger errNums = mapStatics.get(key);
num = errNums.incrementAndGet();
} else {
mapStatics.put(key, new AtomicInteger(1));
num = 1;
}
mapStatics.putIfAbsent(key, new AtomicInteger(0));
num = mapStatics.get(key).incrementAndGet();
JavaApiInfo javaApiInfo = javaApiStatics.getJavaApiInfo();
if (javaApiInfo.getApiWarnTrigger() > 0 &&
... ... @@ -65,14 +63,14 @@ public class MsgJavaApiHandler implements IJavaApiHadnler {
public String buildKey(JavaApiStatics javaApiStatics) {
String key;
key = javaApiStatics.getJavaApiInfo().getServiceId() + "_" + javaApiStatics.getMObjectInfo().getMoId();
key = javaApiStatics.getJavaApiInfo().getServiceId() + "_" + javaApiStatics.getMObjectDetails().getMoId();
return key;
}
public String buildErrMsg(int num, JavaApiStatics javaApiStatics) {
JavaApiInfo javaApiInfo = javaApiStatics.getJavaApiInfo();
MObjectInfo mObjectInfo = javaApiStatics.getMObjectInfo();
MObjectDetails mObjectInfo = javaApiStatics.getMObjectDetails();
StringBuilder msgBuilder = new StringBuilder();
msgBuilder.append("Java API Error ")
... ...
package com.monitor.javaserver.handle.impl;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.common.JavaApiStatus;
import com.monitor.javaserver.handle.IJavaApiHadnler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 统计信息处理
* Created by fruwei on 2016/6/27.
*/
@Component
public class StaticsJavaApiHandler implements IJavaApiHadnler {
@Autowired
JavaApiStatus javaApiStatus;
@Override
public void handler(JavaApiStatics javaApiStatics) {
if(javaApiStatics.getJavaApiInfo().getApiToggle()==0){
return;
}
if (javaApiStatics.isHasException()) {
javaApiStatus.addError(javaApiStatics.getJavaApiInfo().getServiceType(), javaApiStatics.getMObjectDetails().getCloudType());
} else {
javaApiStatus.addSuccess(javaApiStatics.getJavaApiInfo().getServiceType(), javaApiStatics.getMObjectDetails().getCloudType());
}
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.monitor.javaserver.service;
import com.monitor.model.request.JavaApiHisReq;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.JavaApiStaticsRep;
import com.monitor.model.response.JavaApiStatusRep;
import java.util.List;
... ... @@ -22,5 +23,7 @@ public interface IJavaApiStatusService {
public int countJavaApiStatusHisByTime(JavaApiHisReq req);
public List<JavaApiStaticsRep> getAllJavaApiStatus();
}
... ...
... ... @@ -3,10 +3,12 @@ package com.monitor.javaserver.service.impl;
import com.monitor.common.contants.PaginationContants;
import com.monitor.common.util.DateFormatUtil;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.common.JavaApiStatus;
import com.monitor.javaserver.service.IJavaApiStatusService;
import com.monitor.model.domain.JavaApiStaticsModel;
import com.monitor.model.request.JavaApiHisReq;
import com.monitor.model.request.JavaApiStatusReq;
import com.monitor.model.response.JavaApiStaticsRep;
import com.monitor.model.response.JavaApiStatusRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -24,6 +26,10 @@ public class JavaAppiStatusServiceImpl implements IJavaApiStatusService {
@Autowired
JavaApiStaticsMapper javaApiStaticsMapper;
@Autowired
JavaApiStatus javaApiStatus;
@Override
public JavaApiStatusRep getJavaApiStatus(JavaApiStatusReq req) {
JavaApiStatusRep rep = new JavaApiStatusRep();
... ... @@ -82,7 +88,7 @@ public class JavaAppiStatusServiceImpl implements IJavaApiStatusService {
long start = System.currentTimeMillis();
long end = start;
start = start - 1000 * 60 * 60; //一小时之内
JavaApiHisReq req=new JavaApiHisReq();
JavaApiHisReq req = new JavaApiHisReq();
req.setPageSize(10);
req.setApiId(api_id);
req.setMobjId(mobj_id);
... ... @@ -131,5 +137,12 @@ public class JavaAppiStatusServiceImpl implements IJavaApiStatusService {
return count;
}
@Override
public List<JavaApiStaticsRep> getAllJavaApiStatus() {
return javaApiStatus.getStatusList();
}
}
... ...
package com.monitor.model.response;
import lombok.Data;
/**
* Created by fruwei on 2016/6/27.
*/
@Data
public class JavaApiStaticsRep {
private int serviceType;
private int cloudType;
private int errNum;
private int okNum;
private String updateTime;
}
... ...
package com.model;
import lombok.Data;
import java.io.Serializable;
/**
* Created by yoho on 2016/6/14.
*/
@Data
public class MObjectDetails implements Serializable {
private int moId = 0;
private String moName;
private String moHostIp;
private int moTypeId;
private String moTags;
private String moUrl;
private int cloudType;
}
... ...
... ... @@ -50,5 +50,4 @@
ORDER BY id asc
</select>
</mapper>
\ No newline at end of file
... ...