+ * 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. + *
+ * The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}. + *
+ * ${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 protocols
== null
, 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;
+ }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
index 54c981e8103..467810de0b2 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
@@ -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());
}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
index 0d1a0a9dd0f..677710048fe 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java
@@ -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) {
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
index 06b2dfa8f0e..4a97a24b6d7 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java
@@ -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 = ":";
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java
new file mode 100644
index 00000000000..28c53ccd06a
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.config.annotation.Reference;
+import com.alibaba.dubbo.config.annotation.Service;
+import com.alibaba.dubbo.config.spring.api.DemoService;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.mock.env.MockEnvironment;
+import org.springframework.util.ReflectionUtils;
+
+import static com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.GROUP;
+import static com.alibaba.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.VERSION;
+
+/**
+ * {@link AnnotationBeanNameBuilder} Test
+ *
+ * @see AnnotationBeanNameBuilder
+ * @since 2.6.6
+ */
+@Service(interfaceClass = DemoService.class, group = GROUP, version = VERSION,
+ application = "application", module = "module", registry = {"1", "2", "3"})
+public class AnnotationBeanNameBuilderTest {
+
+ @Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "${dubbo.version}",
+ application = "application", module = "module", registry = {"1", "2", "3"})
+ static final Class> INTERFACE_CLASS = DemoService.class;
+
+ static final String GROUP = "DUBBO";
+
+ static final String VERSION = "1.0.0";
+
+ private MockEnvironment environment;
+
+ @Before
+ public void prepare() {
+ environment = new MockEnvironment();
+ environment.setProperty("dubbo.version", "1.0.0");
+ }
+
+ @Test
+ public void testServiceAnnotation() {
+ Service service = AnnotationUtils.getAnnotation(AnnotationBeanNameBuilderTest.class, Service.class);
+ AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, INTERFACE_CLASS);
+ Assert.assertEquals("providers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
+ builder.build());
+
+ builder.environment(environment);
+ Assert.assertEquals("providers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
+ builder.build());
+ }
+
+ @Test
+ public void testReferenceAnnotation() {
+ Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(AnnotationBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class);
+ AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, INTERFACE_CLASS);
+ Assert.assertEquals("consumers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:${dubbo.version}:DUBBO",
+ builder.build());
+
+ builder.environment(environment);
+ Assert.assertEquals("consumers:dubbo:com.alibaba.dubbo.config.spring.api.DemoService:1.0.0:DUBBO",
+ builder.build());
+ }
+
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java
index 0003d699211..dcb4127cf53 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessorTest.java
@@ -229,7 +229,7 @@ public DemoService getDemoService() {
return demoService;
}
- @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345")
+ @Reference(version = "2.5.7", url = "dubbo://127.0.0.1:12345", protocol = "dubbo")
public void setDemoService(DemoService demoService) {
this.demoService = demoService;
}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java
index 84451bcaf4a..91b4b7043aa 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java
@@ -37,6 +37,7 @@
*/
@Service(interfaceClass = DemoService.class, group = GROUP, version = VERSION,
application = "application", module = "module", registry = {"1", "2", "3"})
+@Deprecated
public class ServiceBeanNameBuilderTest {
@Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "1.0.0",
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/EmbeddedZooKeeper.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/EmbeddedZooKeeper.java
new file mode 100644
index 00000000000..05c08e72e17
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/EmbeddedZooKeeper.java
@@ -0,0 +1,252 @@
+/*
+ * 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.multiple;
+
+
+import org.apache.zookeeper.server.ServerConfig;
+import org.apache.zookeeper.server.ZooKeeperServerMain;
+import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.SmartLifecycle;
+import org.springframework.util.ErrorHandler;
+import org.springframework.util.SocketUtils;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.Properties;
+import java.util.UUID;
+
+/**
+ * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
+ *
+ * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper. + *
+ * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for + * org.springframework.xd.dirt.server.singlenode.SingleNodeApplication + * + * @author Patrick Peralta + * @author Mark Fisher + * @author David Turanski + */ +public class EmbeddedZooKeeper implements SmartLifecycle { + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class); + + /** + * ZooKeeper client port. This will be determined dynamically upon startup. + */ + private final int clientPort; + + /** + * Whether to auto-start. Default is true. + */ + private boolean autoStartup = true; + + /** + * Lifecycle phase. Default is 0. + */ + private int phase = 0; + + /** + * Thread for running the ZooKeeper server. + */ + private volatile Thread zkServerThread; + + /** + * ZooKeeper server. + */ + private volatile ZooKeeperServerMain zkServer; + + /** + * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. + */ + private ErrorHandler errorHandler; + + private boolean daemon = true; + + /** + * Construct an EmbeddedZooKeeper with a random port. + */ + public EmbeddedZooKeeper() { + clientPort = SocketUtils.findAvailableTcpPort(); + } + + /** + * Construct an EmbeddedZooKeeper with the provided port. + * + * @param clientPort port for ZooKeeper server to bind to + */ + public EmbeddedZooKeeper(int clientPort, boolean daemon) { + this.clientPort = clientPort; + this.daemon = daemon; + } + + /** + * Returns the port that clients should use to connect to this embedded server. + * + * @return dynamically determined client port + */ + public int getClientPort() { + return this.clientPort; + } + + /** + * Specify whether to start automatically. Default is true. + * + * @param autoStartup whether to start automatically + */ + public void setAutoStartup(boolean autoStartup) { + this.autoStartup = autoStartup; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAutoStartup() { + return this.autoStartup; + } + + /** + * Specify the lifecycle phase for the embedded server. + * + * @param phase the lifecycle phase + */ + public void setPhase(int phase) { + this.phase = phase; + } + + /** + * {@inheritDoc} + */ + @Override + public int getPhase() { + return this.phase; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isRunning() { + return (zkServerThread != null); + } + + /** + * Start the ZooKeeper server in a background thread. + *
+ * Register an error handler via {@link #setErrorHandler} in order to handle + * any exceptions thrown during startup or execution. + */ + @Override + public synchronized void start() { + if (zkServerThread == null) { + zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter"); + zkServerThread.setDaemon(daemon); + zkServerThread.start(); + } + } + + /** + * Shutdown the ZooKeeper server. + */ + @Override + public synchronized void stop() { + if (zkServerThread != null) { + // The shutdown method is protected...thus this hack to invoke it. + // This will log an exception on shutdown; see + // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details. + try { + Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown"); + shutdown.setAccessible(true); + shutdown.invoke(zkServer); + } catch (Exception e) { + throw new RuntimeException(e); + } + + // It is expected that the thread will exit after + // the server is shutdown; this will block until + // the shutdown is complete. + try { + zkServerThread.join(5000); + zkServerThread = null; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.warn("Interrupted while waiting for embedded ZooKeeper to exit"); + // abandoning zk thread + zkServerThread = null; + } + } + } + + /** + * Stop the server if running and invoke the callback when complete. + */ + @Override + public void stop(Runnable callback) { + stop(); + callback.run(); + } + + /** + * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none + * is provided, only error-level logging will occur. + * + * @param errorHandler the {@link ErrorHandler} to be invoked + */ + public void setErrorHandler(ErrorHandler errorHandler) { + this.errorHandler = errorHandler; + } + + /** + * Runnable implementation that starts the ZooKeeper server. + */ + private class ServerRunnable implements Runnable { + + @Override + public void run() { + try { + Properties properties = new Properties(); + File file = new File(System.getProperty("java.io.tmpdir") + + File.separator + UUID.randomUUID()); + file.deleteOnExit(); + properties.setProperty("dataDir", file.getAbsolutePath()); + properties.setProperty("clientPort", String.valueOf(clientPort)); + + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + quorumPeerConfig.parseProperties(properties); + + zkServer = new ZooKeeperServerMain(); + ServerConfig configuration = new ServerConfig(); + configuration.readFrom(quorumPeerConfig); + + zkServer.runFromConfig(configuration); + } catch (Exception e) { + if (errorHandler != null) { + errorHandler.handleError(e); + } else { + logger.error("Exception running embedded ZooKeeper", e); + } + } + } + } + +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java new file mode 100644 index 00000000000..2eeb073b49c --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/consumer/MultipleProtocolsServiceConsumer.java @@ -0,0 +1,62 @@ +/* + * 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.multiple.consumer; + +import com.alibaba.dubbo.config.annotation.Reference; +import com.alibaba.dubbo.config.spring.api.HelloService; +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +/** + * Multiple Protocols Test + */ + +public class MultipleProtocolsServiceConsumer { + + @EnableDubbo + @PropertySource({ + "classpath:/META-INF/multiple-protocols-consumer.properties", + "classpath:/META-INF/dubbo-common.properties" + }) + @Configuration + static class ConsumerConfiguration { + + @Reference(version = "${hello.service.version}", protocol = "dubbo") + private HelloService dubboHelloService; + +// @Reference(version = "${hello.service.version}", protocol = "rest") +// private HelloService restHelloService; + + } + + public static void main(String[] args) { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(ConsumerConfiguration.class); + context.refresh(); + + ConsumerConfiguration configuration = context.getBean(ConsumerConfiguration.class); + System.out.println(configuration.dubboHelloService.sayHello("mercyblitz")); +// System.out.println(configuration.restHelloService.sayHello("mercyblitz")); + + context.close(); + } + +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java new file mode 100644 index 00000000000..60c1dea0201 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/DefaultHelloService.java @@ -0,0 +1,36 @@ +/* + * 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.multiple.provider; + +import com.alibaba.dubbo.config.annotation.Service; +import com.alibaba.dubbo.config.spring.api.HelloService; +import com.alibaba.dubbo.rpc.RpcContext; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +@Service(version = "${hello.service.version}", protocol = {"dubbo"}) +@Path("/hello-service") +public class DefaultHelloService implements HelloService { + + @Override + @GET + public String sayHello(@QueryParam("name") String name) { + return String.format("[%s] Hello , %s", RpcContext.getContext().getUrl(), name); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java new file mode 100644 index 00000000000..8e3981a499f --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/java/com/alibaba/dubbo/config/spring/beans/factory/annotation/multiple/provider/MultipleProtocolsServiceProvider.java @@ -0,0 +1,51 @@ +/* + * 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.multiple.provider; + +import com.alibaba.dubbo.config.spring.beans.factory.annotation.multiple.EmbeddedZooKeeper; +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.PropertySource; + +import java.io.IOException; + +/** + * Multiple Protocols Service Provider + */ +@EnableDubbo +@PropertySource({ + "classpath:/META-INF/multiple-protocols-provider.properties", + "classpath:/META-INF/dubbo-common.properties" +}) +public class MultipleProtocolsServiceProvider { + + public static void main(String[] args) throws IOException { + EmbeddedZooKeeper embeddedZooKeeper = new EmbeddedZooKeeper(2181, false); + embeddedZooKeeper.start(); + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.register(MultipleProtocolsServiceProvider.class); + context.refresh(); + + System.out.println("Enter any key to close the application"); + System.in.read(); + + context.close(); + embeddedZooKeeper.stop(); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-common.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-common.properties new file mode 100644 index 00000000000..36146a165b7 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/dubbo-common.properties @@ -0,0 +1,2 @@ +dubbo.registry.address=zookeeper://127.0.0.1:2181 +hello.service.version=1.0.0 \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-consumer.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-consumer.properties new file mode 100644 index 00000000000..abbe51ce929 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-consumer.properties @@ -0,0 +1 @@ +dubbo.application.id=provider \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-provider.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-provider.properties new file mode 100644 index 00000000000..98790494336 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/multiple-protocols-provider.properties @@ -0,0 +1,6 @@ +dubbo.application.id=provider +dubbo.protocols.dubbo.name=dubbo +dubbo.protocols.dubbo.port=12345 +dubbo.protocols.rest.name=rest +dubbo.protocols.rest.port=9090 +dubbo.protocols.rest.server=netty \ No newline at end of file