| import java.util.Map; |
| import java.util.Set; |
| import java.util.concurrent.ConcurrentHashMap; |
| import java.util.concurrent.atomic.AtomicBoolean; |
| import java.util.concurrent.atomic.AtomicLong; |
| |
| import com.alibaba.cloud.nacos.NacosConfigManager; |
| import com.alibaba.cloud.nacos.NacosConfigProperties; |
| import com.alibaba.cloud.nacos.NacosPropertySourceRepository; |
| import com.alibaba.cloud.nacos.client.NacosPropertySource; |
| import com.alibaba.cloud.nacos.refresh.NacosRefreshHistory; |
| import com.alibaba.cloud.nacos.refresh.NacosRefreshProperties; |
| import com.alibaba.nacos.api.config.ConfigService; |
| import com.alibaba.nacos.api.config.listener.AbstractSharedListener; |
| import com.alibaba.nacos.api.config.listener.Listener; |
| import com.alibaba.nacos.api.exception.NacosException; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import org.springframework.boot.context.event.ApplicationReadyEvent; |
| import org.springframework.cloud.context.refresh.ContextRefresher; |
| import org.springframework.context.ApplicationContext; |
| import org.springframework.context.ApplicationContextAware; |
| import org.springframework.context.ApplicationListener; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public class CustomNacosContextRefresher |
| implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware { |
| |
| private final static Logger log = LoggerFactory |
| .getLogger(CustomNacosContextRefresher.class); |
| |
| private static final AtomicLong REFRESH_COUNT = new AtomicLong(0); |
| |
| private NacosConfigProperties nacosConfigProperties; |
| |
| private final NacosRefreshHistory nacosRefreshHistory; |
| |
| private final ConfigService configService; |
| |
| private ContextRefresher refresh; |
| |
| private ApplicationContext applicationContext; |
| |
| private AtomicBoolean ready = new AtomicBoolean(false); |
| |
| private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16); |
| |
| public CustomNacosContextRefresher(NacosConfigManager nacosConfigManager, |
| NacosRefreshHistory refreshHistory, ContextRefresher refresh) { |
| this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties(); |
| this.nacosRefreshHistory = refreshHistory; |
| this.configService = nacosConfigManager.getConfigService(); |
| this.refresh = refresh; |
| } |
| |
| |
| |
| |
| |
| |
| |
| @Deprecated |
| public CustomNacosContextRefresher(NacosRefreshProperties refreshProperties, |
| NacosRefreshHistory refreshHistory, ConfigService configService) { |
| this.nacosRefreshHistory = refreshHistory; |
| this.configService = configService; |
| } |
| |
| @Override |
| public void onApplicationEvent(ApplicationReadyEvent event) { |
| |
| if (this.ready.compareAndSet(false, true)) { |
| this.registerNacosListenersForApplications(); |
| } |
| } |
| |
| @Override |
| public void setApplicationContext(ApplicationContext applicationContext) { |
| this.applicationContext = applicationContext; |
| } |
| |
| |
| |
| |
| private void registerNacosListenersForApplications() { |
| for (NacosPropertySource propertySource : NacosPropertySourceRepository |
| .getAll()) { |
| if (!propertySource.isRefreshable()) { |
| continue; |
| } |
| String dataId = propertySource.getDataId(); |
| registerNacosListener(propertySource.getGroup(), dataId); |
| } |
| } |
| |
| private void registerNacosListener(final String groupKey, final String dataKey) { |
| String key = NacosPropertySourceRepository.getMapKey(dataKey, groupKey); |
| Listener listener = listenerMap.computeIfAbsent(key, |
| lst -> new AbstractSharedListener() { |
| @Override |
| public void innerReceive(String dataId, String group, |
| String configInfo) { |
| refreshCountIncrement(); |
| nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo); |
| |
| Set<String> keys = refresh.refreshEnvironment(); |
| log.info("Refresh keys changed: " + keys); |
| if (log.isDebugEnabled()) { |
| log.debug(String.format( |
| "Refresh Nacos config group=%s,dataId=%s,configInfo=%s", |
| group, dataId, configInfo)); |
| } |
| } |
| }); |
| try { |
| configService.addListener(dataKey, groupKey, listener); |
| } |
| catch (NacosException e) { |
| log.warn(String.format( |
| "register fail for nacos listener ,dataId=[%s],group=[%s]", dataKey, |
| groupKey), e); |
| } |
| } |
| |
| public NacosConfigProperties getNacosConfigProperties() { |
| return nacosConfigProperties; |
| } |
| |
| public CustomNacosContextRefresher setNacosConfigProperties( |
| NacosConfigProperties nacosConfigProperties) { |
| this.nacosConfigProperties = nacosConfigProperties; |
| return this; |
| } |
| |
| public static long getRefreshCount() { |
| return REFRESH_COUNT.get(); |
| } |
| |
| public static void refreshCountIncrement() { |
| REFRESH_COUNT.incrementAndGet(); |
| } |
| |
| } |