Authored by FengRuwei

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

Showing 20 changed files with 521 additions and 28 deletions
@@ -15,10 +15,9 @@ public interface IJavaApiInfoService { @@ -15,10 +15,9 @@ public interface IJavaApiInfoService {
15 15
16 public List<JavaApiInfo> queryJavaApiInfoByType(int type); 16 public List<JavaApiInfo> queryJavaApiInfoByType(int type);
17 17
18 - public BaseResponse<Integer> addJavaApiInfo(JavaApiInfo javaApiInfo); 18 + public BaseResponse<Integer> saveJavaApiInfo(JavaApiInfo javaApiInfo);
19 19
20 public BaseResponse<Integer> delJavaApiInfo(int id); 20 public BaseResponse<Integer> delJavaApiInfo(int id);
21 21
22 - public BaseResponse<Integer> updateJavaApiInfo(JavaApiInfo javaApiInfo);  
23 22
24 } 23 }
@@ -3,16 +3,23 @@ package com.monitor.cmdb.service.impl; @@ -3,16 +3,23 @@ package com.monitor.cmdb.service.impl;
3 import com.model.JavaApiInfo; 3 import com.model.JavaApiInfo;
4 import com.monitor.cmdb.service.IJavaApiInfoService; 4 import com.monitor.cmdb.service.IJavaApiInfoService;
5 import com.monitor.model.response.BaseResponse; 5 import com.monitor.model.response.BaseResponse;
  6 +import com.monitor.mysql.mapper.JavaApiInfoMapper;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Component;
  9 +import org.springframework.stereotype.Service;
6 10
7 import java.util.List; 11 import java.util.List;
8 12
9 /** 13 /**
10 * Created by fruwei on 2016/6/17. 14 * Created by fruwei on 2016/6/17.
11 */ 15 */
  16 +@Service
12 public class JavaApiInfoService implements IJavaApiInfoService { 17 public class JavaApiInfoService implements IJavaApiInfoService {
  18 + @Autowired
  19 + private JavaApiInfoMapper javaApiInfoMapper;
13 @Override 20 @Override
14 public List<JavaApiInfo> queryJavaApiInfo() { 21 public List<JavaApiInfo> queryJavaApiInfo() {
15 - return null; 22 + return javaApiInfoMapper.selectAllApi();
16 } 23 }
17 24
18 @Override 25 @Override
@@ -21,17 +28,20 @@ public class JavaApiInfoService implements IJavaApiInfoService { @@ -21,17 +28,20 @@ public class JavaApiInfoService implements IJavaApiInfoService {
21 } 28 }
22 29
23 @Override 30 @Override
24 - public BaseResponse<Integer> addJavaApiInfo(JavaApiInfo javaApiInfo) {  
25 - return null; 31 + public BaseResponse<Integer> saveJavaApiInfo(JavaApiInfo javaApiInfo) {
  32 + int result=0;
  33 + if(javaApiInfo.getServiceId()!=null&&javaApiInfo.getServiceId()>0){
  34 + result=javaApiInfoMapper.updateByPrimaryKey(javaApiInfo);
  35 + }else{
  36 + result=javaApiInfoMapper.insert(javaApiInfo);
  37 + }
  38 + return new BaseResponse<Integer>(result);
26 } 39 }
27 40
28 @Override 41 @Override
29 public BaseResponse<Integer> delJavaApiInfo(int id) { 42 public BaseResponse<Integer> delJavaApiInfo(int id) {
30 - return null; 43 + int result=javaApiInfoMapper.deleteByPrimaryKey(id);
  44 + return new BaseResponse<Integer>(result);
31 } 45 }
32 46
33 - @Override  
34 - public BaseResponse<Integer> updateJavaApiInfo(JavaApiInfo javaApiInfo) {  
35 - return null;  
36 - }  
37 } 47 }
  1 +package com.monitor.javaserver;
  2 +
  3 +import com.monitor.javaserver.client.JavaApiClient;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.scheduling.annotation.Scheduled;
  8 +import org.springframework.stereotype.Component;
  9 +
  10 +/**
  11 + * Created by fruwei on 2016/6/19.
  12 + */
  13 +@Component
  14 +public class ScheduledPlan {
  15 + Logger log = LoggerFactory.getLogger(ScheduledPlan.class);
  16 +
  17 +
  18 + @Autowired
  19 + JavaApiClient javaApiClient;
  20 +
  21 +
  22 + @Scheduled(fixedRate=10000)
  23 + public void run() {
  24 + log.info("task start...");
  25 +
  26 + javaApiClient.init4Test();
  27 +// javaApiClient.initClient();
  28 +// javaApiClient.initApiMonitor();
  29 + javaApiClient.run();
  30 +
  31 +
  32 + log.info("task end...");
  33 +
  34 + }
  35 +}
