Authored by FengRuwei

influxdb 插入 javaapi 请求信息

... ... @@ -26,15 +26,25 @@ public class JavaApiStaticsMapper implements IJavaApiStaticsMapper {
.database(InfluxDBContants.APP_ALARM).retentionPolicy("default")
.build();
Point point = Point.measurement(InfluxDBContants.YOMO_TB_JAVAAPI)
.addField("java_api_info", statics.get("javaApiInfo").toString())
.addField("mobject_info", statics.get("mobject").toString())
.addField("start", statics.getLongValue("start"))
.addField("end", statics.getLongValue("end")).build();
.addField("api_id", statics.getIntValue("api_id"))
.addField("api_name", statics.getString("api_name"))
.addField("api_type", statics.getIntValue("api_type"))
.addField("api_url", statics.getString("api_url"))
.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", statics.getLongValue("end"))
.addField("end", statics.getLongValue("end"))
.addField("is_exception", statics.getBooleanValue("is_exception"))
.addField("exception", statics.getString("exception"))
.build();
batchPoints.point(point);
inluxDBSingle.getInfluxDBByName(InfluxDBContants.AWS).getInfluxDB()
.write(batchPoints);
}
}
... ...
... ... @@ -19,13 +19,13 @@ public class ScheduledPlan {
JavaApiClient javaApiClient;
@Scheduled(fixedRate=10000000)
// @Scheduled(fixedRate=10000000)
public void run() {
log.info("task start...");
javaApiClient.init4Test();
// javaApiClient.initClient();
// javaApiClient.initApiMonitor();
// javaApiClient.init4Test();
javaApiClient.initClient();
javaApiClient.initApiMonitor();
javaApiClient.run();
... ...
... ... @@ -36,12 +36,14 @@ public class JavaApiClient {
private IMObjectInfoService mObjectInfoService;
@Autowired
@Qualifier("javaapiRestTemplate")
private RestTemplate restTemplate;
@Autowired
@Qualifier("influxDBJavaApiHandler")
private IJavaApiHadnler javaApiHadnler;
//TODO 需要 线程安全 处理
private Map<String, JavaApiInfo> javaApimap;
private MultiValueMap<Integer, MObjectInfo> mObjInfoMap;
... ... @@ -54,6 +56,7 @@ public class JavaApiClient {
private long endTime;
public void initClient() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
... ... @@ -120,6 +123,9 @@ public class JavaApiClient {
}
/**
* 模拟数据
*/
public void init4Test() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
... ...
package com.monitor.javaserver.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
... ... @@ -33,7 +34,7 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
public JavaApiStatics call() throws Exception {
String url = createReqUrl();
log.info("url: {}", url);
// log.info("url: {}", url);
if (url == null)
return null;
JSONObject jsonRep = null;
... ... @@ -42,12 +43,14 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
apiStatics.setHasException(false);
apiStatics.setJavaApiInfo(this.javaApiInfo);
apiStatics.setMObjectInfo(this.mObjectInfo);
//TODO JSON解析异常
try {
if (javaApiInfo.getApiReqMethod() == 0) {
JSONObject req = JSON.parseObject(javaApiInfo.getApiData());
jsonRep = restTemplate.getForObject(url, JSONObject.class);
} else {
// JSONObject req = JSONObject.parse(javaApiInfo.getApiData(),JSONObject.class);
// jsonRep = restTemplate.postForObject(url, req, JSONObject.class);
JSONObject req = JSON.parseObject(javaApiInfo.getApiData());
jsonRep = restTemplate.postForObject(url, req, JSONObject.class);
}
} catch (Exception e) {
... ...
package com.monitor.javaserver.ctrl;
import com.monitor.javaserver.client.JavaApiClient;
import com.monitor.javaserver.status.IJavaApiStatusService;
import com.monitor.model.request.JavaApiStatusListReq;
import com.monitor.model.request.JavaApiStatusReq;
... ... @@ -48,7 +49,7 @@ public class JavaAppInfoCtrl {
public BaseResponse getJavaApiStatus(@RequestBody JavaApiStatusReq[] req) {
BaseResponse rep = new BaseResponse();
try {
List<JavaApiStatusReq> list= Arrays.asList(req);
List<JavaApiStatusReq> list = Arrays.asList(req);
List<JavaApiStatusRep> javaApiStatusRepList = javaApiStatusService.getJavaApiStatusList(list);
rep.setData(javaApiStatusRepList);
} catch (Exception e) {
... ... @@ -59,4 +60,19 @@ public class JavaAppInfoCtrl {
}
@Autowired
JavaApiClient client;
@RequestMapping("/tasktest")
@ResponseBody
public String getJavaApiStatus() {
long start = System.currentTimeMillis();
client.initClient();
client.initApiMonitor();
client.run();
long end = System.currentTimeMillis();
return "cost : " + (end - start) / 1000 + "s";
}
}
... ...
package com.monitor.javaserver.handle.impl;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
import com.monitor.influxdb.mapper.impl.JavaApiStaticsMapper;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.handle.IJavaApiHadnler;
... ... @@ -21,14 +23,33 @@ public class InfluxDBJavaApiHandler implements IJavaApiHadnler {
@Override
public void handler(JavaApiStatics javaApiStatics) {
log.info("handle result: {}", javaApiStatics);
// log.info("handle result: {}", javaApiStatics);
JSONObject jsonObject = new JSONObject();
jsonObject.put("javaApiInfo", javaApiStatics.getJavaApiInfo());
jsonObject.put("mobject", javaApiStatics.getMObjectInfo());
jsonObject.put("statrt", javaApiStatics.getStartTime());
JavaApiInfo javaApiInfo = javaApiStatics.getJavaApiInfo();
jsonObject.put("api_id", javaApiInfo.getServiceId());
jsonObject.put("api_name", javaApiInfo.getApiName());
jsonObject.put("api_type", javaApiInfo.getServiceType());
jsonObject.put("api_url", javaApiInfo.getApiUrl());
MObjectInfo mobjInfo = javaApiStatics.getMObjectInfo();
jsonObject.put("mobj_name", mobjInfo.getMoName());
jsonObject.put("mobj_ip", mobjInfo.getMoHostIp());
jsonObject.put("mobj_id", mobjInfo.getMoId());
if (mobjInfo.getMoTags() != null)
jsonObject.put("mobj_port", mobjInfo.getMoTags());
else
jsonObject.put("mobj_port", "");
jsonObject.put("start", javaApiStatics.getEndTime());
jsonObject.put("end", javaApiStatics.getEndTime());
jsonObject.put("is_exception", javaApiStatics.isHasException());
jsonObject.put("exception", javaApiStatics.getException());
javaApiStaticsMapper.insertJavaApiStatics(null, jsonObject);
... ...
... ... @@ -4,5 +4,37 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="javaapi_ConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
<property name="maxTotal" value="40" />
<property name="defaultMaxPerRoute" value="5" />
</bean>
<bean id="javaapi_httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
factory-method="create">
<property name="connectionManager" ref="javaapi_ConnectionManager" />
</bean>
<bean id="javaapi_httpClient" factory-bean="javaapi_httpClientBuilder"
factory-method="build" />
<!-- -->
<bean id="javaapi_clientHttpRequestFactory"
class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="javaapi_httpClient" />
<property name="connectTimeout" value="2000" />
<property name="readTimeout" value="2000" />
</bean>
<bean id="javaapiRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="javaapi_clientHttpRequestFactory" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" />
</list>
</property>
</bean>
</beans>
\ No newline at end of file
... ...