use of com.alibaba.nacos.client.naming.event.InstancesChangeEvent in project muses by acgist.
the class ShutdownListener method init.
@PostConstruct
public void init() {
if (!this.shutdownEnable) {
log.info("服务没有配置自动关机:{}", this.serviceName);
return;
}
log.info("启动服务关机配置:{}-{}", this.serviceName, this.shutdownGracefully);
NotifyCenter.registerSubscriber(new Subscriber<InstancesChangeEvent>() {
/**
* 锁
*/
private final Lock lock = new ReentrantLock();
/**
* 条件
*/
private final Condition condition = this.lock.newCondition();
@Override
public void onEvent(InstancesChangeEvent event) {
final Optional<Instance> optional = event.getHosts().stream().filter(ShutdownListener.this::self).findFirst().or(ShutdownListener.this::findSelf);
if (optional.isPresent()) {
this.up();
} else {
this.down();
}
}
/**
* 启动
*/
private void up() {
if (!ShutdownListener.this.shutdown) {
log.debug("实例有效:已经生效:{}", ShutdownListener.this.serviceName);
return;
}
log.debug("实例有效:重置关闭事件:{}", ShutdownListener.this.serviceName);
ShutdownListener.this.shutdown = false;
try {
this.lock.lock();
this.condition.signalAll();
} finally {
this.lock.unlock();
}
}
/**
* 关闭
*/
private void down() {
if (ShutdownListener.this.shutdown) {
log.debug("实例无效:已经启动关闭事件:{}", ShutdownListener.this.serviceName);
return;
}
log.debug("实例无效:启动关闭事件:{}-{}", ShutdownListener.this.serviceName, ShutdownListener.this.shutdownGracefully);
ShutdownListener.this.shutdown = true;
ShutdownListener.this.taskExecutor.execute(() -> {
try {
this.lock.lock();
final long remaing = this.condition.awaitNanos(TimeUnit.SECONDS.toNanos(ShutdownListener.this.shutdownGracefully));
if (ShutdownListener.this.shutdown) {
log.info("实例无效:关闭实例:{}", ShutdownListener.this.serviceName);
// 关闭NacosServiceManager.nacosServiceShutDown()会出现空指针异常:2021-08以后版本已经修复
// 阻塞关闭:不用再次等待
ShutdownListener.this.context.close();
// System.exit(0);
// 强制关机
Runtime.getRuntime().halt(0);
} else {
log.debug("实例有效:忽略关闭事件:{}-{}", ShutdownListener.this.serviceName, TimeUnit.NANOSECONDS.toSeconds(remaing));
}
} catch (InterruptedException e) {
log.error("关闭实例异常", e);
} finally {
this.lock.unlock();
}
});
}
@Override
public Class<? extends Event> subscribeType() {
return InstancesChangeEvent.class;
}
});
}
use of com.alibaba.nacos.client.naming.event.InstancesChangeEvent in project nacos by alibaba.
the class ServiceInfoHolder method processServiceInfo.
/**
* Process service info.
*
* @param serviceInfo new service info
* @return service info
*/
public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) {
String serviceKey = serviceInfo.getKey();
if (serviceKey == null) {
return null;
}
ServiceInfo oldService = serviceInfoMap.get(serviceInfo.getKey());
if (isEmptyOrErrorPush(serviceInfo)) {
// empty or error push, just ignore
return oldService;
}
serviceInfoMap.put(serviceInfo.getKey(), serviceInfo);
boolean changed = isChangedServiceInfo(oldService, serviceInfo);
if (StringUtils.isBlank(serviceInfo.getJsonFromServer())) {
serviceInfo.setJsonFromServer(JacksonUtils.toJson(serviceInfo));
}
MetricsMonitor.getServiceInfoMapSizeMonitor().set(serviceInfoMap.size());
if (changed) {
NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceInfo.getKey(), JacksonUtils.toJson(serviceInfo.getHosts()));
NotifyCenter.publishEvent(new InstancesChangeEvent(serviceInfo.getName(), serviceInfo.getGroupName(), serviceInfo.getClusters(), serviceInfo.getHosts()));
DiskCache.write(serviceInfo, cacheDir);
}
return serviceInfo;
}
Aggregations