Dubbo zookeeper 初探
阿新 • • 發佈:2019-02-16
轉:
c、applicationProvider.xml配置
d、啟動服務
3、服務呼叫者的工程
b、applicationConsumer.xml配置
參考:
Dubbo的簡介 http://doc.okbase.net/congcong68/archive/112508.html
http://www.iteye.com/magazines/103
建議參考資料:
先把zookeeper在本地給安裝好,這裡的話講述了兩個工程一個工程是提供服務的,一個工程是呼叫服務的,因為dubbo是跟spring進行無縫連線的,故功能配置在spring的配置檔案中,跟spring進行整合開發
1、工程是以maven進行構建的,使用的jar包如下:
2、服務提供者的工程
a、dubbo-demo-api 定義介面
- publicinterface IProcessData {
- public String deal(String data);
- }
- publicclass ProcessDataImpl implements IProcessData {
- /*
- * @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String)
- */
- @Override
- public
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return"Finished:" + data;
- }
- }
c、applicationProvider.xml配置
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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
- ">
- <!-- Application name -->
- <dubbo:applicationname="hello-world-app"/>
- <!-- registry address, used for service to register itself -->
- <dubbo:registryaddress="zookeeper://127.0.0.1:2181"/>
- <!-- expose this service through dubbo protocol, through port 20880 -->
- <!--
- <dubbo:protocolname="dubbo"port="20880"/>
- <dubbo:protocolname="dubbo"port="9090"server="netty"
- client="netty"codec="dubbo"serialization="hessian2"charset="UTF-8"
- threadpool="fixed"threads="100"queues="0"iothreads="9"buffer="8192"
- accepts="1000"payload="8388608"/>
- -->
- <!-- Service interface Concurrent Control -->
- <dubbo:serviceinterface="com.huangjie.dubbo_Service.service.IProcessData"
- ref="demoService"executes="10"/>
- <!-- Default Protocol -->
- <!--
- <dubbo:protocol server="netty" />
- -->
- <!-- designate implementation -->
- <beanid="demoService"class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl"/>
- </beans>
d、啟動服務
- publicclass DubboProviderMain {
- publicstaticvoid main(String[] args) throws Exception {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[]{"applicationProvider.xml"});
- context.start();
- System.out.println("Press any key to exit.");
- System.in.read();
- }
- }
3、服務呼叫者的工程
a、呼叫類
- publicclass ConsumerThd implements Runnable {
- /*
- * @see java.lang.Runnable#run()
- */
- @Override
- publicvoid run() {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
- new String[]{"applicationConsumer.xml"});
- context.start();
- IProcessData demoService = (IProcessData) context.getBean("demoService"); // get
- // service
- // invocation
- // proxy
- String hello = demoService.deal("nihao"); // do invoke!
- System.out.println(Thread.currentThread().getName() + " "+hello);
- }
- publicstaticvoid main(String[] args) {
- new Thread(new ConsumerThd()).start();
- /**
- * 輸出結果:
- * Thread-0 Finished:nihao
- */
- }
- }
b、applicationConsumer.xml配置
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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
- ">
- <!-- consumer application name -->
- <dubbo:applicationname="consumer-of-helloworld-app"/>
- <!-- registry address, used for consumer to discover services -->
- <dubbo:registryaddress="zookeeper://127.0.0.1:2181"/>
- <dubbo:consumertimeout="5000"/>
- <!-- which service to consume? -->
- <dubbo:referenceid="demoService"interface="com.huangjie.dubbo_Service.service.IProcessData"/>
- </beans>
這樣整個呼叫就完成了,這樣的好處是隻要遠端提供ip地址及埠號,以及對外呼叫的類,客戶端就可以呼叫,客戶端不必知道服務端的地址之類的
而且服務端可以開多個zookeeper服務,這樣如果其中一個zookeeper 服務死掉了,其他服務還能正常執行
- http://blog.csdn.net/wxwzy738/article/details/16330253