diff --git a/src/main/java/com/ctrip/zeus/nginx/NginxStatusFetcher.java b/src/main/java/com/ctrip/zeus/nginx/NginxStatusFetcher.java new file mode 100644 index 00000000..fc935891 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/nginx/NginxStatusFetcher.java @@ -0,0 +1,47 @@ +package com.ctrip.zeus.nginx; + +import com.ctrip.zeus.client.LocalClient; +import com.ctrip.zeus.nginx.entity.TrafficStatus; +import com.ctrip.zeus.util.RollingTrafficStatus; +import org.quartz.JobExecutionException; +import org.springframework.scheduling.quartz.QuartzJobBean; +import org.springframework.web.context.support.SpringBeanAutowiringSupport; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Created by zhoumy on 2015/6/3. + */ +public class NginxStatusFetcher extends QuartzJobBean { + + private RollingTrafficStatus rollingTrafficStatus; + private AtomicInteger tick = new AtomicInteger(); + + public List getResult() { + return rollingTrafficStatus.getResult(); + } + + @Override + protected void executeInternal(org.quartz.JobExecutionContext jobExecutionContext) throws JobExecutionException { + if (tick.incrementAndGet() == 10) { + clearDirtyRecords(System.currentTimeMillis()); + tick.set(0); + } + fetchTrafficStatus(); + } + + private void fetchTrafficStatus() { + String stubStatus = LocalClient.getInstance().getStubStatus(); + String reqStatus = LocalClient.getInstance().getReqStatuses(); + rollingTrafficStatus.add(stubStatus, reqStatus); + } + + private void clearDirtyRecords(long stamp) { + rollingTrafficStatus.clearDirty(stamp); + } + + private void setRollingTrafficStatus(RollingTrafficStatus rollingTrafficStatus) { + this.rollingTrafficStatus = rollingTrafficStatus; + } +} diff --git a/src/main/java/com/ctrip/zeus/server/SlbAdminServer.java b/src/main/java/com/ctrip/zeus/server/SlbAdminServer.java index 789b39eb..65b44af1 100644 --- a/src/main/java/com/ctrip/zeus/server/SlbAdminServer.java +++ b/src/main/java/com/ctrip/zeus/server/SlbAdminServer.java @@ -123,9 +123,6 @@ public class SlbAdminServer extends AbstractServer { @Override protected void doStart() throws Exception { server.start(); - - //Set traffic status collector daemon thread - TrafficStatusCollector.getInstance().start(); } @Override diff --git a/src/main/java/com/ctrip/zeus/service/nginx/impl/NginxServiceImpl.java b/src/main/java/com/ctrip/zeus/service/nginx/impl/NginxServiceImpl.java index f9146393..fd15d1a5 100644 --- a/src/main/java/com/ctrip/zeus/service/nginx/impl/NginxServiceImpl.java +++ b/src/main/java/com/ctrip/zeus/service/nginx/impl/NginxServiceImpl.java @@ -12,6 +12,7 @@ import com.ctrip.zeus.nginx.entity.TrafficStatus; import com.ctrip.zeus.service.build.NginxConfService; import com.ctrip.zeus.service.model.SlbRepository; import com.ctrip.zeus.service.nginx.NginxService; +import com.ctrip.zeus.util.RollingTrafficStatus; import com.ctrip.zeus.util.S; import com.ctrip.zeus.util.TrafficStatusCollector; import com.netflix.config.DynamicIntProperty; @@ -40,6 +41,8 @@ public class NginxServiceImpl implements NginxService { private NginxConfService nginxConfService; @Resource private NginxServerDao nginxServerDao; + @Resource + private RollingTrafficStatus rollingTrafficStatus; private Logger logger = LoggerFactory.getLogger(NginxServiceImpl.class); @@ -280,7 +283,7 @@ public class NginxServiceImpl implements NginxService { @Override public List getLocalTrafficStatus() { - List l = TrafficStatusCollector.getInstance().getResult(); + List l = rollingTrafficStatus.getResult(); for (TrafficStatus trafficStatus : l) { trafficStatus.setServerIp(S.getIp()); } diff --git a/src/main/java/com/ctrip/zeus/util/RollingTrafficStatus.java b/src/main/java/com/ctrip/zeus/util/RollingTrafficStatus.java index f5d68a4f..dc3804a0 100644 --- a/src/main/java/com/ctrip/zeus/util/RollingTrafficStatus.java +++ b/src/main/java/com/ctrip/zeus/util/RollingTrafficStatus.java @@ -3,17 +3,23 @@ package com.ctrip.zeus.util; import com.ctrip.zeus.nginx.entity.ReqStatus; import com.ctrip.zeus.nginx.entity.TrafficStatus; import com.google.common.base.Preconditions; +import org.springframework.stereotype.Component; import java.util.*; /** * Created by zhoumy on 2015/5/6. */ +@Component("rollingTrafficStatus") public class RollingTrafficStatus { private final CircularArray buckets; private final int numberOfBuckets; private final int interval; + public RollingTrafficStatus() { + this(10, 60); + } + /** * Collecting data and offering statistics of a given time over defined interval. * For example, if you want to track 1-minute data per 10 minutes, it doesn't empty out every 10 minutes, diff --git a/src/main/java/com/ctrip/zeus/util/TrafficStatusCollector.java b/src/main/java/com/ctrip/zeus/util/TrafficStatusCollector.java index abd96a90..4816d348 100644 --- a/src/main/java/com/ctrip/zeus/util/TrafficStatusCollector.java +++ b/src/main/java/com/ctrip/zeus/util/TrafficStatusCollector.java @@ -13,16 +13,11 @@ public class TrafficStatusCollector { private final int syncInterval; private final int size; private final RollingTrafficStatus rollingTrafficStatus; - private final static TrafficStatusCollector instance = new TrafficStatusCollector(); public TrafficStatusCollector() { this(60, 10); } - public static TrafficStatusCollector getInstance() { - return instance; - } - public TrafficStatusCollector(int syncInterval, int size) { this.syncInterval = syncInterval; this.size = size; diff --git a/src/main/resources/spring-context.xml b/src/main/resources/spring-context.xml index 9e1484f7..5cd37ec9 100644 --- a/src/main/resources/spring-context.xml +++ b/src/main/resources/spring-context.xml @@ -18,5 +18,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + +