最近,api老不稳定呀,要等用户反馈才知道问题,老板火了,问同事做过没,没做过呀,小码农我只能翻身干活儿,这个需求被自己想起到基本框架实现,也就10来分钟的样子;能准确理解需求,然后迅速转化为代码实现,属于现学现用,很多不大会,但只要花时间基本都可以会,在自我看来几乎是没有上限的,只是感叹时间在哪儿,能超过这种理解能力的应该大有人在,不过超过这种水平的人一般不在我们面前装逼,因为确实是高手;还有一种偶尔写了一个文章就开始装了,大多半桶水,必须吐槽一下,没时间写文章,吐槽一定还是有时间的,兴趣之一
直接上代码,有需要讨论的欢迎讨论,有不完善的欢迎纠正
数据库设计:
Create Table
CREATE TABLE `api_info` (
`api_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`api_url` char(200) DEFAULT NULL,
`api_name` char(200) NOT NULL DEFAULT '""' COMMENT 'api名称',
`method` char(10) DEFAULT 'GET' COMMENT 'GET/POST/DELETE/PUT',
`header` char(255) NOT NULL DEFAULT 'application/json' COMMENT 'application',
`params` char(255) DEFAULT NULL COMMENT '参数',
`result` text NOT NULL COMMENT '结果',
`extract_variable_value` char(200) DEFAULT '""' COMMENT '提取变量值',
`time_interval` int(11) NOT NULL DEFAULT '5' COMMENT '请求时间间隔,单位秒',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`group_id` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`api_id`),
UNIQUE KEY `api_url` (`api_url`,`params`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=utf8mb4
核心代码:
package io.xx.xx.api.cron;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.xx.xx.api.entity.ApiMonitor;
import io.xx.xx.api.service.ApiMonitorService;
import io.xx.xx.core.bean.OkHttpUtils;
import io.xx.xx.framework.service.MailSendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
@EnableAsync
public class ApiMonitorCron implements Runnable {
private static ExecutorService pool;
private static final Logger logger = LoggerFactory.getLogger(ApiMonitorCron.class);
private MailSendService mailSendService;
@Autowired
private ApiMonitorService apiMonitorService;
static AtomicInteger i = new AtomicInteger(-1);
static AtomicInteger n = new AtomicInteger(0);
static List<ApiMonitor> apiInfolist = new ArrayList<>();
public static void main(String[] args) {
//maximumPoolSize设置为2 ,拒绝策略为AbortPolic策略,直接抛出异常
pool = new ThreadPoolExecutor(3, 5, 1000, MILLISECONDS, new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 5; i++) {
pool.execute(new ApiMonitorCron());
}
}
@Async
@Scheduled(cron = "0/10 * * * * ?")
public void configureTasks() {
apiInfolist = apiMonitorService.listAll();
n = new AtomicInteger(apiInfolist.size() - 1);
i = new AtomicInteger(-1);
pool = new ThreadPoolExecutor(3, 5, 5000, MILLISECONDS, new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
for (int i = 0; i < 5; i++) {
pool.execute(new ApiMo