Authored by FengRuwei

influxdb 插入 javaapi 请求信息

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