学习dubbo(二): 第1个例子
提供者
接口
| 1 2 3 4 5 6 7 | public?interface?SampleService?{ ????String?sayHello(String?name); ????public?List?getUsers(); } |
接口實現(xiàn)
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public?class?SampleServiceImpl?implements?SampleService?{ ????? ????public?String?sayHello(String?name)?{ ????????return?"Hello?"?+?name; ????} ????public?List?getUsers()?{ ????????List?list?=?new?ArrayList(); ????????User?u1?=?new?User(); ????????u1.setName("jack"); ????????u1.setAge(20); ????????u1.setSex("m"); ????????User?u2?=?new?User(); ????????u2.setName("tom"); ????????u2.setAge(21); ????????u2.setSex("m"); ????????User?u3?=?new?User(); ????????u3.setName("rose"); ????????u3.setAge(19); ????????u3.setSex("w"); ????????list.add(u1); ????????list.add(u2); ????????list.add(u3); ????????return?list; ????} } |
sample-provider.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans" ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" ????xsi:schemaLocation="http://www.springframework.org/schema/beans ????????http://www.springframework.org/schema/beans/spring-beans.xsd ????????http://code.alibabatech.com/schema/dubbo ????????http://code.alibabatech.com/schema/dubbo/dubbo.xsd ????????"> ????<!--?具體的實現(xiàn)bean?--> ????<bean?id="sampleService"?class="bhz.dubbo.sample.provider.impl.SampleServiceImpl"?/> ????<!--?提供方應用信息,用于計算依賴關系?--> ????<dubbo:application?name="sample-provider"?/> ????<!--?使用zookeeper注冊中心暴露服務地址?--> ????<dubbo:registry?address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.4:2181"?/> ????<!--?用dubbo協(xié)議在20880端口暴露服務?--> ????<dubbo:protocol?name="dubbo"?port="20880"?/> ????<!--?聲明需要暴露的服務接口??寫操作可以設置retries=0?避免重復調用SOA服務?--> ????<dubbo:service?retries="0"?interface="bhz.dubbo.sample.provider.SampleService"?ref="sampleService"?/> </beans> |
啟動測試
| 1 2 3 4 5 6 7 8 9 | public?class?Provider?{ ????public?static?void?main(String[]?args)?throws?Exception?{ ????????ClassPathXmlApplicationContext?context?=?new?ClassPathXmlApplicationContext( ????????????????new?String[]?{?"sample-provider.xml"?}); ????????context.start();? ????????System.in.read();?//?為保證服務一直開著,利用輸入流的阻塞來模擬 ????} } |
測試結果
消費者
接口
| 1 2 3 4 5 6 7 | public?interface?SampleService?{ ????String?sayHello(String?name); ????public?List?getUsers(); } |
sample-consumer.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml?version="1.0"?encoding="UTF-8"?> <beans?xmlns="http://www.springframework.org/schema/beans" ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" ????xsi:schemaLocation="http://www.springframework.org/schema/beans ????????http://www.springframework.org/schema/beans/spring-beans.xsd ????????http://code.alibabatech.com/schema/dubbo ????????http://code.alibabatech.com/schema/dubbo/dubbo.xsd ????????"> ????<!--?消費方應用名,用于計算依賴關系,不是匹配條件,不要與提供方一樣?--> ????<dubbo:application?name="sample-consumer"?/> ????<dubbo:registry?address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.4:2181"?/> ????<!--?生成遠程服務代理,可以像使用本地bean一樣使用demoService?檢查級聯(lián)依賴關系?默認為true?當有依賴服務的時候,需要根據(jù)需求進行設置?--> ????<dubbo:reference?id="sampleService"?check="false" ????????interface="bhz.dubbo.sample.provider.SampleService"?/> </beans> |
測試
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public?class?Consumer?{ ????public?static?void?main(String[]?args)?throws?Exception?{ ????????ClassPathXmlApplicationContext?context?=?new?ClassPathXmlApplicationContext( ????????????????new?String[]?{?"sample-consumer.xml"?}); ????????context.start(); ????????????? ????????SampleService?sampleService?=?(SampleService)?context.getBean("sampleService"); ????????String?hello?=?sampleService.sayHello("tom"); ????????System.out.println(hello); ????????System.in.read(); ????} } |
測試結果
Xml文件說明
生產(chǎn)者:
????
注冊中心Zookeeper配置:
單機:
<dubbo:registry address="zookeeper://192.168.175.3:2181" />
集群:
<dubbo:registry address="zookeeper://192.168.175.3:2181?backup=192.168.175.4:2181,192.168.175.5:2181" />
或
<dubbo:registry protocol="zookeeper" address="192.168.175.3:2181,192.168.175.4:2181,192.168.175.5:2181" />
同一Zookeeper,分成多組注冊中心:
<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="192.168.175.3:2181" group="china" />
<dubbo:registry id="intlRegistry" protocol="zookeeper" address="192.168.175.3:2181" group="intl" />
本文轉自我愛大金子博客51CTO博客,原文鏈接http://blog.51cto.com/1754966750/1886241如需轉載請自行聯(lián)系原作者
我愛大金子
總結
以上是生活随笔為你收集整理的学习dubbo(二): 第1个例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL主从介绍、准备工作、配置主、配
- 下一篇: xen虚拟化实战系列(六)之xen虚拟机