diff --git a/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/BarService.java b/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/BarService.java new file mode 100644 index 0000000000000000000000000000000000000000..48a9f08e4441efe49f4c32fc19b8af9d29541500 --- /dev/null +++ b/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/BarService.java @@ -0,0 +1,12 @@ +/** + * Created by shenhongxi on 2017/6/21. + */ +package org.hongxi.whatsmars.dubbo.demo.api; + +import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar; + +public interface BarService { + + Bar findBar(String barId); + +} \ No newline at end of file diff --git a/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/vo/Bar.java b/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/vo/Bar.java new file mode 100644 index 0000000000000000000000000000000000000000..78d5a32a500851192d731af05de6ea9af2b43eda --- /dev/null +++ b/whatsmars-dubbo/whatsmars-dubbo-api/src/main/java/org/hongxi/whatsmars/dubbo/demo/api/vo/Bar.java @@ -0,0 +1,51 @@ +package org.hongxi.whatsmars.dubbo.demo.api.vo; + +import java.io.Serializable; + +public class Bar implements Serializable { + + private static final long serialVersionUID = -5809782578272943999L; + + private String barId; + + private String name; + + private String address; + + public Bar() {} + + public Bar(String barId, String name, String address) { + this.barId = barId; + this.name = name; + this.address = address; + } + + public String getBarId() { + return barId; + } + + public void setBarId(String barId) { + this.barId = barId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public String toString() { + return "barId:" + barId + ",name:" + name + ",address" + address; + } +} diff --git a/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/java/org/hongxi/whatsmars/dubbo/demo/consumer/DemoConsumer.java b/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/java/org/hongxi/whatsmars/dubbo/demo/consumer/DemoConsumer.java index 4c77755628cdc6b05dfa8751d13310ae81f503e1..cef3f3c874382bd92f17aa2aa1e3cabcd1c6ecd3 100644 --- a/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/java/org/hongxi/whatsmars/dubbo/demo/consumer/DemoConsumer.java +++ b/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/java/org/hongxi/whatsmars/dubbo/demo/consumer/DemoConsumer.java @@ -3,17 +3,25 @@ */ package org.hongxi.whatsmars.dubbo.demo.consumer; +import com.alibaba.dubbo.rpc.RpcContext; import com.alibaba.dubbo.rpc.service.EchoService; +import org.hongxi.whatsmars.dubbo.demo.api.BarService; import org.hongxi.whatsmars.dubbo.demo.api.DemoService; +import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar; import org.springframework.context.support.ClassPathXmlApplicationContext; +import java.util.concurrent.Future; + public class DemoConsumer { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); context.start(); + // 异步调用 + async(context); + // dubbo protocol DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理 String hello = demoService.sayHello("dubbo"); // 执行远程方法 @@ -39,6 +47,24 @@ public class DemoConsumer { Object status = echoService.$echo("OK"); System.out.println("回声测试:" + status.equals("OK")); + System.out.println("#######################ALL SUCCESSFUL##########################"); + } + private static void async(ClassPathXmlApplicationContext context) throws Exception { + // 异步调用 + DemoService demoService5 = (DemoService) context.getBean("demoService5"); + demoService5.sayHello("aysc"); + Future helloFuture = RpcContext.getContext().getFuture(); + + BarService barService = (BarService) context.getBean("barService"); + barService.findBar("m123456"); + Future barFuture = RpcContext.getContext().getFuture(); + + String hello5 = helloFuture.get(); + Bar bar = barFuture.get(); + System.out.println(hello5); + System.out.println(bar); + } + } \ No newline at end of file diff --git a/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml b/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml index 8380266c842cdc4206b4c9e1336cb4ec9ef11f28..ab0645f6bde6c46303b398c8f98cc4ce582d1eab 100644 --- a/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml +++ b/whatsmars-dubbo/whatsmars-dubbo-consumer/src/main/resources/META-INF/spring/dubbo-demo-consumer.xml @@ -22,4 +22,12 @@ + + + + + + + + \ No newline at end of file diff --git a/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/java/org/hongxi/whatsmars/dubbo/demo/provider/BarServiceImpl.java b/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/java/org/hongxi/whatsmars/dubbo/demo/provider/BarServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..3257b15663c206c11227f23f1b013ea0386bd99e --- /dev/null +++ b/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/java/org/hongxi/whatsmars/dubbo/demo/provider/BarServiceImpl.java @@ -0,0 +1,23 @@ +/** + * Created by shenhongxi on 2017/6/21. + */ +package org.hongxi.whatsmars.dubbo.demo.provider; + +import org.hongxi.whatsmars.dubbo.demo.api.BarService; +import org.hongxi.whatsmars.dubbo.demo.api.vo.Bar; +import org.springframework.stereotype.Service; + +@Service("barService") +public class BarServiceImpl implements BarService { + + @Override + public Bar findBar(String barId) { + try { + Thread.sleep(1000); + } catch (Exception e) { + + } + return new Bar(barId, "芝根芝底", "酒仙桥302号"); + } + +} \ No newline at end of file diff --git a/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml b/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml index e530b7d7b7a3d718f01cbe726ead92becc1c3578..3ca710edf0ff9695b0f8e9b47ac5f4b21f29a6f1 100644 --- a/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml +++ b/whatsmars-dubbo/whatsmars-dubbo-provider/src/main/resources/META-INF/spring/dubbo-demo-provider.xml @@ -29,4 +29,6 @@ + + \ No newline at end of file