如何通过Spring Cloud整合Dubbo实现RPC服务的开发与调用?

2026-05-28 09:421阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计534个文字,预计阅读时间需要3分钟。

如何通过Spring Cloud整合Dubbo实现RPC服务的开发与调用?

本篇文章简要介绍了Spring Cloud如何利用Dubbo开发RPC服务及调用。文中通过示例代码展示了非非常详细的实现过程,对大型项目的学习和工作具有一定的参考价值。需要的伙伴可以参考以下内容。

这篇文章主要介绍了springcloud如何使用dubbo开发rpc服务及调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

spring cloud中基于springboot开发的微服务,是基于127.0.0.1:8848 scan: base-packages: net.biui.impl protocol: port: 20881 name: dubbo

5,goodsServiceServer编写接口实现

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

如何通过Spring Cloud整合Dubbo实现RPC服务的开发与调用?

二,创建调用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 @EnableDiscoveryClient public class demoDubboApplication { public static void main(String[] args) { SpringApplication.run(demoDubboApplication.class, args); } }

启动后,demo-dubbo服务也会自动注册到nacos(因为nacos.register.enable默认为true,即代表自动注册,可以只订阅,不注册),对应接口返回了dubbo服务返回的信息!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计534个文字,预计阅读时间需要3分钟。

如何通过Spring Cloud整合Dubbo实现RPC服务的开发与调用?

本篇文章简要介绍了Spring Cloud如何利用Dubbo开发RPC服务及调用。文中通过示例代码展示了非非常详细的实现过程,对大型项目的学习和工作具有一定的参考价值。需要的伙伴可以参考以下内容。

这篇文章主要介绍了springcloud如何使用dubbo开发rpc服务及调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

spring cloud中基于springboot开发的微服务,是基于127.0.0.1:8848 scan: base-packages: net.biui.impl protocol: port: 20881 name: dubbo

5,goodsServiceServer编写接口实现

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

如何通过Spring Cloud整合Dubbo实现RPC服务的开发与调用?

二,创建调用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 @EnableDiscoveryClient public class demoDubboApplication { public static void main(String[] args) { SpringApplication.run(demoDubboApplication.class, args); } }

启动后,demo-dubbo服务也会自动注册到nacos(因为nacos.register.enable默认为true,即代表自动注册,可以只订阅,不注册),对应接口返回了dubbo服务返回的信息!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。