下面给你一个从零到可用的 Java Dubbo 配置使用教程,以 Spring Boot + Dubbo 3.x 为主(目前最常用),也会补充传统 XML 方式作为参考。
Dubbo 是 RPC 框架,用于:
# 本地启动
bin/zkServer.sh start
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
public interface UserService {
String sayHello(String name);
}
@DubboService
public class UserServiceImpl implements UserService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
dubbo:
application:
name: user-provider
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20880
@RestController
public class TestController {
@DubboReference
private UserService userService;
@GetMapping("/hello")
public String hello() {
return userService.sayHello("Dubbo");
}
}
dubbo:
application:
name: user-consumer
registry:
address: zookeeper://127.0.0.1:2181
<dubbo:application name="provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.xxx.UserService"
ref="userService"/>
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="userService"
interface="com.xxx.UserService"/>
| 配置 | 作用 |
|---|---|
@DubboService |
暴露服务 |
@DubboReference |
引用服务 |
registry |
注册中心 |
protocol |
通信协议 |
timeout |
调用超时 |
retries |
重试次数 |
✅ 服务找不到
✅ 端口冲突
protocol.port✅ 版本控制
@DubboService(version = "1.0.0")
@DubboReference(version = "1.0.0")
✅ Dubbo 使用核心三步:
@DubboService@DubboReference如果你需要:
告诉我,我可以直接给你完整 demo。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。