1 -package com.monitor.javaserver;  
2 -  
3 -/**  
4 - * Created by zhengyouwei on 2016/6/13.  
5 - */  
6 -public class Test {  
7 -}  
  1 +package com.monitor.javaserver.client;
  2 +
  3 +import com.model.JavaApiInfo;
  4 +import com.model.MObjectInfo;
  5 +import com.monitor.cmdb.service.IJavaApiInfoService;
  6 +import com.monitor.cmdb.service.IMObjectInfoService;
  7 +import com.monitor.javaserver.common.JavaApiStatics;
  8 +import com.monitor.javaserver.common.JavaApiTask;
  9 +import com.monitor.javaserver.common.JavaApiThreadFactory;
  10 +import com.monitor.javaserver.handle.IJavaApiHadnler;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +import org.springframework.beans.factory.annotation.Autowired;
  14 +import org.springframework.beans.factory.annotation.Qualifier;
  15 +import org.springframework.stereotype.Component;
  16 +import org.springframework.util.LinkedMultiValueMap;
  17 +import org.springframework.util.MultiValueMap;
  18 +import org.springframework.web.client.RestTemplate;
  19 +
  20 +import java.util.List;
  21 +import java.util.Map;
  22 +import java.util.concurrent.*;
  23 +
  24 +/**
  25 + * Created by fruwei on 2016/6/19.
  26 + */
  27 +@Component
  28 +public class JavaApiClient {
  29 + Logger log = LoggerFactory.getLogger(JavaApiTask.class);
  30 +
  31 +
  32 + @Autowired
  33 + private IJavaApiInfoService javaApiInfoService;
  34 +
  35 + @Autowired
  36 + private IMObjectInfoService mObjectInfoService;
  37 +
  38 + @Autowired
  39 + private RestTemplate restTemplate;
  40 +
  41 + @Autowired
  42 + @Qualifier("influxDBJavaApiHandler")
  43 + private IJavaApiHadnler javaApiHadnler;
  44 +
  45 + private Map<String, JavaApiInfo> javaApimap;
  46 +
  47 + private MultiValueMap<Integer, MObjectInfo> mObjInfoMap;
  48 +
  49 + private ExecutorService executorService;
  50 +
  51 + private CompletionService<JavaApiStatics> completionService;
  52 +
  53 + private long startTime;
  54 + private long endTime;
  55 +
  56 +
  57 + public void initClient() {
  58 + executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
  59 + new JavaApiThreadFactory());
  60 +
  61 + completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
  62 +
  63 + javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
  64 + mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
  65 + }
  66 +
  67 + public void initApiMonitor() {
  68 + List<JavaApiInfo> apiInfos = javaApiInfoService.queryJavaApiInfo();
  69 +
  70 + for (JavaApiInfo javaApiInfo : apiInfos) {
  71 + javaApimap.put(javaApiInfo.getApiName(), javaApiInfo);
  72 + }
  73 +
  74 + //TODO 需要删选java服务
  75 + List<MObjectInfo> mObjectInfos = mObjectInfoService.queryMObjectsInfo();
  76 +
  77 + for (MObjectInfo mObjInfo : mObjectInfos) {
  78 + mObjInfoMap.add(mObjInfo.getMoTypeId(), mObjInfo);
  79 + }
  80 +
  81 + }
  82 +
  83 +
  84 + public void run() {
  85 + startTime=System.currentTimeMillis();
  86 + int tastNum = 0;
  87 + for (JavaApiInfo javaApiInfo : javaApimap.values()) {
  88 +
  89 + List<MObjectInfo> mObjectInfos = mObjInfoMap.get(javaApiInfo.getServiceType());
  90 + if (mObjectInfos == null) {
  91 + log.warn("type {} has no service object", javaApiInfo.getServiceType());
  92 + continue;
  93 + }
  94 + for (MObjectInfo mObjectInfo : mObjectInfos) {
  95 + completionService.submit(new JavaApiTask(restTemplate, javaApiInfo, mObjectInfo));
  96 + tastNum++;
  97 + }
  98 + }
  99 +
  100 + //wait for all rep end...
  101 + for (int i = 0; i < tastNum; i++) {
  102 + try {
  103 + Future<JavaApiStatics> rep = completionService.take();
  104 + handleResult(rep.get());
  105 + } catch (InterruptedException e) {
  106 + log.warn("completionService get rep interrupted ", e);
  107 + } catch (ExecutionException e) {
  108 + log.warn("completionService get rep failed ", e);
  109 + }
  110 + }
  111 +
  112 + //close 线程池
  113 + executorService.shutdownNow();
  114 + endTime=System.currentTimeMillis();
  115 + }
  116 +
  117 +
  118 + public void handleResult(JavaApiStatics rep) {
  119 + javaApiHadnler.handler(rep);
  120 + }
  121 +
  122 +
  123 + public void init4Test() {
  124 + executorService = new ThreadPoolExecutor(10, 30, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
  125 + new JavaApiThreadFactory());
  126 +
  127 + completionService = new ExecutorCompletionService<JavaApiStatics>(executorService);
  128 +
  129 + javaApimap = new ConcurrentHashMap<String, JavaApiInfo>();
  130 + mObjInfoMap = new LinkedMultiValueMap<Integer, MObjectInfo>();
  131 +
  132 + MObjectInfo mObj1 = new MObjectInfo();
  133 + mObj1.setMoId(1);
  134 + mObj1.setMoHostIp("192.168.102.205");
  135 + mObj1.setMoTags("8080");
  136 + mObj1.setMoName("gateway");
  137 + mObj1.setMoTypeId(1);
  138 +
  139 + mObjInfoMap.add(1, mObj1);
  140 +
  141 +// MObjectInfo mObj2 = new MObjectInfo();
  142 +// mObj2.setMoId(1);
  143 +// mObj2.setMoHostIp("192.168.102.207");
  144 +// mObj2.setMoTags("8080");
  145 +// mObj2.setMoName("gateway");
  146 +// mObj2.setMoTypeId(1);
  147 +//
  148 +// mObjInfoMap.add(1, mObj2);
  149 +
  150 + int uid = 8041886;
  151 + for (int i = 0; i < 10; i++) {
  152 + JavaApiInfo javaApiInfo = new JavaApiInfo();
  153 + javaApiInfo.setApiName("order.get_" + i);
  154 + javaApiInfo.setApiReqMethod(0);
  155 + javaApiInfo.setApiToggle(0);
  156 + javaApiInfo.setApiUrl("/gateway/?debug=XYZ&method=app.Shopping.listCoupon&uid=" + (uid++));
  157 + javaApiInfo.setServiceType(1);
  158 + javaApimap.put("order.get_" + i, javaApiInfo);
  159 + }
  160 +
  161 +
  162 + }
  163 +
  164 +}
  1 +package com.monitor.javaserver.common;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.model.JavaApiInfo;
  5 +import com.model.MObjectInfo;
  6 +import lombok.Data;
  7 +import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  8 +
  9 +/**
  10 + * 调用统计信息
  11 + * Created by fruwei on 2016/6/19.
  12 + */
  13 +@Data
  14 +public class JavaApiStatics{
  15 +
  16 + private JavaApiInfo javaApiInfo;
  17 +
  18 + private MObjectInfo mObjectInfo;
  19 +
  20 + private long startTime;
  21 +
  22 + private long endTime;
  23 +
  24 + private JSONObject response;
  25 +
  26 + private boolean hasException;
  27 +
  28 + private Exception exception;
  29 +
  30 + @Override
  31 + public String toString() {
  32 + return ReflectionToStringBuilder.toString(this);
  33 + }
  34 +}
  1 +package com.monitor.javaserver.common;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.model.JavaApiInfo;
  5 +import com.model.MObjectInfo;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.web.client.RestTemplate;
  9 +
  10 +import java.util.concurrent.Callable;
  11 +
  12 +/**
  13 + * Created by fruwei on 2016/6/19.
  14 + */
  15 +public class JavaApiTask implements Callable<JavaApiStatics> {
  16 +
  17 + Logger log = LoggerFactory.getLogger(JavaApiTask.class);
  18 +
  19 + private JavaApiInfo javaApiInfo;
  20 + private MObjectInfo mObjectInfo;
  21 +
  22 + private RestTemplate restTemplate;
  23 +
  24 +
  25 + public JavaApiTask(RestTemplate restTemplate, JavaApiInfo javaApiInfo, MObjectInfo mObjectInfo) {
  26 + this.javaApiInfo = javaApiInfo;
  27 + this.mObjectInfo = mObjectInfo;
  28 + this.restTemplate = restTemplate;
  29 + }
  30 +
  31 +
  32 + @Override
  33 + public JavaApiStatics call() throws Exception {
  34 +
  35 + String url = createReqUrl();
  36 + log.info("url: {}", url);
  37 + if (url == null)
  38 + return null;
  39 + JSONObject jsonRep = null;
  40 + JavaApiStatics apiStatics = new JavaApiStatics();
  41 + apiStatics.setStartTime(System.currentTimeMillis());
  42 + apiStatics.setHasException(false);
  43 + apiStatics.setJavaApiInfo(this.javaApiInfo);
  44 + apiStatics.setMObjectInfo(this.mObjectInfo);
  45 + try {
  46 + if (javaApiInfo.getApiReqMethod() == 0) {
  47 + jsonRep = restTemplate.getForObject(url, JSONObject.class);
  48 + } else {
  49 + JSONObject req = null;
  50 + jsonRep = restTemplate.postForObject(url, req, JSONObject.class);
  51 +
  52 + }
  53 + } catch (Exception e) {
  54 + log.warn("exception {} ",url);
  55 + apiStatics.setHasException(true);
  56 + apiStatics.setException(e);
  57 + }finally {
  58 + apiStatics.setEndTime(System.currentTimeMillis());
  59 + apiStatics.setResponse(jsonRep);
  60 + }
  61 +
  62 + return apiStatics;
  63 + }
  64 +
  65 +
  66 + private void createReq() {
  67 +
  68 + }
  69 +
  70 + private String createReqUrl() {
  71 + if (mObjectInfo.getMoHostIp() == null || mObjectInfo.getMoHostIp() == null || javaApiInfo.getApiUrl() == null) {
  72 + log.warn("get url failed has null prop. ");
  73 + return null;
  74 + }
  75 + String url = "http://" + mObjectInfo.getMoHostIp() + ":" + mObjectInfo.getMoTags() + "/" + javaApiInfo.getApiUrl();
  76 + return url;
  77 + }
  78 +}
  1 +package com.monitor.javaserver.common;
  2 +
  3 +import java.util.concurrent.ThreadFactory;
  4 +import java.util.concurrent.atomic.AtomicInteger;
  5 +
  6 +/**
  7 + * Created by fruwei on 2016/6/20.
  8 + */
  9 +public class JavaApiThreadFactory implements ThreadFactory {
  10 +
  11 + private static final AtomicInteger poolNumber = new AtomicInteger(1);
  12 + private final ThreadGroup group;
  13 + private final AtomicInteger threadNumber = new AtomicInteger(1);
  14 + private final String namePrefix;
  15 +
  16 + public JavaApiThreadFactory() {
  17 + SecurityManager s = System.getSecurityManager();
  18 + group = (s != null) ? s.getThreadGroup() :
  19 + Thread.currentThread().getThreadGroup();
  20 + namePrefix = "pool-javaApi" +
  21 + poolNumber.getAndIncrement() +
  22 + "-thread-";
  23 + }
  24 +
  25 +
  26 + @Override
  27 + public Thread newThread(Runnable r) {
  28 + Thread t = new Thread(group, r,
  29 + namePrefix + threadNumber.getAndIncrement(),
  30 + 0);
  31 + if (t.isDaemon())
  32 + t.setDaemon(false);
  33 + if (t.getPriority() != Thread.NORM_PRIORITY)
  34 + t.setPriority(Thread.NORM_PRIORITY);
  35 + return t;
  36 + }
  37 +}
  1 +package com.monitor.javaserver.handle;
  2 +
  3 +import com.monitor.javaserver.common.JavaApiStatics;
  4 +
  5 +/**
  6 + * java服务请求结果处理接口
  7 + * Created by fruwei on 2016/6/20.
  8 + */
  9 +public interface IJavaApiHadnler {
  10 +
  11 + public void handler(JavaApiStatics javaApiStatics) ;
  12 +
  13 +}
  1 +package com.monitor.javaserver.handle.impl;
  2 +
  3 +import com.monitor.javaserver.common.JavaApiStatics;
  4 +import com.monitor.javaserver.handle.IJavaApiHadnler;
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +import org.springframework.stereotype.Component;
  8 +
  9 +/**
  10 + * Created by fruwei on 2016/6/20.
  11 + */
  12 +@Component("influxDBJavaApiHandler")
  13 +public class InfluxDBJavaApiHandler implements IJavaApiHadnler{
  14 + Logger log= LoggerFactory.getLogger(InfluxDBJavaApiHandler.class);
  15 +
  16 + @Override
  17 + public void handler(JavaApiStatics javaApiStatics) {
  18 + log.info("handle result: {}",javaApiStatics);
  19 + //TODO handle
  20 +
  21 +
  22 +
  23 + }
  24 +}
