From 289c9e50b169eaa4682954fcec15335535f1bd1f Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Wed, 5 Sep 2018 14:45:39 +0800 Subject: [PATCH 1/2] update README --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5d2f68e68c..3ed5d8d85b0 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ We are now collecting dubbo user info in order to help us to improve dubbo bette ## Getting started +The following code snippet comes from [Dubbo Samples](https://github.com/dubbo/dubbo-samples/tree/master/dubbo-samples-api). You may clone the sample project and step into `dubbo-samples-api` sub directory before read on. + +```bash +# git clone https://github.com/dubbo/dubbo-samples.git +# cd dubbo-samples/dubbo-samples-api +``` + +There's one [README](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/README.md) file under `dubbo-samples-api` directory. Read it and try this sample out by following the instructions. + ### Maven dependency ```xml @@ -35,17 +44,19 @@ We are now collecting dubbo user info in order to help us to improve dubbo bette ``` -### Defining service interfaces +### Define service interfaces ```java -package org.apache.dubbo.demo; +package org.apache.dubbo.demo.api; public interface GreetingService { String sayHello(String name); } ``` -### Implement interface in service provider +*See [api/GreetingService.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/api/GreetingsService.java) on GitHub.* + +### Implement service interface for the provider ```java package org.apache.dubbo.demo.provider; @@ -59,7 +70,9 @@ public class GreetingServiceImpl implements GreetingService { } ``` -### Starting service provider +*See [provider/GreetingServiceImpl.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/server/GreetingsServiceImpl.java) on GitHub.* + +### Start service provider ```java package org.apache.dubbo.demo.provider; @@ -71,7 +84,7 @@ import org.apache.dubbo.demo.GreetingService; import java.io.IOException; -public class Provider { +public class Application { public static void main(String[] args) throws IOException { ServiceConfig serviceConfig = new ServiceConfig(); @@ -85,6 +98,15 @@ public class Provider { } ``` +*See [provider/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/provider/Application.java) on GitHub.* + +### Build and run the provider + +```bash +# mvn clean package +# mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.demo.provider.Application exec:java +``` + ### Call remote service in consumer ```java @@ -95,7 +117,7 @@ import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.RegistryConfig; import org.apache.dubbo.demo.GreetingService; -public class Consumer { +public class Application { public static void main(String[] args) { ReferenceConfig referenceConfig = new ReferenceConfig(); referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer")); @@ -107,8 +129,20 @@ public class Consumer { } ``` +### Build and run the consumer + +```bash +# mvn clean package +# mvn -Djava.net.preferIPv4Stack=true -Dexec.mainClass=org.apache.dubbo.demo.consumer.Application exec:java +``` + +The consumer will print out `Hello world` on the screen. + +*See [consumer/Application.java](https://github.com/dubbo/dubbo-samples/blob/master/dubbo-samples-api/src/main/java/com/alibaba/dubbo/samples/consumer/Application.java) on GitHub.* + ### Next steps +* [Your first Dubbo application](http://dubbo.apache.org/en-us/blog/dubbo-101.html) - A 101 tutorial to reveal more details, with the same code above. * [Dubbo user manual](http://dubbo.apache.org/en-us/docs/user/preface/background.html) - How to use Dubbo and all its features. * [Dubbo developer guide](http://dubbo.apache.org/en-us/docs/dev/build.html) - How to invovle in Dubbo development. * [Dubbo admin manual](http://dubbo.apache.org/en-us/docs/admin/install/provider-demo.html) - How to admin and manage Dubbo services. From 2cfc1aa8d8c1abfd0e2fbfde120a9c41c12d993a Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Tue, 9 Oct 2018 10:46:25 +0800 Subject: [PATCH 2/2] #2583: ApplicationModel serviceName is not unique --- .../dubbo/config/model/ApplicationModel.java | 39 ++++++++++--------- .../dubbo/qos/command/impl/Offline.java | 4 +- .../apache/dubbo/qos/command/impl/Online.java | 4 +- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/model/ApplicationModel.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/model/ApplicationModel.java index d728fb441f7..36d4f03825f 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/model/ApplicationModel.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/model/ApplicationModel.java @@ -21,13 +21,15 @@ import org.apache.dubbo.common.utils.ConcurrentHashSet; import org.apache.dubbo.rpc.Invoker; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static java.util.stream.Collectors.toSet; + // TODO need to adjust project structure in order to fully utilize the methods introduced here. public class ApplicationModel { @@ -36,42 +38,41 @@ public class ApplicationModel { /** * full qualified class name -> provided service */ - private static final ConcurrentMap providedServices = new ConcurrentHashMap(); + private static final ConcurrentMap> providedServices = new ConcurrentHashMap<>(); /** * full qualified class name -> subscribe service */ - private static final ConcurrentMap consumedServices = new ConcurrentHashMap(); + private static final ConcurrentMap> consumedServices = new ConcurrentHashMap<>(); - public static final ConcurrentMap> providedServicesInvoker = new ConcurrentHashMap>(); + private static final ConcurrentMap> providedServicesInvoker = new ConcurrentHashMap<>(); - public static List allConsumerModels() { - return new ArrayList(consumedServices.values()); + public static Collection allConsumerModels() { + return consumedServices.values().stream().flatMap(Collection::stream).collect(toSet()); } - public static ProviderModel getProviderModel(String serviceName) { - return providedServices.get(serviceName); + public static Collection allProviderModels() { + return providedServices.values().stream().flatMap(Collection::stream).collect(toSet()); } - public static ConsumerModel getConsumerModel(String serviceName) { - return consumedServices.get(serviceName); + public static Collection getProviderModel(String serviceName) { + return providedServices.get(serviceName); } - public static List allProviderModels() { - return new ArrayList(providedServices.values()); + public static Collection getConsumerModel(String serviceName) { + return consumedServices.get(serviceName); } - public static boolean initConsumerModel(String serviceName, ConsumerModel consumerModel) { - if (consumedServices.putIfAbsent(serviceName, consumerModel) != null) { + public static void initConsumerModel(String serviceName, ConsumerModel consumerModel) { + Set consumerModels = consumedServices.computeIfAbsent(serviceName, k -> new HashSet<>()); + if (!consumerModels.add(consumerModel)) { logger.warn("Already register the same consumer:" + serviceName); - return false; } - return true; } public static void initProviderModel(String serviceName, ProviderModel providerModel) { - if (providedServices.put(serviceName, providerModel) != null) { + Set providerModels = providedServices.computeIfAbsent(serviceName, k -> new HashSet<>()); + if (!providerModels.add(providerModel)) { logger.warn("already register the provider service: " + serviceName); - return; } } diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java index 59ac30d47ee..a886e7e083a 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Offline.java @@ -29,7 +29,7 @@ import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; -import java.util.List; +import java.util.Collection; import java.util.Set; @Cmd(name = "offline", summary = "offline dubbo", example = { @@ -49,7 +49,7 @@ public String execute(CommandContext commandContext, String[] args) { } boolean hasService = false; - List providerModelList = ApplicationModel.allProviderModels(); + Collection providerModelList = ApplicationModel.allProviderModels(); for (ProviderModel providerModel : providerModelList) { if (providerModel.getServiceName().matches(servicePattern)) { hasService = true; diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java index 20714d4c20d..10cf453b570 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Online.java @@ -29,7 +29,7 @@ import org.apache.dubbo.registry.support.ProviderConsumerRegTable; import org.apache.dubbo.registry.support.ProviderInvokerWrapper; -import java.util.List; +import java.util.Collection; import java.util.Set; @Cmd(name = "online", summary = "online dubbo", example = { @@ -50,7 +50,7 @@ public String execute(CommandContext commandContext, String[] args) { boolean hasService = false; - List providerModelList = ApplicationModel.allProviderModels(); + Collection providerModelList = ApplicationModel.allProviderModels(); for (ProviderModel providerModel : providerModelList) { if (providerModel.getServiceName().matches(servicePattern)) { hasService = true;