当前位置: 首页 > news >正文

Dubbo 的 Java 项目间调用的完整示例

1. 项目结构

假设项目分为三个模块:

  • api:定义服务接口

  • provider:服务提供者

  • consumer:服务消费者

2. 依赖配置

pom.xml 中添加 Dubbo 和注册中心(如 Nacos)的依赖:

<dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>3.0.2</version>
</dependency>
<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>2.0.3</version>
</dependency>

3. 定义服务接口(api 模块)

创建一个服务接口:

package com.example.api;public interface HelloService {String sayHello(String name);
}

4. 服务提供者(provider 模块)

4.1 实现服务接口
package com.example.provider;import com.example.api.HelloService;
import org.apache.dubbo.config.annotation.Service;@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return "Hello, " + name;}
}
4.2 配置 Dubbo

application.properties 中配置 Dubbo 和注册中心:

properties

dubbo.application.name=hello-service-provider
dubbo.registry.address=nacos://127.0.0.1:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

5. 服务消费者(consumer 模块)

5.1 引用服务接口

application.properties 中配置 Dubbo 和注册中心:

properties

dubbo.application.name=hello-service-consumer
dubbo.registry.address=nacos://127.0.0.1:8848
5.2 调用服务
package com.example.consumer;import com.example.api.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@Reference(version = "1.0.0")private HelloService helloService;@GetMapping("/sayHello")public String sayHello(@RequestParam String name) {return helloService.sayHello(name);}
}

6. 启动项目

  1. 启动 Nacos 注册中心。

  2. 启动服务提供者模块(provider)。

  3. 启动服务消费者模块(consumer)。

  4. 访问 http://localhost:8080/sayHello?name=Kimi,即可看到返回结果。

7. 泛化调用示例(可选)

如果需要泛化调用,可以在消费者端使用 GenericService

package com.example.consumer;import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.service.GenericService;public class GenericConsumer {public static void main(String[] args) {ReferenceConfig<GenericService> reference = new ReferenceConfig<>();reference.setInterface("com.example.api.HelloService");reference.setGeneric(true);GenericService genericService = reference.get();String result = genericService.$invoke("sayHello", new String[]{"java.lang.String"}, new Object[]{"Kimi"});System.out.println(result);}
}

泛化调用不需要提前知道服务接口的具体定义。

通过以上步骤,可以实现一个完整的 Dubbo 服务调用案例

http://www.dtcms.com/a/341117.html

相关文章:

  • 分析NeRF模型中颜色计算公式中的参数
  • Paraformer实时语音识别中的碎碎念
  • RuntimeError: Dataset scripts are no longer supported, but found wikipedia.py
  • 车辆订单状态管理的优化方案:状态机设计模式
  • 从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践
  • 从零开发Java坦克大战Ⅱ(上) -- 从单机到联机(架构演进与设计模式剖析)
  • 音频大模型学习笔记
  • CS+ for CC编译超慢的问题该如何解决
  • 0-1 背包问题(模板)
  • 汽车ECU实现数据安全存储(机密性保护)的一种方案
  • Ubuntu apt安装nginx
  • 使用Spring Retry组件优雅地实现重试
  • Java 定时任务 - 从基础到高阶使用 - 从 Timer 到 Quart
  • 数据结构 二叉树 二叉树链式结构的实现
  • 数据分析师常用命令
  • 数据结构中的列表:深度解析数组与链表的实现与抉择
  • PyTorch API 3 - distributed
  • 前后端联合实现文件上传,实现 SQL Server image 类型文件上传
  • 51单片机-驱动LED点阵模块教程
  • SQL-leetcode—3374. 首字母大写 II
  • Docker--安装MySQL、Redis
  • 面试常考的 SQL 窗口函数汇总
  • 【Tech Arch】Apache Pig大数据处理的高效利器
  • 深入理解数据结构:从数组、链表到B树家族
  • 数据结构:利用旋转在AVL树中维持平衡(Inserting in AVL with Rotation)
  • FastAPI初学
  • PyTorch API 1
  • 新能源知识库(81)新能源半实物仿真平台介绍
  • C/C++ Linux系统编程:详解常见的系统调用函数,文件I/O核心:open, close, read, write
  • 【C++】基础:C++11-14-17常用新特性介绍