Dubbo服务发布调用实现
服務(wù)發(fā)布調(diào)用實(shí)現(xiàn)
系統(tǒng)需求
根據(jù)商品id,查詢商品信息
Dao
單表查詢,不需要寫代碼
使用Mybatis逆向工程生成的代碼
Interface
在taotao-manager-interface工程中
創(chuàng)建一個ItemService接口
Service
實(shí)現(xiàn)接口
要找到接口,需要添加對接口的引用
在taotao-manager-Service工程中
創(chuàng)建一個itemSeviceImpl的實(shí)現(xiàn)類
發(fā)布服務(wù)
使用Dubbo發(fā)布服務(wù),引入jar包
父工程,控制版本號
Service,添加jar包引用
注意
Maven依賴傳遞
引入Dubbo的時候,會把依賴的Spring版本也添加進(jìn)來
排除Spring的引用,否則,可能會有版本沖突問題
同理,排除netty的引用
選中,右鍵Exclude Maven Artifact
引用標(biāo)簽
在配置文件中,使用dubbo標(biāo)簽
需要添加dubbo標(biāo)簽,dubbo約束
否則,使用dubbo標(biāo)簽,標(biāo)簽提示錯誤
applicationContext-service.xml
發(fā)布服務(wù)
<!-- 發(fā)布dubbo服務(wù) --> <!-- 提供方應(yīng)用信息,用于計算依賴關(guān)系 --> <dubbo:application name="taotao-manager" /> <!-- 注冊中心的地址 --> <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181" /> <!-- 用dubbo協(xié)議在20880端口暴露服務(wù) --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明需要暴露的服務(wù)接口 --> <dubbo:service interface="com.taotao.service.ItemService" ref="itemServiceImpl" timeout="300"/>配置說明
dubbo:application,配置應(yīng)用程序,名稱
dubbo:registry,配置注冊中心,地址端口號,默認(rèn)2181
dubbo:protocol,配置dubbo服務(wù)端口號,默認(rèn)20880
這個端口號,可以改為其他端口號
只要不沖突,并且在65535范圍內(nèi)即可
dubbo:service,暴露服務(wù)接口
Interface,接口,配置接口的全路徑名
Ref,引用接口實(shí)現(xiàn),從容器中獲取
itemServiceImpl
通過掃描包,掃描到具體的實(shí)現(xiàn)類
在容器中創(chuàng)建一個Bean,添加到Spring容器中
Id,默認(rèn)為類名,首字母小寫itemServiceImpl
Timeout,設(shè)置超時時間
服務(wù)調(diào)用
Web工程,添加dubbo引用
<!-- dubbo相關(guān)的jar包 --> <dependency><groupId>com.alibaba</groupId><artifactId>dubbo</artifactId><exclusions><exclusion><artifactId>spring</artifactId><groupId>org.springframework</groupId></exclusion><exclusion><artifactId>netty</artifactId><groupId>org.jboss.netty</groupId></exclusion></exclusions> </dependency> <dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId> </dependency> <dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId> </dependency>引用標(biāo)簽
需要在Spring容器中,引用服務(wù)
Spring MVC前端控制器,也會初始化一個Spring容器
所以,可以在Spring MVC里面直接引用服務(wù)
Springmvc.xml
同理,添加dubbo標(biāo)簽、約束
引用服務(wù)
<!-- 引用dubbo服務(wù) --> <dubbo:application name="taotao-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.167:2181"/> <dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />配置說明
發(fā)布服務(wù),需要在注冊中心注冊
引用服務(wù),也需要在注冊中心注冊
dubbo:application,配置應(yīng)用程序,程序名稱
dubbo:registry,配置注冊中心,地址端口號
dubbo:reference,引用服務(wù)
Interface
Web需要引用Interface接口
Pom文件,添加對接口的引用
接口
需要單獨(dú)提出了,存放接口的定義
服務(wù)端,發(fā)布服務(wù),需要這個接口定義
表現(xiàn)層,引用服務(wù),也需要這個接口定義
所以,把接口打成一個jar包,方便表現(xiàn)層的引用
雖然,Interface是工程的一個模塊
但是,只要是一個jar包有坐標(biāo),就可以通過坐標(biāo)引用
此時,Spring MVC容器中
就有這個Interface的代理對象,通過id給Bean起個名字
容器中,就有這個Bean對象
Controller
通過注解,注入這個對象
在Spring MVC容器初始化的時候
會把這個Bean注入到Controller中,直接調(diào)用即可
總結(jié)
以上是生活随笔為你收集整理的Dubbo服务发布调用实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dubbo服务发布调用
- 下一篇: Dubbo使用