Authored by FengRuwei

java服务监控,服务请求执行&请求结果获取

Showing 20 changed files with 521 additions and 28 deletions
... ... @@ -15,10 +15,9 @@ public interface IJavaApiInfoService {
public List<JavaApiInfo> queryJavaApiInfoByType(int type);
public BaseResponse<Integer> addJavaApiInfo(JavaApiInfo javaApiInfo);
public BaseResponse<Integer> saveJavaApiInfo(JavaApiInfo javaApiInfo);
public BaseResponse<Integer> delJavaApiInfo(int id);
public BaseResponse<Integer> updateJavaApiInfo(JavaApiInfo javaApiInfo);
}
... ...
... ... @@ -3,16 +3,23 @@ package com.monitor.cmdb.service.impl;
import com.model.JavaApiInfo;
import com.monitor.cmdb.service.IJavaApiInfoService;
import com.monitor.model.response.BaseResponse;
import com.monitor.mysql.mapper.JavaApiInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by fruwei on 2016/6/17.
*/
@Service
public class JavaApiInfoService implements IJavaApiInfoService {
@Autowired
private JavaApiInfoMapper javaApiInfoMapper;
@Override
public List<JavaApiInfo> queryJavaApiInfo() {
return null;
return javaApiInfoMapper.selectAllApi();
}
@Override
... ... @@ -21,17 +28,20 @@ public class JavaApiInfoService implements IJavaApiInfoService {
}
@Override
public BaseResponse<Integer> addJavaApiInfo(JavaApiInfo javaApiInfo) {
return null;
public BaseResponse<Integer> saveJavaApiInfo(JavaApiInfo javaApiInfo) {
int result=0;
if(javaApiInfo.getServiceId()!=null&&javaApiInfo.getServiceId()>0){
result=javaApiInfoMapper.updateByPrimaryKey(javaApiInfo);
}else{
result=javaApiInfoMapper.insert(javaApiInfo);
}
return new BaseResponse<Integer>(result);
}
@Override
public BaseResponse<Integer> delJavaApiInfo(int id) {
return null;
int result=javaApiInfoMapper.deleteByPrimaryKey(id);
return new BaseResponse<Integer>(result);
}
@Override
public BaseResponse<Integer> updateJavaApiInfo(JavaApiInfo javaApiInfo) {
return null;
}
}
... ...
system.envi=product
\ No newline at end of file
... ...
package com.monitor.javaserver;
import com.monitor.javaserver.client.JavaApiClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by fruwei on 2016/6/19.
*/
@Component
public class ScheduledPlan {
Logger log = LoggerFactory.getLogger(ScheduledPlan.class);
@Autowired
JavaApiClient javaApiClient;
@Scheduled(fixedRate=10000)
public void run() {
log.info("task start...");
javaApiClient.init4Test();
// javaApiClient.initClient();
// javaApiClient.initApiMonitor();
javaApiClient.run();
log.info("task end...");
}
}
... ...
package com.monitor.javaserver;
/**
* Created by zhengyouwei on 2016/6/13.
*/
public class Test {
}
package com.monitor.javaserver.client;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
import com.monitor.cmdb.service.IJavaApiInfoService;
import com.monitor.cmdb.service.IMObjectInfoService;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.common.JavaApiTask;
import com.monitor.javaserver.common.JavaApiThreadFactory;
import com.monitor.javaserver.handle.IJavaApiHadnler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
/**
* Created by fruwei on 2016/6/19.
*/
@Component
public class JavaApiClient {
Logger log = LoggerFactory.getLogger(JavaApiTask.class);
@Autowired
private IJavaApiInfoService javaApiInfoService;
@Autowired
private IMObjectInfoService mObjectInfoService;
@Autowired
private RestTemplate restTemplate;
@Autowired
@Qualifier("influxDBJavaApiHandler")
private IJavaApiHadnler javaApiHadnler;
private Map<String, JavaApiInfo> javaApimap;
private MultiValueMap<Integer, MObjectInfo> mObjInfoMap;
private ExecutorService executorService;
private CompletionService<JavaApiStatics> completionService;
private long startTime;
private long endTime;
public void initClient() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
}
public void initApiMonitor() {
List<JavaApiInfo> apiInfos = javaApiInfoService.queryJavaApiInfo();
for (JavaApiInfo javaApiInfo : apiInfos) {
javaApimap.put(javaApiInfo.getApiName(), javaApiInfo);
}
//TODO 需要删选java服务
List<MObjectInfo> mObjectInfos = mObjectInfoService.queryMObjectsInfo();
for (MObjectInfo mObjInfo : mObjectInfos) {
mObjInfoMap.add(mObjInfo.getMoTypeId(), mObjInfo);
}
}
public void run() {
startTime=System.currentTimeMillis();
int tastNum = 0;
for (JavaApiInfo javaApiInfo : javaApimap.values()) {
List<MObjectInfo> mObjectInfos = mObjInfoMap.get(javaApiInfo.getServiceType());
if (mObjectInfos == null) {
log.warn("type {} has no service object", javaApiInfo.getServiceType());
continue;
}
for (MObjectInfo mObjectInfo : mObjectInfos) {
completionService.submit(new JavaApiTask(restTemplate, javaApiInfo, mObjectInfo));
tastNum++;
}
}
//wait for all rep end...
for (int i = 0; i < tastNum; i++) {
try {
Future<JavaApiStatics> rep = completionService.take();
handleResult(rep.get());
} catch (InterruptedException e) {
log.warn("completionService get rep interrupted ", e);
} catch (ExecutionException e) {
log.warn("completionService get rep failed ", e);
}
}
//close 线程池
executorService.shutdownNow();
endTime=System.currentTimeMillis();
}
public void handleResult(JavaApiStatics rep) {
javaApiHadnler.handler(rep);
}
public void init4Test() {
executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
new JavaApiThreadFactory());
completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
MObjectInfo mObj1 = new MObjectInfo();
mObj1.setMoId(1);
mObj1.setMoHostIp("192.168.102.205");
mObj1.setMoTags("8080");
mObj1.setMoName("gateway");
mObj1.setMoTypeId(1);
mObjInfoMap.add(1, mObj1);
// MObjectInfo mObj2 = new MObjectInfo();
// mObj2.setMoId(1);
// mObj2.setMoHostIp("192.168.102.207");
// mObj2.setMoTags("8080");
// mObj2.setMoName("gateway");
// mObj2.setMoTypeId(1);
//
// mObjInfoMap.add(1, mObj2);
int uid = 8041886;
for (int i = 0; i < 10; i++) {
JavaApiInfo javaApiInfo = new JavaApiInfo();
javaApiInfo.setApiName("order.get_" + i);
javaApiInfo.setApiReqMethod(0);
javaApiInfo.setApiToggle(0);
javaApiInfo.setApiUrl("/gateway/?debug=XYZ&method=app.Shopping.listCoupon&uid=" + (uid++));
javaApiInfo.setServiceType(1);
javaApimap.put("order.get_" + i, javaApiInfo);
}
}
}
... ...
package com.monitor.javaserver.common;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
import lombok.Data;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
/**
* 调用统计信息
* Created by fruwei on 2016/6/19.
*/
@Data
public class JavaApiStatics{
private JavaApiInfo javaApiInfo;
private MObjectInfo mObjectInfo;
private long startTime;
private long endTime;
private JSONObject response;
private boolean hasException;
private Exception exception;
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
... ...
package com.monitor.javaserver.common;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
import java.util.concurrent.Callable;
/**
* Created by fruwei on 2016/6/19.
*/
public class JavaApiTask implements Callable<JavaApiStatics> {
Logger log = LoggerFactory.getLogger(JavaApiTask.class);
private JavaApiInfo javaApiInfo;
private MObjectInfo mObjectInfo;
private RestTemplate restTemplate;
public JavaApiTask(RestTemplate restTemplate, JavaApiInfo javaApiInfo, MObjectInfo mObjectInfo) {
this.javaApiInfo = javaApiInfo;
this.mObjectInfo = mObjectInfo;
this.restTemplate = restTemplate;
}
@Override
public JavaApiStatics call() throws Exception {
String url = createReqUrl();
log.info("url: {}", url);
if (url == null)
return null;
JSONObject jsonRep = null;
JavaApiStatics apiStatics = new JavaApiStatics();
apiStatics.setStartTime(System.currentTimeMillis());
apiStatics.setHasException(false);
apiStatics.setJavaApiInfo(this.javaApiInfo);
apiStatics.setMObjectInfo(this.mObjectInfo);
try {
if (javaApiInfo.getApiReqMethod() == 0) {
jsonRep = restTemplate.getForObject(url, JSONObject.class);
} else {
JSONObject req = null;
jsonRep = restTemplate.postForObject(url, req, JSONObject.class);
}
} catch (Exception e) {
log.warn("exception {} ",url);
apiStatics.setHasException(true);
apiStatics.setException(e);
}finally {
apiStatics.setEndTime(System.currentTimeMillis());
apiStatics.setResponse(jsonRep);
}
return apiStatics;
}
private void createReq() {
}
private String createReqUrl() {
if (mObjectInfo.getMoHostIp() == null || mObjectInfo.getMoHostIp() == null || javaApiInfo.getApiUrl() == null) {
log.warn("get url failed has null prop. ");
return null;
}
String url = "http://" + mObjectInfo.getMoHostIp() + ":" + mObjectInfo.getMoTags() + "/" + javaApiInfo.getApiUrl();
return url;
}
}
... ...
package com.monitor.javaserver.common;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by fruwei on 2016/6/20.
*/
public class JavaApiThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public JavaApiThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-javaApi" +
poolNumber.getAndIncrement() +
"-thread-";
}
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
... ...
package com.monitor.javaserver.handle;
import com.monitor.javaserver.common.JavaApiStatics;
/**
* java服务请求结果处理接口
* Created by fruwei on 2016/6/20.
*/
public interface IJavaApiHadnler {
public void handler(JavaApiStatics javaApiStatics) ;
}
... ...
package com.monitor.javaserver.handle.impl;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.handle.IJavaApiHadnler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Created by fruwei on 2016/6/20.
*/
@Component("influxDBJavaApiHandler")
public class InfluxDBJavaApiHandler implements IJavaApiHadnler{
Logger log= LoggerFactory.getLogger(InfluxDBJavaApiHandler.class);
@Override
public void handler(JavaApiStatics javaApiStatics) {
log.info("handle result: {}",javaApiStatics);
//TODO handle
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
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">
</beans>
\ No newline at end of file
... ...
package com.model;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
public class JavaApiInfo {
private Integer serviceId;
... ... @@ -13,6 +15,8 @@ public class JavaApiInfo {
private Integer apiToggle;
private Integer apiReqMethod;
public Integer getServiceId() {
return serviceId;
}
... ... @@ -60,4 +64,17 @@ public class JavaApiInfo {
public void setApiToggle(Integer apiToggle) {
this.apiToggle = apiToggle;
}
public Integer getApiReqMethod() {
return apiReqMethod;
}
public void setApiReqMethod(Integer apiReqMethod) {
this.apiReqMethod = apiReqMethod;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
\ No newline at end of file
... ...
... ... @@ -3,6 +3,8 @@ package com.monitor.mysql.mapper;
import com.model.JavaApiInfo;
import java.util.List;
public interface JavaApiInfoMapper {
int deleteByPrimaryKey(Integer serviceId);
... ... @@ -15,4 +17,6 @@ public interface JavaApiInfoMapper {
int updateByPrimaryKeySelective(JavaApiInfo record);
int updateByPrimaryKey(JavaApiInfo record);
List<JavaApiInfo> selectAllApi();
}
\ No newline at end of file
... ...
... ... @@ -8,9 +8,10 @@
<result column="api_url" property="apiUrl" jdbcType="VARCHAR" />
<result column="api_data" property="apiData" jdbcType="VARCHAR" />
<result column="api_toggle" property="apiToggle" jdbcType="INTEGER" />
<result column="api_req_method" property="apiReqMethod" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
service_id, service_type, api_name, api_url, api_data, api_toggle
service_id, service_type, api_name, api_url, api_data, api_toggle, api_req_method
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
... ... @@ -24,11 +25,11 @@
</delete>
<insert id="insert" parameterType="com.model.JavaApiInfo" >
insert into java_api_info (service_id, service_type, api_name,
api_url, api_data, api_toggle
)
api_url, api_data, api_toggle,
api_req_method)
values (#{serviceId,jdbcType=INTEGER}, #{serviceType,jdbcType=INTEGER}, #{apiName,jdbcType=VARCHAR},
#{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER}
)
#{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER},
#{apiReqMethod,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.model.JavaApiInfo" >
insert into java_api_info
... ... @@ -51,6 +52,9 @@
<if test="apiToggle != null" >
api_toggle,
</if>
<if test="apiReqMethod != null" >
api_req_method,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="serviceId != null" >
... ... @@ -71,6 +75,9 @@
<if test="apiToggle != null" >
#{apiToggle,jdbcType=INTEGER},
</if>
<if test="apiReqMethod != null" >
#{apiReqMethod,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.model.JavaApiInfo" >
... ... @@ -91,6 +98,9 @@
<if test="apiToggle != null" >
api_toggle = #{apiToggle,jdbcType=INTEGER},
</if>
<if test="apiReqMethod != null" >
api_req_method = #{apiReqMethod,jdbcType=INTEGER},
</if>
</set>
where service_id = #{serviceId,jdbcType=INTEGER}
</update>
... ... @@ -100,7 +110,16 @@
api_name = #{apiName,jdbcType=VARCHAR},
api_url = #{apiUrl,jdbcType=VARCHAR},
api_data = #{apiData,jdbcType=VARCHAR},
api_toggle = #{apiToggle,jdbcType=INTEGER}
api_toggle = #{apiToggle,jdbcType=INTEGER},
api_req_method = #{apiReqMethod,jdbcType=INTEGER}
where service_id = #{serviceId,jdbcType=INTEGER}
</update>
<select id="selectAllApi" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from java_api_info
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -51,9 +51,14 @@
<artifactId>monitor-service-switch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
... ...
#---------jdbc config----------
local.jdbc.url=jdbc:mysql://172.16.6.234:3306/yh_ops?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
local.jdbc.url=jdbc:mysql://localhost:3306/ops?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
local.jdbc.user=root
local.jdbc.password=123456
local.jdbc.password=root
#---------jdbc config----------
\ No newline at end of file
... ...
package com.monitor;
import com.model.JavaApiInfo;
import com.monitor.cmdb.service.IJavaApiInfoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/**
* Created by fruwei on 2016/6/17.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath*:META-INF/spring/spring*.xml"})
@PropertySource({"classpath:jdbc.properties",
"classpath:*.properties"})
public class JavaApiDBTest {
Logger logger = LoggerFactory.getLogger("test");
@Autowired
IJavaApiInfoService javaApiInfoService;
@Test
public void testAdd() {
JavaApiInfo javaApiInfo = new JavaApiInfo();
javaApiInfo.setApiName("test");
javaApiInfo.setServiceType(1);
javaApiInfo.setApiUrl("1111111");
javaApiInfoService.saveJavaApiInfo(javaApiInfo);
}
@Test
public void testQuery() {
List<JavaApiInfo> list = javaApiInfoService.queryJavaApiInfo();
for (JavaApiInfo javaApiInfo : list) {
logger.info(javaApiInfo.getApiName());
}
}
@Test
public void testDel() {
}
@Test
public void testUpdate() {
javaApiInfoService.delJavaApiInfo(1);
}
}
... ...