温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springcloud使用dubbo开发rpc服务及调用案例

发布时间:2021-03-05 14:43:50 来源:亿速云 阅读:210 作者:小新 栏目:编程语言

这篇文章主要介绍了springcloud使用dubbo开发rpc服务及调用案例,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

spring cloud中基于springboot开发的微服务,是基于http的rest接口,也可以开发基于dubbo的rpc接口。

一,创建goodsService模块

  1, 在创建的goodsService模块中再创建goodsServiceApi和goodsServiceServer模块

  2,在oodsServiceApi模块中定义接口 ,goodsServiceServer用于接口实现

  3,goodsServiceServer模块中pom文件引入相关依赖

<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

编写接口实现

@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服务会自动注册到nacos服务发现中心

二,创建调用dubbo服务的模块

  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调用dubbo服务

@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服务也会自动注册到nacos(因为nacos.register.enable默认为true,即代表自动注册,可以只订阅,不注册),对应接口返回了dubbo服务返回的信息!

感谢你能够认真阅读完这篇文章,希望小编分享的“springcloud使用dubbo开发rpc服务及调用案例”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI