Skip to content

Commit 7fefb81

Browse files
mercyblitzbeiwei30
authored andcommitted
Enhancement and Bugfix in 2.7.1 ( Part 3 ) (#3675)
* Polish /#1306 : @reference bean name conflict * Polish /#3582 : service register support on nacos * Polish /#3582 : Add license header and remove hard-code version * Polish /#3582 : Remove junit * Polish /#3582 : Translate to English
1 parent fcdf9c8 commit 7fefb81

File tree

20 files changed

+1288
-148
lines changed

20 files changed

+1288
-148
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.dubbo.config.spring.beans.factory.annotation;
18+
19+
import org.apache.dubbo.common.Constants;
20+
import org.apache.dubbo.config.annotation.Reference;
21+
import org.apache.dubbo.config.annotation.Service;
22+
import org.apache.dubbo.registry.Registry;
23+
24+
import org.springframework.core.env.Environment;
25+
26+
import static org.apache.dubbo.common.Constants.CONSUMERS_CATEGORY;
27+
import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL;
28+
import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY;
29+
import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName;
30+
import static org.springframework.util.StringUtils.arrayToCommaDelimitedString;
31+
import static org.springframework.util.StringUtils.hasText;
32+
33+
/**
34+
* The Bean Name Builder for the annotations {@link Service} and {@link Reference}
35+
* <p>
36+
* The naming rule is consistent with the the implementation {@link Registry} that is based on the service-name aware
37+
* infrastructure, e.g Spring Cloud, Cloud Native and so on.
38+
* <p>
39+
* The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}.
40+
* <p>
41+
* ${version} and ${group} are optional.
42+
*
43+
* @since 2.6.6
44+
*/
45+
class AnnotationBeanNameBuilder {
46+
47+
private static final String SEPARATOR = ":";
48+
49+
// Required properties
50+
51+
private final String category;
52+
53+
private final String protocol;
54+
55+
private final String interfaceClassName;
56+
57+
// Optional properties
58+
59+
private String version;
60+
61+
private String group;
62+
63+
private Environment environment;
64+
65+
private AnnotationBeanNameBuilder(String category, String protocol, String interfaceClassName) {
66+
this.category = category;
67+
this.protocol = protocol;
68+
this.interfaceClassName = interfaceClassName;
69+
}
70+
71+
private AnnotationBeanNameBuilder(Service service, Class<?> interfaceClass) {
72+
this(PROVIDERS_CATEGORY, resolveProtocol(service.protocol()), resolveInterfaceName(service, interfaceClass));
73+
this.group(service.group());
74+
this.version(service.version());
75+
}
76+
77+
private AnnotationBeanNameBuilder(Reference reference, Class<?> interfaceClass) {
78+
this(CONSUMERS_CATEGORY, resolveProtocol(reference.protocol()), resolveInterfaceName(reference, interfaceClass));
79+
this.group(reference.group());
80+
this.version(reference.version());
81+
}
82+
83+
public static AnnotationBeanNameBuilder create(Service service, Class<?> interfaceClass) {
84+
return new AnnotationBeanNameBuilder(service, interfaceClass);
85+
}
86+
87+
public static AnnotationBeanNameBuilder create(Reference reference, Class<?> interfaceClass) {
88+
return new AnnotationBeanNameBuilder(reference, interfaceClass);
89+
}
90+
91+
private static void append(StringBuilder builder, String value) {
92+
if (hasText(value)) {
93+
builder.append(SEPARATOR).append(value);
94+
}
95+
}
96+
97+
public AnnotationBeanNameBuilder group(String group) {
98+
this.group = group;
99+
return this;
100+
}
101+
102+
public AnnotationBeanNameBuilder version(String version) {
103+
this.version = version;
104+
return this;
105+
}
106+
107+
public AnnotationBeanNameBuilder environment(Environment environment) {
108+
this.environment = environment;
109+
return this;
110+
}
111+
112+
/**
113+
* Resolve the protocol
114+
*
115+
* @param protocols one or more protocols
116+
* @return if <code>protocols</code> == <code>null</code>, it will return
117+
* {@link Constants#DEFAULT_PROTOCOL "dubbo"} as the default protocol
118+
* @see Constants#DEFAULT_PROTOCOL
119+
*/
120+
private static String resolveProtocol(String... protocols) {
121+
String protocol = arrayToCommaDelimitedString(protocols);
122+
return hasText(protocol) ? protocol : DEFAULT_PROTOCOL;
123+
}
124+
125+
/**
126+
* Build bean name while resolve the placeholders if possible.
127+
*
128+
* @return pattern : ${category}:${protocol}:${serviceInterface}:${version}:${group}
129+
*/
130+
public String build() {
131+
// Append the required properties
132+
StringBuilder beanNameBuilder = new StringBuilder(category);
133+
append(beanNameBuilder, protocol);
134+
append(beanNameBuilder, interfaceClassName);
135+
// Append the optional properties
136+
append(beanNameBuilder, version);
137+
append(beanNameBuilder, group);
138+
String beanName = beanNameBuilder.toString();
139+
// Resolve placeholders
140+
return environment != null ? environment.resolvePlaceholders(beanName) : beanName;
141+
}
142+
}

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ protected String buildInjectedObjectCacheKey(Reference reference, Object bean, S
188188

189189
private String buildReferencedBeanName(Reference reference, Class<?> injectedType) {
190190

191-
ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment());
191+
AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, injectedType);
192+
193+
builder.environment(getEnvironment());
192194

193195
return getEnvironment().resolvePlaceholders(builder.build());
194196
}
@@ -261,4 +263,4 @@ public void destroy() throws Exception {
261263
this.injectedFieldReferenceBeanCache.clear();
262264
this.injectedMethodReferenceBeanCache.clear();
263265
}
264-
}
266+
}

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.apache.dubbo.common.logger.Logger;
2020
import org.apache.dubbo.common.logger.LoggerFactory;
21-
import org.apache.dubbo.config.MethodConfig;
2221
import org.apache.dubbo.config.annotation.Service;
2322
import org.apache.dubbo.config.spring.ServiceBean;
2423
import org.apache.dubbo.config.spring.context.annotation.DubboClassPathBeanDefinitionScanner;
@@ -290,8 +289,9 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean
290289
*/
291290
private String generateServiceBeanName(Service service, Class<?> interfaceClass, String annotatedServiceBeanName) {
292291

293-
ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, interfaceClass, environment);
292+
AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, interfaceClass);
294293

294+
builder.environment(environment);
295295

296296
return builder.build();
297297

@@ -316,8 +316,9 @@ private Class<?> resolveServiceInterfaceClass(Class<?> annotatedServiceBeanClass
316316
}
317317

318318
if (interfaceClass == null) {
319-
320-
Class<?>[] allInterfaces = annotatedServiceBeanClass.getInterfaces();
319+
// Find all interfaces from the annotated class
320+
// To resolve an issue : https://github.com/apache/incubator-dubbo/issues/3251
321+
Class<?>[] allInterfaces = ClassUtils.getAllInterfacesForClass(annotatedServiceBeanClass);
321322

322323
if (allInterfaces.length > 0) {
323324
interfaceClass = allInterfaces[0];
@@ -435,11 +436,6 @@ private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class
435436
builder.addPropertyValue("protocols", protocolRuntimeBeanReferences);
436437
}
437438

438-
List<MethodConfig> methodConfigs = MethodConfig.constructMethodConfig(service.methods());
439-
if (!methodConfigs.isEmpty()) {
440-
builder.addPropertyValue("methods", methodConfigs);
441-
}
442-
443439
return builder.getBeanDefinition();
444440

445441
}
@@ -490,4 +486,4 @@ public void setBeanClassLoader(ClassLoader classLoader) {
490486
this.classLoader = classLoader;
491487
}
492488

493-
}
489+
}

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)