## 添加微服务注册成功刷新nacos缓存;
This commit is contained in:
parent
3c680ebd07
commit
defe92fa25
|
@ -0,0 +1,43 @@
|
|||
package com.hzs.listener;
|
||||
|
||||
import com.alibaba.nacos.client.naming.event.InstancesChangeEvent;
|
||||
import com.alibaba.nacos.common.notify.NotifyCenter;
|
||||
import com.alibaba.nacos.common.notify.listener.Subscriber;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class NacosInstancesChangeEventListener extends Subscriber<InstancesChangeEvent> {
|
||||
|
||||
@Resource
|
||||
private CacheManager defaultLoadBalancerCacheManager;
|
||||
|
||||
@PostConstruct
|
||||
public void registerToNotifyCenter(){
|
||||
NotifyCenter.registerSubscriber(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(InstancesChangeEvent event) {
|
||||
log.info("scm 接收微服务刷新事件:{}, 开始刷新本地存储的微服务缓存", event.getServiceName());
|
||||
Cache cache = defaultLoadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME);
|
||||
if (cache != null) {
|
||||
cache.evict(event.getServiceName());
|
||||
log.info("{} 微服务刷新", event.getServiceName());
|
||||
}
|
||||
log.info("scm 微服务缓存刷新完成");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends com.alibaba.nacos.common.notify.Event> subscribeType() {
|
||||
return InstancesChangeEvent.class;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue