這篇文章主要介紹了springcloud使用dubbo開發(fā)rpc服務(wù)及調(diào)用案例,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

spring cloud中基于springboot開發(fā)的微服務(wù),是基于http的rest接口,也可以開發(fā)基于dubbo的rpc接口。
一,創(chuàng)建goodsService模塊
1, 在創(chuàng)建的goodsService模塊中再創(chuàng)建goodsServiceApi和goodsServiceServer模塊
2,在oodsServiceApi模塊中定義接口 ,goodsServiceServer用于接口實(shí)現(xiàn)
3,goodsServiceServer模塊中pom文件引入相關(guān)依賴
<dependencies> <dependency> <groupId>net.biui</groupId> <artifactId>goods-service-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> </dependencies>
4,goodsServiceServer中添加配置
spring: application: name: goods-service cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501dubbo: registry: address: nacos://127.0.0.1:8848 scan: base-packages: net.biui.impl protocol: port: 20881 name: dubbo 5,goodsServiceServer
編寫接口實(shí)現(xiàn)
@org.apache.dubbo.config.annotation.Servicepublic class GoodsImpl implements GoodsApi { public String getGoodsName() { return "商品一"; }}
6,goodsServiceServer編寫啟動類
@SpringBootApplication@EnableDiscoveryClientpublic class GoodsServiceServerApplication { public static void main(String[] args) { SpringApplication.run(GoodsServiceServerApplication.class, args); }}啟動后,dubbo服務(wù)會自動注冊到nacos服務(wù)發(fā)現(xiàn)中心
二,創(chuàng)建調(diào)用dubbo服務(wù)的模塊
1,new -> module -> 填寫信息 -> finish
2,添加pom依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> </dependency> <dependency> <groupId>net.biui</groupId> <artifactId>goods-service-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
3,添加配置
spring: application: name: demo-dubbo cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: c22e5019-0bee-43b1-b80b-fc0b9d847501
4,編寫controller調(diào)用dubbo服務(wù)
@RestController@RequestMapping("/demo")public class demoController { @org.apache.dubbo.config.annotation.Reference GoodsApi goodsApi; @GetMapping("/test") public String test(){ return "test " + goodsApi.getGoodsName(); }}5,編寫啟動類
@SpringBootApplication@EnableDiscoveryClientpublic class demoDubboApplication { public static void main(String[] args) { SpringApplication.run(demoDubboApplication.class, args); }}啟動后,demo-dubbo服務(wù)也會自動注冊到nacos(因?yàn)閚acos.register.enable默認(rèn)為true,即代表自動注冊,可以只訂閱,不注冊),對應(yīng)接口返回了dubbo服務(wù)返回的信息!
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“springcloud使用dubbo開發(fā)rpc服務(wù)及調(diào)用案例”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
標(biāo)題名稱:springcloud使用dubbo開發(fā)rpc服務(wù)及調(diào)用案例-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article6/sheog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、服務(wù)器托管、動態(tài)網(wǎng)站、網(wǎng)站收錄、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容