yihuihan closed pull request #2575: Add API configured Provider/Consumer sample
URL: https://github.com/apache/incubator-dubbo/pull/2575
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/dubbo-demo/dubbo-demo-consumer/src/main/java/org/apache/dubbo/demo/consumer/ConsumerConfByAPI.java
 
b/dubbo-demo/dubbo-demo-consumer/src/main/java/org/apache/dubbo/demo/consumer/ConsumerConfByAPI.java
new file mode 100644
index 0000000000..7523f2e9e1
--- /dev/null
+++ 
b/dubbo-demo/dubbo-demo-consumer/src/main/java/org/apache/dubbo/demo/consumer/ConsumerConfByAPI.java
@@ -0,0 +1,33 @@
+package org.apache.dubbo.demo.consumer;
+
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.demo.DemoService;
+
+public class ConsumerConfByAPI {
+    public static void main(String[] args) {
+        ApplicationConfig applicationConfig = new ApplicationConfig();
+        applicationConfig.setName("demo-consumer");
+
+        RegistryConfig registryConfig = new RegistryConfig();
+        registryConfig.setAddress("zookeeper://127.0.0.1:2181"); //The address 
should be changed according to your own environment
+
+        ReferenceConfig<DemoService> referenceConfig = new ReferenceConfig<>();
+        referenceConfig.setApplication(applicationConfig);
+        referenceConfig.setRegistry(registryConfig);
+        referenceConfig.setInterface(DemoService.class);
+        referenceConfig.setCheck(false);
+
+        DemoService demoService = referenceConfig.get();
+        while (true) {
+            try {
+                Thread.sleep(1000);
+                String hello = demoService.sayHello("world"); // call remote 
method
+                System.out.println(hello); // get result
+            } catch (Throwable throwable) {
+                throwable.printStackTrace();
+            }
+        }
+    }
+}
diff --git 
a/dubbo-demo/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
 
b/dubbo-demo/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
index a56a10ee7e..43b8da4c0c 100644
--- 
a/dubbo-demo/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
+++ 
b/dubbo-demo/dubbo-demo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
@@ -26,7 +26,7 @@
     <dubbo:application name="demo-consumer"/>
 
     <!-- use multicast registry center to discover service -->
-    <dubbo:registry address="multicast://224.5.6.7:1234"/>
+    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
 
     <!-- generate proxy for the remote service, then demoService can be used 
in the same way as the
     local regular interface -->
diff --git 
a/dubbo-demo/dubbo-demo-provider/src/main/java/org/apache/dubbo/demo/provider/ProviderConfByAPI.java
 
b/dubbo-demo/dubbo-demo-provider/src/main/java/org/apache/dubbo/demo/provider/ProviderConfByAPI.java
new file mode 100644
index 0000000000..4c72c20e0a
--- /dev/null
+++ 
b/dubbo-demo/dubbo-demo-provider/src/main/java/org/apache/dubbo/demo/provider/ProviderConfByAPI.java
@@ -0,0 +1,35 @@
+package org.apache.dubbo.demo.provider;
+
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.demo.DemoService;
+
+import java.io.IOException;
+
+public class ProviderConfByAPI {
+    public static void main(String[] args) throws IOException {
+        DemoService demoService = new DemoServiceImpl();
+
+        ApplicationConfig applicationConfig = new ApplicationConfig();
+        applicationConfig.setName("demo-provider");
+
+        RegistryConfig registryConfig= new RegistryConfig();
+        registryConfig.setAddress("zookeeper://127.0.0.1:2181"); //The address 
should be changed according to your own environment
+
+        ProtocolConfig protocolConfig=new ProtocolConfig();
+        protocolConfig.setName("dubbo");
+        protocolConfig.setPort(20880);
+
+        ServiceConfig<DemoService> serviceConfig=new ServiceConfig<>();
+        serviceConfig.setApplication(applicationConfig);
+        serviceConfig.setRegistry(registryConfig);
+        serviceConfig.setProtocol(protocolConfig);
+        serviceConfig.setInterface(DemoService.class);
+        serviceConfig.setRef(demoService);
+        serviceConfig.export();
+
+        System.in.read();
+    }
+}
diff --git 
a/dubbo-demo/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml
 
b/dubbo-demo/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml
index c2ab4a21e0..ed08464c88 100644
--- 
a/dubbo-demo/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml
+++ 
b/dubbo-demo/dubbo-demo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml
@@ -25,7 +25,7 @@
     <dubbo:application name="demo-provider"/>
 
     <!-- use multicast registry center to export service -->
-    <dubbo:registry address="multicast://224.5.6.7:1234"/>
+    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
 
     <!-- use dubbo protocol to export service on port 20880 -->
     <dubbo:protocol name="dubbo" port="20880"/>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to