日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

dubbo 体验

發布時間:2023/12/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 dubbo 体验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?第一步,要選擇dubbo的中間件,之前用的是zookeeper來做注冊中心的,所以我這邊也使用它來搭建注冊中心,下載地址去apache的官網下載,可以戳我直接去到官網下載穩定版本。而后解壓到本地文件夾,解壓出來的結構如下

打開conf文件夾,copy zoo_sample.cfg副本,重命名為zoo.cfg,然后可以修改里面的內容,也可以不修改。

打開bin目錄下的zkServer.cmd文件,如果報錯,請設置好環境變量。

?

?

正常啟動如下

注冊中心就弄好了,

?????????第二步,創建service工程與其中的服務,本案例使用maven構建系統,怎么創建maven系統這邊就不做贅述了,主要上下其中的代碼

pom.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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <project?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/xsd/maven-4.0.0.xsd"> ?????????<modelVersion>4.0.0</modelVersion> ?? ?????????<groupId>com.inspires</groupId> ?????????<artifactId>dubbo-service</artifactId> ?????????<version>0.0.1-SNAPSHOT</version> ?????????<packaging>jar</packaging> ?? ?????????<name>dubbo-service</name> ?????????<url>http://maven.apache.org</url> ?? ?????????<properties> ???????????????????<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ?????????</properties> ?????????<dependencies> ????????? ???????????????????<dependency> ????????????????????????????<groupId>junit</groupId> ????????????????????????????<artifactId>junit</artifactId> ????????????????????????????<version>4.12</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>org.springframework</groupId> ????????????????????????????<artifactId>spring-context</artifactId> ????????????????????????????<version>3.2.5.RELEASE</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>com.alibaba</groupId> ????????????????????????????<artifactId>dubbo</artifactId> ????????????????????????????<version>2.4.9</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>org.apache.zookeeper</groupId> ????????????????????????????<artifactId>zookeeper</artifactId> ????????????????????????????<version>3.4.6</version> ???????????????????</dependency> ???????????????????<dependency> ????????????????????????????<groupId>com.101tec</groupId> ????????????????????????????<artifactId>zkclient</artifactId> ????????????????????????????<version>0.4</version> ???????????????????</dependency> ?????????</dependencies> </project>

然后創建一個spring的配置文件命名為service-dubbo.xml

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?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="service-test-dubbo"/> ?????????<!--?使用multicast廣播注冊中心暴露服務地址?--> ?????????<dubbo:registry?address="zookeeper://127.0.0.1:2181"?/> ?????????<!--?用dubbo協議在20880端口暴露服務?--> ?????????<dubbo:protocol?name="dubbo"?port="20880"?/> ?????????<!--?聲明需要暴露的服務接口?--> ?????????<dubbo:service?interface="com.inspires.dubbo.service.IDemoService"?ref="demoService"?/> ?????????<!--?和本地bean一樣實現服務?--> ?????????<bean?id="demoService"?class="com.inspires.dubbo.service.impl.DemoService"?/> </beans>

?

接下來創建一個接口與實現類IDemoServiceDemoService,只創建一個sayHello方法以做校驗

IDemoService.java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /** ?* ?*/ package?com.inspires.dubbo.service; ?? /** ?*?@author?Jon?Chiang ?*?@project?dubbo-service ?*?@create_date?2014-12-30?下午5:06:52 ?*/ public?interface?IDemoService?{ ?? ?????????/** ???????????*?@author?Jon?Chiang ???????????*?@create_date?2014-12-30?下午5:09:27 ???????????*?@param?name ???????????*?@return ???????????*/ ?????????String?sayHello(String?name); }

DemoService.java?不多說

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /** ?* ?*/ package?com.inspires.dubbo.service.impl; ?? import?com.inspires.dubbo.service.IDemoService; ?? /** ?*?@author?Jon?Chiang ?*?@project?dubbo-service ?*?@create_date?2014-12-30?下午5:07:29 ?*/ public?class?DemoService?implements?IDemoService?{ ?????????static?int?sayHelloCount?=?0; ?????????@Override ?????????public?String?sayHello(String?name)?{ ???????????????????String?hello?=?"Hello?"?+?name; ???????????????????System.out.println(++sayHelloCount); ???????????????????System.out.println(hello); ???????????????????return?hello; ?????????} }

還有web.xml里面要做spring配置文件和監聽的配置,這里不做贅述(附件里面有,不會的同事可以參考附件)這里service工程基本弄好了

