NacosValue注解
NacosValue 定义在 nacos-api ⼯程中:com.alibaba.fig.annotation.NacosValue
注解解析在 nacos-spring-project ⼯程中:
com.alibaba.nacos.spring.util.NacosBeanUtils#registerNacosValueAnnotationBeanPostProcessor
com.alibaba.fig.NacosValueAnnotationBeanPostProcessor
实现 BeanPostProcessor 接⼝,可以在创建 bean 的过程中插⼊⾃定义逻辑,NacosValueAnnotationBeanPostProcessor 扫描属性和⽅法上的NacosValue 注解。
执⾏顺序:bean 的构造⽅法,postProcessBeforeInitialization,init,postProcessAfterInitialization
@Override
public Object postProcessBeforeInitialization(Object bean, final String beanName)
throws BeansException {
doWithFields(bean, beanName);
doWithMethods(bean, beanName);
return super.postProcessBeforeInitialization(bean, beanName);ttle
}
nacos 客户端定时从 nacos rver 拉取更新,如果有更新,则通过事件把新值注⼊到属性中。
com.alibaba.fig.impl.ClientWorker.LongPullingRunnable小学五年级英语
com.alibaba.fig.impl.CacheData#safeNotifyListener强奸英文
检测到变化
// 配置⽂件发⽣变化,把这种变化转化为 spring 事件
private void safeNotifyListener(final String dataId, final String group, final String content,
final String md5, final ManagerListenerWrap listenerWrap) {
final Listener listener = listenerWrap.listener;
// 把逻辑封装成 job,可异步可同步
Runnable job = new Runnable() {
public void run() {
ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader();
ClassLoader appClassLoader = Class().getClassLoader();
try {
if (listener instanceof AbstractSharedListener) {
AbstractSharedListener adapter = (AbstractSharedListener) listener;
adapter.fillContext(dataId, group);
LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5);
}
// 执⾏回调之前先将线程classloader设置为具体webapp的classloader,以免回调⽅法中调⽤spi接⼝是出现异常或错⽤(多应⽤部署才会有该问题)。
Thread.currentThread().tContextClassLoader(appClassLoader);
ConfigRespon cr = new ConfigRespon();
cr.tDataId(dataId);
cr.tGroup(group);
pant什么意思
cr.tContent(content);
configFilterChainManager.doFilter(null, cr);
String contentTmp = cr.getContent();
// 真正发布事件的地⽅
listenerWrap.lastCallMd5 = md5;
LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ", name, dataId, group, md5,
listener);
} catch (NacosException de) {
<("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} errCode={} errMsg={}", name,
dataId, group, md5, listener, de.getErrCode(), de.getErrMsg());
} catch (Throwable t) {
<("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId, group,
md5, listener, t.getCau());
} finally {
Thread.currentThread().tContextClassLoader(myClassLoader);
}
}
};
final long startNotify = System.currentTimeMillis();
try {
if (null != Executor()) {
} el {
job.run();
}
} catch (Throwable t) {
<("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} throwable={}", name, dataId, group,
md5, listener, t.getCau());
}负责人翻译
final long finishNotify = System.currentTimeMillis();
LOGGER.info("[{}] [notify-listener] time cost={}ms in ClientWorker, dataId={}, group={}, md5={}, listener={} ",
name, (finishNotify - startNotify), dataId, group, md5, listener);
}
发布事件
bridget
com.alibaba.fig.DelegatingEventPublishingListener#publishEvent DelegatingEventPublishingListener 类在 nacos-spring-context ⼯程中
private void publishEvent(String content) {doubt是什么意思
NacosConfigReceivedEvent event = new NacosConfigReceivedEvent(configService, dataId, groupId, content);
applicationEventPublisher.publishEvent(event);
}
消费事件
com.alibaba.fig.NacosValueAnnotationBeanPostProcessor#onApplicationEvent NacosValueAnnotationBeanPostProcessor 类在 nacos-spring-context ⼯程中
NacosValueAnnotationBeanPostProcessor 实现了 ApplicationListener<NacosConfigReceivedEvent> 接⼝,NacosConfigReceivedEvent 继承⾃ ApplicationEvent。
website design
public void onApplicationEvent(NacosConfigReceivedEvent event) {
国家地理下载String content = Content();
if (content != null) {
Properties configProperties = toProperties(content);
for (Object key : configProperties.keySet()) {
String propertyKey = (String)key;
List<NacosValueTarget> beanPropertyList = (propertyKey);
if (beanPropertyList == null) {
continue;
}
String propertyValue = Property(propertyKey);
for (NacosValueTarget nacosValueTarget : beanPropertyList) {
if (hod == null) {
tField(nacosValueTarget, propertyValue);
} el {
tMethod(nacosValueTarget, propertyValue);
}
in front of
}
}
}
}