1 <?xml version="1.0" encoding="UTF-8"?> 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans" 2 <beans xmlns="http://www.springframework.org/schema/beans"
3 - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
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"> 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
7 </beans> 8 </beans>
1 package com.model; 1 package com.model;
2 2
  3 +import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  4 +
3 public class JavaApiInfo { 5 public class JavaApiInfo {
4 private Integer serviceId; 6 private Integer serviceId;
5 7
@@ -13,6 +15,8 @@ public class JavaApiInfo { @@ -13,6 +15,8 @@ public class JavaApiInfo {
13 15
14 private Integer apiToggle; 16 private Integer apiToggle;
15 17
  18 + private Integer apiReqMethod;
  19 +
16 public Integer getServiceId() { 20 public Integer getServiceId() {
17 return serviceId; 21 return serviceId;
18 } 22 }
@@ -60,4 +64,17 @@ public class JavaApiInfo { @@ -60,4 +64,17 @@ public class JavaApiInfo {
60 public void setApiToggle(Integer apiToggle) { 64 public void setApiToggle(Integer apiToggle) {
61 this.apiToggle = apiToggle; 65 this.apiToggle = apiToggle;
62 } 66 }
  67 +
  68 + public Integer getApiReqMethod() {
  69 + return apiReqMethod;
  70 + }
  71 +
  72 + public void setApiReqMethod(Integer apiReqMethod) {
  73 + this.apiReqMethod = apiReqMethod;
  74 + }
  75 +
  76 + @Override
  77 + public String toString() {
  78 + return ReflectionToStringBuilder.toString(this);
  79 + }
63 } 80 }
@@ -3,6 +3,8 @@ package com.monitor.mysql.mapper; @@ -3,6 +3,8 @@ package com.monitor.mysql.mapper;
3 3
4 import com.model.JavaApiInfo; 4 import com.model.JavaApiInfo;
5 5
  6 +import java.util.List;
  7 +
6 public interface JavaApiInfoMapper { 8 public interface JavaApiInfoMapper {
7 int deleteByPrimaryKey(Integer serviceId); 9 int deleteByPrimaryKey(Integer serviceId);
8 10
@@ -15,4 +17,6 @@ public interface JavaApiInfoMapper { @@ -15,4 +17,6 @@ public interface JavaApiInfoMapper {
15 int updateByPrimaryKeySelective(JavaApiInfo record); 17 int updateByPrimaryKeySelective(JavaApiInfo record);
16 18
17 int updateByPrimaryKey(JavaApiInfo record); 19 int updateByPrimaryKey(JavaApiInfo record);
  20 +
  21 + List<JavaApiInfo> selectAllApi();
18 } 22 }
@@ -8,9 +8,10 @@ @@ -8,9 +8,10 @@
8 <result column="api_url" property="apiUrl" jdbcType="VARCHAR" /> 8 <result column="api_url" property="apiUrl" jdbcType="VARCHAR" />
9 <result column="api_data" property="apiData" jdbcType="VARCHAR" /> 9 <result column="api_data" property="apiData" jdbcType="VARCHAR" />
10 <result column="api_toggle" property="apiToggle" jdbcType="INTEGER" /> 10 <result column="api_toggle" property="apiToggle" jdbcType="INTEGER" />
  11 + <result column="api_req_method" property="apiReqMethod" jdbcType="INTEGER" />
11 </resultMap> 12 </resultMap>
12 <sql id="Base_Column_List" > 13 <sql id="Base_Column_List" >
13 - service_id, service_type, api_name, api_url, api_data, api_toggle 14 + service_id, service_type, api_name, api_url, api_data, api_toggle, api_req_method
14 </sql> 15 </sql>
15 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > 16 <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
16 select 17 select
@@ -24,11 +25,11 @@ @@ -24,11 +25,11 @@
24 </delete> 25 </delete>
25 <insert id="insert" parameterType="com.model.JavaApiInfo" > 26 <insert id="insert" parameterType="com.model.JavaApiInfo" >
26 insert into java_api_info (service_id, service_type, api_name, 27 insert into java_api_info (service_id, service_type, api_name,
27 - api_url, api_data, api_toggle  
28 - ) 28 + api_url, api_data, api_toggle,
  29 + api_req_method)
29 values (#{serviceId,jdbcType=INTEGER}, #{serviceType,jdbcType=INTEGER}, #{apiName,jdbcType=VARCHAR}, 30 values (#{serviceId,jdbcType=INTEGER}, #{serviceType,jdbcType=INTEGER}, #{apiName,jdbcType=VARCHAR},
30 - #{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER}  
31 - ) 31 + #{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER},
  32 + #{apiReqMethod,jdbcType=INTEGER})
32 </insert> 33 </insert>
33 <insert id="insertSelective" parameterType="com.model.JavaApiInfo" > 34 <insert id="insertSelective" parameterType="com.model.JavaApiInfo" >
34 insert into java_api_info 35 insert into java_api_info
@@ -51,6 +52,9 @@ @@ -51,6 +52,9 @@
51 <if test="apiToggle != null" > 52 <if test="apiToggle != null" >
52 api_toggle, 53 api_toggle,
53 </if> 54 </if>
  55 + <if test="apiReqMethod != null" >
  56 + api_req_method,
  57 + </if>
54 </trim> 58 </trim>
55 <trim prefix="values (" suffix=")" suffixOverrides="," > 59 <trim prefix="values (" suffix=")" suffixOverrides="," >
56 <if test="serviceId != null" > 60 <if test="serviceId != null" >
@@ -71,6 +75,9 @@ @@ -71,6 +75,9 @@
71 <if test="apiToggle != null" > 75 <if test="apiToggle != null" >
72 #{apiToggle,jdbcType=INTEGER}, 76 #{apiToggle,jdbcType=INTEGER},
73 </if> 77 </if>
  78 + <if test="apiReqMethod != null" >
  79 + #{apiReqMethod,jdbcType=INTEGER},
  80 + </if>
74 </trim> 81 </trim>
75 </insert> 82 </insert>
76 <update id="updateByPrimaryKeySelective" parameterType="com.model.JavaApiInfo" > 83 <update id="updateByPrimaryKeySelective" parameterType="com.model.JavaApiInfo" >
@@ -91,6 +98,9 @@ @@ -91,6 +98,9 @@
91 <if test="apiToggle != null" > 98 <if test="apiToggle != null" >
92 api_toggle = #{apiToggle,jdbcType=INTEGER}, 99 api_toggle = #{apiToggle,jdbcType=INTEGER},
93 </if> 100 </if>
  101 + <if test="apiReqMethod != null" >
  102 + api_req_method = #{apiReqMethod,jdbcType=INTEGER},
  103 + </if>
94 </set> 104 </set>
95 where service_id = #{serviceId,jdbcType=INTEGER} 105 where service_id = #{serviceId,jdbcType=INTEGER}
96 </update> 106 </update>
@@ -100,7 +110,16 @@ @@ -100,7 +110,16 @@
100 api_name = #{apiName,jdbcType=VARCHAR}, 110 api_name = #{apiName,jdbcType=VARCHAR},
101 api_url = #{apiUrl,jdbcType=VARCHAR}, 111 api_url = #{apiUrl,jdbcType=VARCHAR},
102 api_data = #{apiData,jdbcType=VARCHAR}, 112 api_data = #{apiData,jdbcType=VARCHAR},
103 - api_toggle = #{apiToggle,jdbcType=INTEGER} 113 + api_toggle = #{apiToggle,jdbcType=INTEGER},
  114 + api_req_method = #{apiReqMethod,jdbcType=INTEGER}
104 where service_id = #{serviceId,jdbcType=INTEGER} 115 where service_id = #{serviceId,jdbcType=INTEGER}
105 </update> 116 </update>
  117 +
  118 + <select id="selectAllApi" resultMap="BaseResultMap" >
  119 + select
  120 + <include refid="Base_Column_List" />
  121 + from java_api_info
  122 + </select>
  123 +
  124 +
106 </mapper> 125 </mapper>
@@ -51,9 +51,14 @@ @@ -51,9 +51,14 @@
51 <artifactId>monitor-service-switch</artifactId> 51 <artifactId>monitor-service-switch</artifactId>
52 </dependency> 52 </dependency>
53 <dependency> 53 <dependency>
  54 + <groupId>org.springframework</groupId>
  55 + <artifactId>spring-test</artifactId>
  56 + <version>4.2.2.RELEASE</version>
  57 + </dependency>
  58 + <dependency>
54 <groupId>junit</groupId> 59 <groupId>junit</groupId>
55 <artifactId>junit</artifactId> 60 <artifactId>junit</artifactId>
56 - <version>3.8.1</version> 61 + <version>4.11</version>
57 <scope>test</scope> 62 <scope>test</scope>
58 </dependency> 63 </dependency>
59 </dependencies> 64 </dependencies>
1 #---------jdbc config---------- 1 #---------jdbc config----------
2 -local.jdbc.url=jdbc:mysql://172.16.6.234:3306/yh_ops?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull 2 +local.jdbc.url=jdbc:mysql://localhost:3306/ops?characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull
3 local.jdbc.user=root 3 local.jdbc.user=root
4 -local.jdbc.password=123456 4 +local.jdbc.password=root
5 #---------jdbc config---------- 5 #---------jdbc config----------
  1 +package com.monitor;
  2 +
  3 +import com.model.JavaApiInfo;
  4 +import com.monitor.cmdb.service.IJavaApiInfoService;
  5 +import org.junit.Test;
  6 +import org.junit.runner.RunWith;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.context.annotation.PropertySource;
  11 +import org.springframework.test.context.ContextConfiguration;
  12 +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  13 +
  14 +import java.util.List;
  15 +
  16 +/**
  17 + * Created by fruwei on 2016/6/17.
  18 + */
  19 +@RunWith(SpringJUnit4ClassRunner.class)
  20 +@ContextConfiguration(locations = {
  21 + "classpath*:META-INF/spring/spring*.xml"})
  22 +@PropertySource({"classpath:jdbc.properties",
  23 + "classpath:*.properties"})
  24 +public class JavaApiDBTest {
  25 + Logger logger = LoggerFactory.getLogger("test");
  26 + @Autowired
  27 + IJavaApiInfoService javaApiInfoService;
  28 +
  29 +
  30 + @Test
  31 + public void testAdd() {
  32 + JavaApiInfo javaApiInfo = new JavaApiInfo();
  33 + javaApiInfo.setApiName("test");
  34 + javaApiInfo.setServiceType(1);
  35 + javaApiInfo.setApiUrl("1111111");
  36 + javaApiInfoService.saveJavaApiInfo(javaApiInfo);
  37 + }
  38 +
  39 + @Test
  40 + public void testQuery() {
  41 + List<JavaApiInfo> list = javaApiInfoService.queryJavaApiInfo();
  42 + for (JavaApiInfo javaApiInfo : list) {
  43 + logger.info(javaApiInfo.getApiName());
  44 + }
  45 + }
  46 +
  47 +
  48 + @Test
  49 + public void testDel() {
  50 +
  51 + }
  52 +
  53 + @Test
  54 + public void testUpdate() {
  55 + javaApiInfoService.delJavaApiInfo(1);
  56 + }
  57 +
  58 +
  59 +}