Skip to content

Commit

Permalink
Nacos registry enhancement & register reference bean (apache#4454)
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz authored and chickenlj committed Jul 4, 2019
1 parent b30b015 commit 1bf5fce
Show file tree
Hide file tree
Showing 21 changed files with 622 additions and 61 deletions.
4 changes: 2 additions & 2 deletions dubbo-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<url>https://github.com/apache/dubbo</url>
<connection>scm:git:https://github.com/apache/dubbo.git</connection>
<developerConnection>scm:git:https://github.com/apache/dubbo.git</developerConnection>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>
<mailingLists>
<mailingList>
<name>Development List</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static Proxy getProxy(ClassLoader cl, Class<?>... ics) {
String key = sb.toString();

// get cache by class loader.
Map<String, Object> cache;
final Map<String, Object> cache;
synchronized (PROXY_CACHE_MAP) {
cache = PROXY_CACHE_MAP.computeIfAbsent(cl, k -> new HashMap<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public T getExtension(String name) {
if ("true".equals(name)) {
return getDefaultExtension();
}
Holder<Object> holder = getOrCreateHolder(name);
final Holder<Object> holder = getOrCreateHolder(name);
Object instance = holder.get();
if (instance == null) {
synchronized (holder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
import java.util.regex.Pattern;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_IP_TO_BIND;

/**
* IP and Port Helper for RPC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Reference
*
* @export
* @since 2.7.0
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
Expand Down Expand Up @@ -228,32 +229,35 @@
String[] parameters() default {};

/**
* Application spring bean name
* Application associated name
*/
String application() default "";

/**
* Module spring bean name
* Module associated name
*/
String module() default "";

/**
* Consumer spring bean name
* Consumer associated name
*/
String consumer() default "";

/**
* Monitor spring bean name
* Monitor associated name
*/
String monitor() default "";

/**
* Registry spring bean name
* Registry associated name
*/
String[] registry() default {};

/**
* Protocol spring bean names
* The communication protocol of Dubbo Service
*
* @return the default value is ""
* @since 2.6.6
*/
String protocol() default "";

Expand All @@ -264,7 +268,16 @@

/**
* methods support
*
* @return
*/
Method[] methods() default {};

/**
* The id
*
* @return default value is empty
* @since 2.7.3
*/
String id() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand Down Expand Up @@ -91,7 +92,12 @@ public abstract class AnnotationInjectedBeanPostProcessor extends

private ClassLoader classLoader;

private int order = Ordered.LOWEST_PRECEDENCE;
/**
* make sure higher priority than {@link AutowiredAnnotationBeanPostProcessor}
*
* @revision 2.7.3
*/
private int order = Ordered.LOWEST_PRECEDENCE - 3;

/**
* @param annotationTypes the multiple types of {@link Annotation annotations}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.InjectionMetadata;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
Expand All @@ -43,6 +46,8 @@
import java.util.concurrent.ConcurrentMap;

import static org.apache.dubbo.config.spring.beans.factory.annotation.ServiceBeanNameBuilder.create;
import static org.apache.dubbo.config.spring.util.AnnotationUtils.getAttribute;
import static org.springframework.util.StringUtils.hasText;

/**
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
Expand Down Expand Up @@ -120,13 +125,96 @@ protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean,

String referencedBeanName = buildReferencedBeanName(attributes, injectedType);

ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referencedBeanName, attributes, injectedType, getClassLoader());
ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referencedBeanName, attributes, injectedType);

registerReferenceBean(referencedBeanName, referenceBean, attributes, injectedType);

cacheInjectedReferenceBean(referenceBean, injectedElement);

return buildProxy(referencedBeanName, referenceBean, injectedType);
}

/**
* Register an instance of {@link ReferenceBean} as a Spring Bean
*
* @param referencedBeanName the referenced bean name
* @param referenceBean the instance of {@link ReferenceBean}
* @param attributes the {@link AnnotationAttributes attributes} of {@link Reference @Reference}
* @param interfaceClass the {@link Class class} of Service interface
* @since 2.7.3
*/
private void registerReferenceBean(String referencedBeanName, ReferenceBean referenceBean,
AnnotationAttributes attributes,
Class<?> interfaceClass) {

ConfigurableListableBeanFactory beanFactory = getBeanFactory();

String beanName = getReferenceBeanName(attributes, interfaceClass);

if (beanFactory.containsBean(referencedBeanName)) { // If @Service bean is local one
/**
* Get the @Service's BeanDefinition from {@link BeanFactory}
* Refer to {@link ServiceAnnotationBeanPostProcessor#buildServiceBeanDefinition}
*/
AbstractBeanDefinition beanDefinition = (AbstractBeanDefinition) beanFactory.getBeanDefinition(referencedBeanName);
RuntimeBeanReference runtimeBeanReference = (RuntimeBeanReference) beanDefinition.getPropertyValues().get("ref");
// The name of bean annotated @Service
String serviceBeanName = runtimeBeanReference.getBeanName();
// register Alias rather than a new bean name, in order to reduce duplicated beans
beanFactory.registerAlias(serviceBeanName, beanName);
} else { // Remote @Service Bean
if (!beanFactory.containsBean(beanName)) {
beanFactory.registerSingleton(beanName, referenceBean);
}
}
}

/**
* Get the bean name of {@link ReferenceBean} if {@link Reference#id() id attribute} is present,
* or {@link #generateReferenceBeanName(AnnotationAttributes, Class) generate}.
*
* @param attributes the {@link AnnotationAttributes attributes} of {@link Reference @Reference}
* @param interfaceClass the {@link Class class} of Service interface
* @return non-null
* @since 2.7.3
*/
private String getReferenceBeanName(AnnotationAttributes attributes, Class<?> interfaceClass) {
// id attribute appears since 2.7.3
String beanName = getAttribute(attributes, "id");
if (!hasText(beanName)) {
beanName = generateReferenceBeanName(attributes, interfaceClass);
}
return beanName;
}

/**
* Build the bean name of {@link ReferenceBean}
*
* @param attributes the {@link AnnotationAttributes attributes} of {@link Reference @Reference}
* @param interfaceClass the {@link Class class} of Service interface
* @return
* @since 2.7.3
*/
private String generateReferenceBeanName(AnnotationAttributes attributes, Class<?> interfaceClass) {
StringBuilder beanNameBuilder = new StringBuilder("@Reference");

if (!attributes.isEmpty()) {
beanNameBuilder.append('(');
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
beanNameBuilder.append(entry.getKey())
.append('=')
.append(entry.getValue())
.append(',');
}
// replace the latest "," to be ")"
beanNameBuilder.setCharAt(beanNameBuilder.lastIndexOf(","), ')');
}

beanNameBuilder.append(" ").append(interfaceClass.getName());

return beanNameBuilder.toString();
}

private Object buildProxy(String referencedBeanName, ReferenceBean referenceBean, Class<?> injectedType) {
InvocationHandler handler = buildInvocationHandler(referencedBeanName, referenceBean);
return Proxy.newProxyInstance(getClassLoader(), new Class[]{injectedType}, handler);
Expand Down Expand Up @@ -199,7 +287,7 @@ private String buildReferencedBeanName(AnnotationAttributes attributes, Class<?>
}

private ReferenceBean buildReferenceBeanIfAbsent(String referencedBeanName, AnnotationAttributes attributes,
Class<?> referencedType, ClassLoader classLoader)
Class<?> referencedType)
throws Exception {

ReferenceBean<?> referenceBean = referenceBeanCache.get(referencedBeanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,27 @@ public void test() throws Exception {
Assert.assertNotNull(testBean.getDemoServiceFromAncestor());
Assert.assertNotNull(testBean.getDemoServiceFromParent());
Assert.assertNotNull(testBean.getDemoService());
Assert.assertNotNull(testBean.autowiredDemoService);

Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromAncestor().sayName("Mercy"));
Assert.assertEquals("Hello,Mercy", testBean.getDemoServiceFromParent().sayName("Mercy"));
Assert.assertEquals("Hello,Mercy", testBean.getDemoService().sayName("Mercy"));
Assert.assertEquals("Hello,Mercy", testBean.autowiredDemoService.sayName("Mercy"));

DemoService myDemoService = context.getBean("my-reference-bean", DemoService.class);

Assert.assertEquals("Hello,Mercy", myDemoService.sayName("Mercy"));

Map<String, DemoService> demoServicesMap = context.getBeansOfType(DemoService.class);

Assert.assertEquals(1, demoServicesMap.size());

for (DemoService demoService1 : demoServicesMap.values()) {

Assert.assertEquals(myDemoService, demoService1);

Assert.assertEquals("Hello,Mercy", demoService1.sayName("Mercy"));
}

}

Expand Down Expand Up @@ -194,7 +211,7 @@ public DemoService getDemoServiceFromAncestor() {
return demoServiceFromAncestor;
}

@Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345")
@Reference(id = "my-reference-bean", version = "2.5.7", url = "dubbo://127.0.0.1:12345")
public void setDemoServiceFromAncestor(DemoService demoServiceFromAncestor) {
this.demoServiceFromAncestor = demoServiceFromAncestor;
}
Expand Down Expand Up @@ -222,6 +239,9 @@ static class TestBean extends ParentBean {

private DemoService demoService;

@Autowired
private DemoService autowiredDemoService;

@Autowired
private ApplicationContext applicationContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public void testConsumer() {

Assertions.assertEquals("Hello,Mercy", value);

DemoService autowiredDemoService = consumerConfiguration.getAutowiredDemoService();

Assertions.assertEquals("Hello,Mercy", autowiredDemoService.sayName("Mercy"));


TestConsumerConfiguration.Child child = context.getBean(TestConsumerConfiguration.Child.class);

// From Child
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.config.spring.api.DemoService;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
Expand Down Expand Up @@ -63,6 +64,9 @@ public RegistryConfig registryConfig() {
return registryConfig;
}

@Autowired
private DemoService autowiredDemoService;

@Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345")
private DemoService demoService;

Expand Down Expand Up @@ -111,9 +115,17 @@ public void setDemoServiceFromParent(DemoService demoServiceFromParent) {

public static class Child extends Parent {

@Autowired
private DemoService demoService;

@Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345")
private DemoService demoServiceFromChild;


public DemoService getDemoService() {
return demoService;
}

public DemoService getDemoServiceFromChild() {
return demoServiceFromChild;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.config.spring.api.DemoService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
Expand All @@ -37,6 +38,13 @@ public class TestConsumerConfiguration {
@Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", application = "dubbo-demo-application")
private DemoService demoService;

@Autowired
private DemoService autowiredDemoService;

public DemoService getAutowiredDemoService() {
return autowiredDemoService;
}

public DemoService getDemoService() {
return demoService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public void test() {

Assertions.assertEquals("Hello,Mercy", value);

Assertions.assertEquals("Hello,Mercy", child.getDemoService().sayName("Mercy"));

// From Parent

demoService = child.getDemoServiceFromParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void doUnregister(URL url) {
}

@Override
public void doSubscribe(URL url, NotifyListener listener) {
public void doSubscribe(URL url, final NotifyListener listener) {
if (ANY_VALUE.equals(url.getServiceInterface())) {
admin = true;
}
Expand Down Expand Up @@ -324,7 +324,7 @@ protected void registered(URL url) {
}
urls.add(url);
List<URL> list = toList(urls);
for (NotifyListener listener : entry.getValue()) {
for (final NotifyListener listener : entry.getValue()) {
notify(key, listener, list);
synchronized (listener) {
listener.notify();
Expand Down
Loading

0 comments on commit 1bf5fce

Please sign in to comment.