NacosValue 注解

NacosValue 定义在 nacos-api 工程中:com.alibaba.nacos.api.config.annotation.NacosValue

注解解析在 nacos-spring-project 工程中:

com.alibaba.nacos.spring.util.NacosBeanUtils#registerNacosValueAnnotationBeanPostProcessor

com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor

实现 BeanPostProcessor 接口,可以在创建 bean 的过程中插入自定义逻辑,NacosValueAnnotationBeanPostProcessor 扫描属性和方法上的 NacosValue 注解。

执行顺序:bean 的构造方法,postProcessBeforeInitialization,init,postProcessAfterInitialization

1@Override 2public Object postProcessBeforeInitialization(Object bean, final String beanName) 3 throws BeansException { 4 5 doWithFields(bean, beanName); 6 7 doWithMethods(bean, beanName); 8 9 return super.postProcessBeforeInitialization(bean, beanName); 10}

nacos 客户端定时从 nacos server 拉取更新,如果有更新,则通过事件把新值注入到属性中。

com.alibaba.nacos.client.config.impl.ClientWorker.LongPullingRunnable
com.alibaba.nacos.client.config.impl.CacheData#safeNotifyListener
检测到变化

 

1// 配置文件发生变化,把这种变化转化为 spring 事件 2private void safeNotifyListener(final String dataId, final String group, final String content, 3 final String md5, final ManagerListenerWrap listenerWrap) { 4 final Listener listener = listenerWrap.listener; 5 6 // 把逻辑封装成 job,可异步可同步 7 Runnable job = new Runnable() { 8 public void run() { 9 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); 10 ClassLoader appClassLoader = listener.getClass().getClassLoader(); 11 try { 12 if (listener instanceof AbstractSharedListener) { 13 AbstractSharedListener adapter = (AbstractSharedListener) listener; 14 adapter.fillContext(dataId, group); 15 LOGGER.info("[{}] [notify-context] dataId={}, group={}, md5={}", name, dataId, group, md5); 16 } 17 // 执行回调之前先将线程classloader设置为具体webapp的classloader,以免回调方法中调用spi接口是出现异常或错用(多应用部署才会有该问题)。 18 Thread.currentThread().setContextClassLoader(appClassLoader); 19 20 ConfigResponse cr = new ConfigResponse(); 21 cr.setDataId(dataId); 22 cr.setGroup(group); 23 cr.setContent(content); 24 configFilterChainManager.doFilter(null, cr); 25 String contentTmp = cr.getContent(); 26 // 真正发布事件的地方 27 listener.receiveConfigInfo(contentTmp); 28 listenerWrap.lastCallMd5 = md5; 29 LOGGER.info("[{}] [notify-ok] dataId={}, group={}, md5={}, listener={} ", name, dataId, group, md5, 30 listener); 31 } catch (NacosException de) { 32 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} errCode={} errMsg={}", name, 33 dataId, group, md5, listener, de.getErrCode(), de.getErrMsg()); 34 } catch (Throwable t) { 35 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} tx={}", name, dataId, group, 36 md5, listener, t.getCause()); 37 } finally { 38 Thread.currentThread().setContextClassLoader(myClassLoader); 39 } 40 } 41 }; 42 43 final long startNotify = System.currentTimeMillis(); 44 try { 45 if (null != listener.getExecutor()) { 46 listener.getExecutor().execute(job); 47 } else { 48 job.run(); 49 } 50 } catch (Throwable t) { 51 LOGGER.error("[{}] [notify-error] dataId={}, group={}, md5={}, listener={} throwable={}", name, dataId, group, 52 md5, listener, t.getCause()); 53 } 54 final long finishNotify = System.currentTimeMillis(); 55 LOGGER.info("[{}] [notify-listener] time cost={}ms in ClientWorker, dataId={}, group={}, md5={}, listener={} ", 56 name, (finishNotify - startNotify), dataId, group, md5, listener); 57}

发布事件

com.alibaba.nacos.spring.context.event.config.DelegatingEventPublishingListener#publishEvent

DelegatingEventPublishingListener 类在 nacos-spring-context 工程中

1private void publishEvent(String content) { 2 NacosConfigReceivedEvent event = new NacosConfigReceivedEvent(configService, dataId, groupId, content); 3 applicationEventPublisher.publishEvent(event); 4}

消费事件

com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor#onApplicationEvent

NacosValueAnnotationBeanPostProcessor 类在 nacos-spring-context 工程中

NacosValueAnnotationBeanPostProcessor 实现了 ApplicationListener<NacosConfigReceivedEvent> 接口,NacosConfigReceivedEvent 继承自 ApplicationEvent。

1public void onApplicationEvent(NacosConfigReceivedEvent event) { 2 String content = event.getContent(); 3 if (content != null) { 4 Properties configProperties = toProperties(content); 5 6 for (Object key : configProperties.keySet()) { 7 String propertyKey = (String)key; 8 9 List<NacosValueTarget> beanPropertyList = placeholderNacosValueTargetMap.get(propertyKey); 10 if (beanPropertyList == null) { 11 continue; 12 } 13 14 String propertyValue = configProperties.getProperty(propertyKey); 15 for (NacosValueTarget nacosValueTarget : beanPropertyList) { 16 if (nacosValueTarget.method == null) { 17 setField(nacosValueTarget, propertyValue); 18 } else { 19 setMethod(nacosValueTarget, propertyValue); 20 } 21 } 22 } 23 } 24}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )