当前位置: 首页 > 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.lryc.cn/news/626847.html

相关文章:

  • 新手向:Python实现文件加密解密工具
  • 【java面试day16】mysql-覆盖索引
  • 害虫检测识别数据集:近4K图像,6类,yolo标注
  • 【CocosCreator】electron/Cocos双窗口本地模拟聊天系统
  • Spring事务源码
  • PyTorch API 1
  • 【数据结构】递归与非递归:归并排序全解析
  • 第一章:认识 CAD 图形文件 —— DXF 格式
  • 车载软件架构 --- 赢得汽车软件开发竞赛
  • 好家园房产中介网后台管理完整(python+flask+mysql)
  • Scikit-learn 预处理函数分类详解
  • 【Task02】:四步构建简单rag(第一章3节)
  • 第R6周:LSTM实现糖尿病探索与预测
  • 深度学习核心技巧与实战指南
  • 机器学习中的数据处理技巧
  • Node.js中的Prisma应用:现代数据库开发的最佳实践
  • 关联规则挖掘3:Eclat算法——等价类转换(Equivalence Class Transformation)
  • Simulink实现RELS递推最小二乘算法
  • 【机器学习】什么是损失景观(Loss Landscape)?
  • 漏扫 js 里面包含一些敏感内容 利用二进制加密 保持原始内容不变 又能过漏扫
  • 亚马逊蓝海掘金:以需供比为锚点的精准选品策略
  • 高压柜无线测温:给智能化配电室装上“智能体温监测仪”
  • Leetcode 深度优先搜索 (11)
  • C语言---分隔符、常量、注释、标识符、关键字、空格
  • 笔试——Day44
  • 域名加白怎么做
  • 实战:本地大模型+function Calling,获取北京天气
  • 保姆级Debezium抽取SQL Server同步kafka
  • JSON::Value 功能详解:从三目运算符到高级用法
  • Pytest项目_day20(log日志)