Skip to content

Commit

Permalink
Merge pull request #150 from nacos-group/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
chuntaojun authored Sep 15, 2019
2 parents e412be2 + 8859505 commit b64cc9f
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 104 deletions.
4 changes: 2 additions & 2 deletions nacos-spring-context/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<parent>
<artifactId>nacos-spring-parent</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>0.3.3</version>
<version>0.3.4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.3.3</version>
<version>0.3.4</version>
<name>Alibaba Nacos :: Spring :: Context</name>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ protected void processListenerMethod(String beanName, final Object bean, Class<?
ConfigService configService = configServiceBeanBuilder.build(listener.properties());

try {

configService.addListener(dataId, groupId, new TimeoutNacosConfigListener(dataId, groupId, timeout) {

@Override
Expand All @@ -112,9 +111,7 @@ protected void onReceived(String config) {
}
});
} catch (NacosException e) {
if (logger.isErrorEnabled()) {
logger.error("ConfigService can't add Listener for dataId : " + dataId + " , groupId : " + groupId, e);
}
logger.error("ConfigService can't add Listener for dataId : " + dataId + " , groupId : " + groupId, e);
}

publishMetadataEvent(beanName, bean, beanClass, dataId, groupId, listener, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.spring.context.annotation.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.client.config.utils.MD5;
import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
import com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor;
import org.slf4j.Logger;
Expand All @@ -28,7 +29,9 @@
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.MethodParameter;
import org.springframework.core.env.Environment;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
Expand All @@ -40,6 +43,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static com.alibaba.nacos.spring.util.NacosUtils.toProperties;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
Expand All @@ -52,7 +56,7 @@
* @since 0.1.0
*/
public class NacosValueAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor<NacosValue>
implements BeanFactoryAware, ApplicationListener<NacosConfigReceivedEvent> {
implements BeanFactoryAware, EnvironmentAware, ApplicationListener<NacosConfigReceivedEvent> {

private final Logger logger = LoggerFactory.getLogger(getClass());

Expand All @@ -71,10 +75,12 @@ public class NacosValueAnnotationBeanPostProcessor extends AnnotationInjectedBea
* placeholder, nacosValueTarget
*/
private Map<String, List<NacosValueTarget>> placeholderNacosValueTargetMap
= new HashMap<String, List<NacosValueTarget>>();
= new HashMap<String, List<NacosValueTarget>>();

private ConfigurableListableBeanFactory beanFactory;

private Environment environment;

@Override
protected Object doGetInjectedBean(NacosValue annotation, Object bean, String beanName, Class<?> injectedType,
InjectionMetadata.InjectedElement injectedElement) {
Expand All @@ -83,11 +89,11 @@ protected Object doGetInjectedBean(NacosValue annotation, Object bean, String be

Member member = injectedElement.getMember();
if (member instanceof Field) {
return convertIfNecessary((Field)member, value);
return convertIfNecessary((Field) member, value);
}

if (member instanceof Method) {
return convertIfNecessary((Method)member, value);
return convertIfNecessary((Method) member, value);
}

return null;
Expand All @@ -104,14 +110,19 @@ protected String buildInjectedObjectCacheKey(NacosValue annotation, Object bean,
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
"NacosValueAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory");
"NacosValueAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory");
}
this.beanFactory = (ConfigurableListableBeanFactory)beanFactory;
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}

@Override
public Object postProcessBeforeInitialization(Object bean, final String beanName)
throws BeansException {
throws BeansException {

doWithFields(bean, beanName);

Expand All @@ -122,24 +133,25 @@ public Object postProcessBeforeInitialization(Object bean, final String beanName

@Override
public void onApplicationEvent(NacosConfigReceivedEvent event) {
String content = event.getContent();
if (content != null) {
Properties configProperties = toProperties(event.getDataId(), event.getGroupId(), content, event.getType());

for (Object key : configProperties.keySet()) {
String propertyKey = (String)key;

List<NacosValueTarget> beanPropertyList = placeholderNacosValueTargetMap.get(propertyKey);
if (beanPropertyList == null) {
continue;
}

String propertyValue = configProperties.getProperty(propertyKey);
for (NacosValueTarget nacosValueTarget : beanPropertyList) {
if (nacosValueTarget.method == null) {
setField(nacosValueTarget, propertyValue);
// In to this event receiver, the environment has been updated the
// latest configuration information, pull directly from the environment
// fix issue #142
for (Map.Entry<String, List<NacosValueTarget>> entry : placeholderNacosValueTargetMap.entrySet()) {
String key = environment.resolvePlaceholders(entry.getKey());
String newValue = environment.getProperty(key);
if (newValue == null) {
continue;
}
List<NacosValueTarget> beanPropertyList = entry.getValue();
for (NacosValueTarget target : beanPropertyList) {
String md5String = MD5.getInstance().getMD5String(newValue);
boolean isUpdate = !target.lastMD5.equals(md5String);
if (isUpdate) {
target.updateLastMD5(md5String);
if (target.method == null) {
setField(target, newValue);
} else {
setMethod(nacosValueTarget, propertyValue);
setMethod(target, newValue);
}
}
}
Expand Down Expand Up @@ -250,13 +262,13 @@ private void setMethod(NacosValueTarget nacosValueTarget, String propertyValue)

if (logger.isDebugEnabled()) {
logger.debug("Update value with {} (method) in {} (bean) with {}",
method.getName(), nacosValueTarget.beanName, propertyValue);
method.getName(), nacosValueTarget.beanName, propertyValue);
}
} catch (Throwable e) {
if (logger.isErrorEnabled()) {
logger.error(
"Can't update value with " + method.getName() + " (method) in "
+ nacosValueTarget.beanName + " (bean)", e);
"Can't update value with " + method.getName() + " (method) in "
+ nacosValueTarget.beanName + " (bean)", e);
}
}
}
Expand All @@ -274,26 +286,28 @@ private void setField(final NacosValueTarget nacosValueTarget, final String prop

if (logger.isDebugEnabled()) {
logger.debug("Update value of the {}" + " (field) in {} (bean) with {}",
fieldName, nacosValueTarget.beanName, propertyValue);
fieldName, nacosValueTarget.beanName, propertyValue);
}
} catch (Throwable e) {
if (logger.isErrorEnabled()) {
logger.error(
"Can't update value of the " + fieldName + " (field) in "
+ nacosValueTarget.beanName + " (bean)", e);
"Can't update value of the " + fieldName + " (field) in "
+ nacosValueTarget.beanName + " (bean)", e);
}
}
}

private static class NacosValueTarget {

private Object bean;
private final Object bean;

private String beanName;
private final String beanName;

private Method method;
private final Method method;

private Field field;
private final Field field;

private String lastMD5;

NacosValueTarget(Object bean, String beanName, Method method, Field field) {
this.bean = bean;
Expand All @@ -303,7 +317,14 @@ private static class NacosValueTarget {
this.method = method;

this.field = field;

this.lastMD5 = "";
}

protected void updateLastMD5(String newMD5) {
this.lastMD5 = newMD5;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public Executor getExecutor() {
*/
@Override
public void receiveConfigInfo(String content) {
publishEvent(content);
onReceived(content);
publishEvent(content);
}

private void publishEvent(String content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public void addListener(String dataId, String group, String type, Listener liste
public void addListener(String dataId, String group, Listener listener) throws NacosException {
configService.addListener(dataId, group, listener);
publishEvent(new NacosConfigListenerRegisteredEvent(configService, dataId, group, listener, true));

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
*/
public abstract class TimeoutNacosConfigListener extends AbstractListener {

static AtomicInteger id = new AtomicInteger(0);
private static AtomicInteger id = new AtomicInteger(0);

static ExecutorService executorService = Executors.newScheduledThreadPool(8, new ThreadFactory() {
private static ExecutorService executorService = Executors.newScheduledThreadPool(8, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
Expand Down Expand Up @@ -75,10 +75,8 @@ public void run() {
throw new RuntimeException(e);
} catch (TimeoutException e) {
future.cancel(true);
if (logger.isWarnEnabled()) {
logger.warn("Listening on Nacos Config exceeds timeout {} ms " +
"[dataId : {}, groupId : {}, data : {}]", timeout, dataId, groupId, content);
}
logger.warn("Listening on Nacos Config exceeds timeout {} ms " +
"[dataId : {}, groupId : {}, data : {}]", timeout, dataId, groupId, content);
}
}

Expand Down
Loading

0 comments on commit b64cc9f

Please sign in to comment.