Skip to content

Commit

Permalink
Polish apache#3355 : Still exists the issues in service discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 27, 2019
1 parent 8ff4f37 commit ebeb1dd
Show file tree
Hide file tree
Showing 15 changed files with 731 additions and 5 deletions.
86 changes: 86 additions & 0 deletions dubbo-config/dubbo-config-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,83 +52,169 @@
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<!-- Netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<scope>test</scope>
</dependency>

<!-- REST support dependencies -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-netty4</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-default</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-monitor-default</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-rest</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-rmi</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-injvm</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-remoting-netty</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-serialization-hessian2</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<scope>test</scope>
</dependency>

<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.config.spring.beans.factory.annotation;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.registry.Registry;

import org.springframework.core.env.Environment;

import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY;
import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;
import static com.alibaba.dubbo.common.Constants.PROVIDERS_CATEGORY;
import static com.alibaba.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName;
import static org.springframework.util.StringUtils.arrayToCommaDelimitedString;
import static org.springframework.util.StringUtils.hasText;

/**
* The Bean Name Builder for the annotations {@link Service} and {@link Reference}
* <p>
* The naming rule is consistent with the the implementation {@link Registry} that is based on the service-name aware
* infrastructure, e.g Spring Cloud, Cloud Native and so on.
* <p>
* The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}.
* <p>
* ${version} and ${group} are optional.
*
* @since 2.6.6
*/
class AnnotationBeanNameBuilder {

private static final String SEPARATOR = ":";

// Required properties

private final String category;

private final String protocol;

private final String interfaceClassName;

// Optional properties

private String version;

private String group;

private Environment environment;

private AnnotationBeanNameBuilder(String category, String protocol, String interfaceClassName) {
this.category = category;
this.protocol = protocol;
this.interfaceClassName = interfaceClassName;
}

private AnnotationBeanNameBuilder(Service service, Class<?> interfaceClass) {
this(PROVIDERS_CATEGORY, resolveProtocol(service.protocol()), resolveInterfaceName(service, interfaceClass));
this.group(service.group());
this.version(service.version());
}

private AnnotationBeanNameBuilder(Reference reference, Class<?> interfaceClass) {
this(CONSUMERS_CATEGORY, resolveProtocol(reference.protocol()), resolveInterfaceName(reference, interfaceClass));
this.group(reference.group());
this.version(reference.version());
}

public static AnnotationBeanNameBuilder create(Service service, Class<?> interfaceClass) {
return new AnnotationBeanNameBuilder(service, interfaceClass);
}

public static AnnotationBeanNameBuilder create(Reference reference, Class<?> interfaceClass) {
return new AnnotationBeanNameBuilder(reference, interfaceClass);
}

private static void append(StringBuilder builder, String value) {
if (hasText(value)) {
builder.append(SEPARATOR).append(value);
}
}

public AnnotationBeanNameBuilder group(String group) {
this.group = group;
return this;
}

public AnnotationBeanNameBuilder version(String version) {
this.version = version;
return this;
}

public AnnotationBeanNameBuilder environment(Environment environment) {
this.environment = environment;
return this;
}

/**
* Resolve the protocol
*
* @param protocols one or more protocols
* @return if <code>protocols</code> == <code>null</code>, it will return
* {@link Constants#DEFAULT_PROTOCOL "dubbo"} as the default protocol
* @see Constants#DEFAULT_PROTOCOL
*/
private static String resolveProtocol(String... protocols) {
String protocol = arrayToCommaDelimitedString(protocols);
return hasText(protocol) ? protocol : DEFAULT_PROTOCOL;
}

/**
* Build bean name while resolve the placeholders if possible.
*
* @return pattern : ${category}:${protocol}:${serviceInterface}:${version}:${group}
*/
public String build() {
// Append the required properties
StringBuilder beanNameBuilder = new StringBuilder(category);
append(beanNameBuilder, protocol);
append(beanNameBuilder, interfaceClassName);
// Append the optional properties
append(beanNameBuilder, version);
append(beanNameBuilder, group);
String beanName = beanNameBuilder.toString();
// Resolve placeholders
return environment != null ? environment.resolvePlaceholders(beanName) : beanName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,16 @@ protected String buildInjectedObjectCacheKey(Reference reference, Object bean, S

String key = buildReferencedBeanName(reference, injectedType) +
"#source=" + (injectedElement.getMember()) +
"#attributes=" + AnnotationUtils.getAttributes(reference,getEnvironment(),true);
"#attributes=" + AnnotationUtils.getAttributes(reference, getEnvironment(), true);

return key;
}

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

ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment());
AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, injectedType);

builder.environment(getEnvironment());

return getEnvironment().resolvePlaceholders(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean
*/
private String generateServiceBeanName(Service service, Class<?> interfaceClass, String annotatedServiceBeanName) {

ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, interfaceClass, environment);
AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, interfaceClass);

builder.environment(environment);

return builder.build();

}

private Class<?> resolveServiceInterfaceClass(Class<?> annotatedServiceBeanClass, Service service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
* @see ServiceBean
* @see ReferenceBean
* @since 2.6.5
* @deprecated {@link AnnotationBeanNameBuilder} as the replacement
*/
@Deprecated
class ServiceBeanNameBuilder {

private static final String SEPARATOR = ":";
Expand Down
Loading

0 comments on commit ebeb1dd

Please sign in to comment.