java nginx监控服务程序调度算法实现的代码

Posted whoamboys

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java nginx监控服务程序调度算法实现的代码相关的知识,希望对你有一定的参考价值。

将内容过程比较常用的内容备份一下,如下内容是关于java nginx监控服务程序调度算法实现的内容,希望对小伙伴们有较大用途。

package com.wole.monitor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.jetty.util.ConcurrentHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wole.monitor.dao.ServiceDao;
import com.wole.servicemonitor.util.ServiceUtils;

public class MonitorsManage {
private final static Logger logger = LoggerFactory.getLogger(MonitorsManage.class);

private ServiceDao dao;

private Executor commExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());

private CompletionService<Response> completionService = new ExecutorCompletionService<Response>(commExecutor);

private ConcurrentHashSet<MonitorService> currentSet = new ConcurrentHashSet<MonitorService>();

private Queue<MonitorService> sleepQueue = new PriorityBlockingQueue<MonitorService>();

private Queue<MonitorService> executeQueue = new LinkedBlockingQueue<MonitorService>();

private AtomicBoolean isClose = new AtomicBoolean(false);

private AtomicLong startTime = new AtomicLong(0);
private AtomicLong intervalTime = new AtomicLong(0);

public void close() {
logger.info("closing................");
isClose.compareAndSet(false, true);
}

 

public void init() {
logger.info("初始化");

}

public void work() {
logger.info("开始工作");

Thread productThread = new Thread(new ProductMonitor(1000));
Thread consumerThread = new Thread(new ConsumerMonitor(1000));
Thread recoverThread = new Thread(new RecoverMonitor(1000));

Thread refreshThread = new Thread(new RefreshMonitorService(60000, dao));
productThread.start();
consumerThread.start();
recoverThread.start();
refreshThread.start();

}

class ProductMonitor implements Runnable {
long sleepTime = 1000;

public ProductMonitor(long sleepTime) {
this.sleepTime = sleepTime;
}

@Override
public void run() {
logger.info("生产者开启工作");
long now = System.currentTimeMillis();
long lastTime = now;
startTime.addAndGet(now);
try {
do {
Thread.sleep(sleepTime);
logger.debug("生产者休息{}ms", sleepTime);
now = System.currentTimeMillis();
intervalTime.addAndGet(now - lastTime);
while (sleepQueue.size() > 0) {
MonitorService service = sleepQueue.peek();
if (service.getCurrentTime() - intervalTime.get() < 1) {
if (!currentSet.contains(service)) {
logger.info("service {} 已被删除,不加入执行队列了", service.toString());
continue;
}
executeQueue.add(service);
} else {
logger.debug("还有{}秒可执行", service.getCurrentTime() - intervalTime.get());
break;
}
}

if (sleepQueue.size() <= 0) {
logger.debug("生产队列为空");
}
lastTime = now;
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}

}

}

class ConsumerMonitor implements Runnable {
long sleepTime = 1000;

public ConsumerMonitor(long sleepTime) {
this.sleepTime = sleepTime;
if (sleepTime < 1000) {
throw new RuntimeException("请配置sleepTime值大一些");
}
}

@Override
public void run() {
logger.info("消费者开启工作");
try {
do {
Thread.sleep(sleepTime);
logger.debug("消费者休息{}ms", sleepTime);
while (executeQueue.size() > 0) {
final MonitorService service = executeQueue.poll();
completionService.submit(new ExecuteCallable(service));
}
logger.debug("消费队列为空");
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}

}

class ExecuteCallable implements Callable<Response> {
final MonitorService service;

public ExecuteCallable(MonitorService service) {
this.service = service;
}

@Override
public Response call() throws Exception {
logger.debug("执行");
Map<String, String> r = new HashMap<String, String>();
Response response = new Response();
response.service = service;
response.response = r;
Monitor m = MonitorFactory.getMonitor(service);
response.isNeedWarn = m.isNeedWarnging(service, r);
if (response.isNeedWarn) {
response.isSucToNotify = m.sendNotify(service, r);
}
return response;
}

}

class RecoverMonitor implements Runnable {
private long sleepTime = 1000;

private long count = 0;

public RecoverMonitor(long sleepTime) {
this.sleepTime = sleepTime;
if (sleepTime < 1000) {
throw new RuntimeException("请配置sleepTime值大一些");
}
}

@Override
public void run() {
logger.info("回收者开启工作");
try {
do {
Future<Response> response = completionService.take();
MonitorService s = response.get().service;
if (!currentSet.contains(s)) {
logger.info("service {} 已被删除,不回收了", s.toString());
continue;
}
s.setCurrentTime(s.getIntervalTime() + intervalTime.get());
sleepQueue.add(s);
count++;
logger.info("回收,当前回收数量:" + count);
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}
}

class RefreshMonitorService implements Runnable {
private long sleepTime = 1000;
private ServiceDao dao;

public RefreshMonitorService(long sleepTime, ServiceDao dao) {
this.sleepTime = sleepTime;
if (sleepTime < 60000) {
logger.warn("刷新加载数据的间隔时间不能太短");
throw new RuntimeException("刷新加载数据的间隔时间不能太短");
}
this.dao = dao;
}

private void firstLoad() {
List<MonitorService> monitorService = dao.getService();
logger.info("加载记录:" + monitorService.size());

for (int j = 0; j < monitorService.size(); j++) {
MonitorService service = monitorService.get(j);
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
currentSet.add(service);
sleepQueue.add(service);
}

}

@Override
public void run() {
logger.info("读取新的service开启工作");
firstLoad();
try {
do {
logger.info("定时加载新的数据监听者休息{}ms", sleepTime);
Thread.sleep(sleepTime);
logger.info("##########开始执行更新数据############");
List<MonitorService> deleteList = dao.deleteService();
List<MonitorService> addList = dao.incrementalService();
logger.info("删除旧的数据共:{}", deleteList.size());
currentSet.removeAll(deleteList);
logger.info("增加新的数据共:{}", addList.size());
currentSet.addAll(addList);
logger.info("更新后的currentSet size:{}", currentSet.size());

for (MonitorService service : addList) {
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
sleepQueue.add(service);
}
logger.info("########这一轮更新结束");
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}
}

class Response {
public Map<String, String> response;
public MonitorService service;
public boolean isNeedWarn;
public boolean isSucToNotify;

}

public void setDao(ServiceDao dao) {
this.dao = dao;
}

}

































































































































































































以上是关于java nginx监控服务程序调度算法实现的代码的主要内容,如果未能解决你的问题,请参考以下文章

Zabbix:项目实战之--自动发现nginx调度器及后端web服务集群自定义参数监控

nginx反代varnish缓存服务器实现后端amp动静分离架构

38-Zabbix故障自愈和主动监控模式以及JAVA程序监控

急求 程序代码 c/c++ 操作系统中的 处理机调度算法

Keepalived+nginx实现高可用

16.负载均衡的配置场景和调度算法