Skip to content

Commit

Permalink
fixbug: 服务启动时,60秒内的服务上线通知过滤掉
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyQing committed Mar 29, 2024
1 parent e9fa5dd commit 82e1861
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,31 @@
import io.github.smart.cloud.starter.monitor.properties.MonitorProperties;
import io.github.smart.cloud.utility.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import reactor.core.publisher.Mono;

import java.time.Instant;

/**
* 服务状态变更监听
*
* @author collin
* @date 2020-09-15
*/
@Slf4j
public class AppChangeNotifier extends AbstractStatusChangeNotifier {
public class AppChangeNotifier extends AbstractStatusChangeNotifier implements ApplicationListener<ApplicationReadyEvent> {

private final InstanceRepository instanceRepository;
private final MonitorProperties monitorProperties;
private final ApplicationEventPublisher applicationEventPublisher;
/**
* 服务启动时过滤消息时间间隔(单位:毫秒)
*/
private static final Long FILTER_EVENT_TS = 60 * 1000L;
/**
* 服务启动时间
*/
private static final long START_UP_TS = System.currentTimeMillis();
private Long startUpTs = null;

public AppChangeNotifier(InstanceRepository instanceRepository, MonitorProperties monitorProperties, ApplicationEventPublisher applicationEventPublisher) {
super(instanceRepository);
Expand All @@ -57,6 +61,11 @@ public AppChangeNotifier(InstanceRepository instanceRepository, MonitorPropertie
this.applicationEventPublisher = applicationEventPublisher;
}

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
this.startUpTs = System.currentTimeMillis();
}

@Override
protected boolean shouldNotify(InstanceEvent event, Instance instance) {
return event instanceof InstanceStatusChangedEvent || event instanceof InstanceDeregisteredEvent;
Expand All @@ -71,8 +80,8 @@ protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
}

StatusInfo statusInfo = instance.getStatusInfo();
if (instance.getStatusTimestamp().plusMillis(monitorProperties.getFilterEventTs()).isBefore(Instant.now()) && statusInfo.isUp()) {
//服务启动时,90秒以前启动的服务上线通知过滤掉
if (statusInfo.isUp() && (startUpTs == null || System.currentTimeMillis() - startUpTs <= monitorProperties.getFilterEventTs())) {
//服务启动时,60秒内的服务上线通知过滤掉
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MonitorProperties {
/**
* 服务启动时过滤消息时间间隔(单位:毫秒)
*/
private Long filterEventTs = 90 * 1000L;
private Long filterEventTs = 60 * 1000L;
/**
* 检查离线服务在线实例数间隔时间(单位:毫秒)
*/
Expand Down

0 comments on commit 82e1861

Please sign in to comment.