文档
目标
演示 Dubbo + Spring Boot + Nacos 的完整 RPC 调用链路:定义 API → 提供者实现 → 消费者调用。
完整代码
项目结构
dubbo-demo/
├── api/ # 公共 API 模块
│ └── GreetingService.java
├── provider/ # 服务提供者
│ └── application.yml
│ └── ProviderApplication.java
│ └── GreetingServiceImpl.java
└── consumer/ # 服务消费者
└── application.yml
└── ConsumerApplication.java
└── GreetingController.java
1. API 模块(api/GreetingService.java)
package com.example.api;
public interface GreetingService {
String sayHello(String name);
GreetingResponse greet(GreetingRequest request);
}
package com.example.api;
import java.io.Serializable;
public class GreetingRequest implements Serializable {
private String name;
private String language;
public GreetingRequest() {}
public GreetingRequest(String name, String language) {
this.name = name;
this.language = language;
}
// getters & setters ...
}
public class GreetingResponse implements Serializable {
private String message;
private long timestamp;
public GreetingResponse() {}
public GreetingResponse(String message, long timestamp) {
this.message = message;
this.timestamp = timestamp;
}
// getters & setters ...
}
2. 服务提供者
provider/application.yml
server:
port: 8081
dubbo:
application:
name: demo-provider
registry:
address: nacos://localhost:8848
protocol:
name: dubbo
port: 20880
provider:
timeout: 3000
retries: 0
loadbalance: roundrobin
provider/GreetingServiceImpl.java
package com.example.provider;
import com.example.api.GreetingRequest;
import com.example.api.GreetingResponse;
import com.example.api.GreetingService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0", group = "demo")
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "! (from Dubbo Provider)";
}
@Override
public GreetingResponse greet(GreetingRequest request) {
String msg = switch (request.getLanguage()) {
case "zh" -> "你好, " + request.getName();
case "ja" -> "こんにちは, " + request.getName();
default -> "Hello, " + request.getName();
};
return new GreetingResponse(msg, System.currentTimeMillis());
}
}
provider/ProviderApplication.java
package com.example.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
3. 服务消费者
consumer/application.yml
server:
port: 8082
dubbo:
application:
name: demo-consumer
registry:
address: nacos://localhost:8848
consumer:
timeout: 3000
check: false # 启动时不检查提供者
consumer/GreetingController.java
package com.example.consumer;
import com.example.api.GreetingRequest;
import com.example.api.GreetingResponse;
import com.example.api.GreetingService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class GreetingController {
@DubboReference(version = "1.0.0", group = "demo", timeout = 5000)
private GreetingService greetingService;
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return greetingService.sayHello(name);
}
@GetMapping("/greet")
public GreetingResponse greet(@RequestParam String name, @RequestParam String lang) {
return greetingService.greet(new GreetingRequest(name, lang));
}
}
consumer/ConsumerApplication.java
package com.example.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
运行步骤
# 1. 启动 Nacos
cd nacos/bin && sh startup.sh -m standalone
# 2. 启动 Provider
cd provider && mvn spring-boot:run
# 3. 启动 Consumer
cd consumer && mvn spring-boot:run
# 4. 测试 RPC 调用
curl "http://localhost:8082/api/hello?name=Alice"
# Hello, Alice! (from Dubbo Provider)
curl "http://localhost:8082/api/greet?name=张三&lang=zh"
# {"message":"你好, 张三","timestamp":1716500000000}
# 5. Nacos 控制台查看服务列表
# http://localhost:8848/nacos → 服务管理 → 服务列表
预期输出
Provider 启动日志:
[DUBBO] DubboBootstrap started
Consumer 调用:
Hello, Alice! (from Dubbo Provider)
{"message":"你好, 张三","timestamp":1716500000000}
Nacos 服务列表:
demo-provider → 1 个健康实例 (192.168.1.x:20880)
demo-consumer → 1 个健康实例
关键点
@DubboService暴露服务,@DubboReference引用远程服务version和group用于服务隔离和灰度发布- 接口定义在独立
api模块,provider 和 consumer 共享 - Dubbo 默认注册到
/dubbo路径下,与 Spring Cloud/隔离