第三步,創建maven的訪問者或者客戶端工程。

pom.xml?注意要引用service工程,不然沒法訪問IDemoService

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 <project?xmlns="http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?http://maven.apache.org/maven-v4_0_0.xsd"> ?????????<modelVersion>4.0.0</modelVersion> ?????????<groupId>com.inspires</groupId> ?????????<artifactId>dubbo-client</artifactId> ?????????<packaging>war</packaging> ?????????<version>0.0.1-SNAPSHOT</version> ?????????<name>dubbo-client?Maven?Webapp</name> ?????????<url>http://maven.apache.org</url> ?? ?????????<build> ???????????????????<finalName>dubbo-client</finalName> ?????????</build> ?????????<dependencies> ???????????????????<dependency> ????????????????????????????<groupId>junit</groupId> ????????????????????????????<artifactId>junit</artifactId> ????????????????????????????<version>4.12</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>org.springframework</groupId> ????????????????????????????<artifactId>spring-context</artifactId> ????????????????????????????<version>3.2.5.RELEASE</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>com.alibaba</groupId> ????????????????????????????<artifactId>dubbo</artifactId> ????????????????????????????<version>2.4.9</version> ???????????????????</dependency> ?? ???????????????????<dependency> ????????????????????????????<groupId>org.apache.zookeeper</groupId> ????????????????????????????<artifactId>zookeeper</artifactId> ????????????????????????????<version>3.4.6</version> ???????????????????</dependency> ???????????????????<dependency> ????????????????????????????<groupId>com.101tec</groupId> ????????????????????????????<artifactId>zkclient</artifactId> ????????????????????????????<version>0.4</version> ???????????????????</dependency> ???????????????????<dependency> ????????????????????????????<groupId>com.inspires</groupId> ????????????????????????????<artifactId>dubbo-service</artifactId> ????????????????????????????<version>0.0.1-SNAPSHOT</version> ???????????????????</dependency> ?????????</dependencies> </project>

然后就是spring文件

client-dubbo.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 26 27 28 29 30 <?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="client-test-dubbo"/>???????<!--?使用zookeeper廣播注冊中心暴露發現服務地址?--> ????<dubbo:registry?address="zookeeper://127.0.0.1:2181"?/>?????????<!--?生成遠程服務代理,可以和本地bean一樣使用demoService?--> ????<dubbo:reference?id="demoService"?interface="com.inspires.dubbo.service.IDemoService"?/> </beans> client-common.xml <?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:context="http://www.springframework.org/schema/context" ?????????xmlns:tx="http://www.springframework.org/schema/tx"?xmlns:util="http://www.springframework.org/schema/util" ?????????xsi:schemaLocation="http://www.springframework.org/schema/beans ?????????http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ?????????http://www.springframework.org/schema/util ?????????http://www.springframework.org/schema/util/spring-util-3.2.xsd ?????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd ???????????????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ???http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" ?????????default-lazy-init="false"> ?????????<context:annotation-config?/> ?????????<context:component-scan?base-package="com.inspires.dubbo.service"?/>??<!--?自動掃描所有注解該路徑?--> </beans>

讓后我們在service里面寫一個測試調用遠程接口的方法。因為我這邊只是測試接口是否通,所以使用@PostConstruct注解,讓工程一啟動就調用init方法訪問接口,達到我測試的目的。

TestService.java

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 30 31 32 33 /** ?* ?*/ package?com.inspires.dubbo.service.impl; ?? import?javax.annotation.PostConstruct; ?? import?org.springframework.beans.factory.annotation.Autowired; import?org.springframework.stereotype.Service; ?? import?com.inspires.dubbo.service.IDemoService; import?com.inspires.dubbo.service.ITestService; ?? /** ?*?@author?Jon?Chiang ?*?@project?dubbo-client ?*?@create_date?2014-12-30?下午6:54:12 ?*/ @Service public?class?TestService?implements?ITestService{ ?? ?????????@Autowired ?????????IDemoService?demoService; ????????? ?????????@PostConstruct ?????????@Override ?????????public?void?init(){ ???????????????????for?(int?i?=?0;?i?<?20;?i++)?{ ????????????????????????????demoService.sayHello("Jon?Chiang?"); ???????????????????} ??????????????????? ?????????} }

然后可以開測了

啟動service工程控制臺無異常,然后啟動client工程,系統調用遠程方法。控制臺答應hello Jon ?Chiang?大功告成也!

?

總結

以上是生活随笔為你收集整理的dubbo 体验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。