springcloud如何使用dubbo開發(fā)rpc服務(wù)及調(diào)用
這篇文章主要介紹了springcloud如何使用dubbo開發(fā)rpc服務(wù)及調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
spring cloud中基于springboot開發(fā)的微服務(wù),是基于http的rest接口,也可以開發(fā)基于dubbo的rpc接口。
一,創(chuàng)建goodsService模塊
1, 在創(chuàng)建的goodsService模塊中再創(chuàng)建goodsServiceApi和goodsServiceServer模塊
2,在oodsServiceApi模塊中定義接口 ,goodsServiceServer用于接口實現(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-fc0b9d847501 dubbo: registry: address: nacos://127.0.0.1:8848 scan: base-packages: net.biui.impl protocol: port: 20881 name: dubbo
5,goodsServiceServer編寫接口實現(xiàn)
@org.apache.dubbo.config.annotation.Service public class GoodsImpl implements GoodsApi { public String getGoodsName() { return "商品一"; } }
6,goodsServiceServer編寫啟動類
@SpringBootApplication @EnableDiscoveryClient public 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 @EnableDiscoveryClient public class demoDubboApplication { public static void main(String[] args) { SpringApplication.run(demoDubboApplication.class, args); } }
啟動后,demo-dubbo服務(wù)也會自動注冊到nacos(因為nacos.register.enable默認為true,即代表自動注冊,可以只訂閱,不注冊),對應(yīng)接口返回了dubbo服務(wù)返回的信息!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法
EhCache是一個純Java的進程內(nèi)緩存框架,是 Hibernate 中默認的 CacheProvider,同Redis一樣,EhCache 不是純內(nèi)存緩存,它支持基于內(nèi)存和磁盤的二級緩存,本文介紹Springboot集成Ehcache3實現(xiàn)本地緩存的配置方法,感興趣的朋友一起看看吧2024-04-04將springboot項目生成可依賴的jar并引入到項目中的方法
SpringBoot項目默認打包的是可運行jar包,也可以打包成不可運行的jar包,本文給大家介紹將springboot項目生成可依賴的jar并引入到項目中的方法,感興趣的朋友一起看看吧2023-11-11Spring Boot構(gòu)建優(yōu)雅的RESTful接口過程詳解
這篇文章主要介紹了spring boot構(gòu)建優(yōu)雅的RESTful接口過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析
這篇文章主要介紹了springboot內(nèi)置tomcat調(diào)優(yōu)并發(fā)線程數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12