Apache Dubbo 项目教程
1. 项目的目录结构及介绍
Apache Dubbo 是一个高性能的 Java RPC 框架,其 GitHub 仓库的目录结构如下:
dubbo/
├── dubbo-cluster/
├── dubbo-common/
├── dubbo-compatible/
├── dubbo-compiler/
├── dubbo-config/
├── dubbo-configcenter/
├── dubbo-container/
├── dubbo-demo/
├── dubbo-dependencies/
├── dubbo-dependencies-bom/
├── dubbo-distribution/
├── dubbo-filter/
├── dubbo-maven-plugin/
├── dubbo-metadata/
├── dubbo-metrics/
├── dubbo-monitor/
├── dubbo-registry/
├── dubbo-remoting/
├── dubbo-rpc/
├── dubbo-serialization/
├── dubbo-test/
├── dubbo-validation/
├── dubbo-bom/
├── dubbo-build-tools/
├── dubbo-filter/
├── dubbo-plugin/
├── dubbo-registry/
├── dubbo-remoting/
├── dubbo-rpc/
├── dubbo-serialization/
├── dubbo-test/
├── dubbo-validation/
└── dubbo-bom/
主要目录介绍:
- dubbo-cluster: 集群容错模块。
- dubbo-common: 公共工具模块。
- dubbo-config: 配置模块。
- dubbo-demo: 示例代码。
- dubbo-dependencies: 依赖管理模块。
- dubbo-distribution: 发布包模块。
- dubbo-registry: 注册中心模块。
- dubbo-remoting: 远程通信模块。
- dubbo-rpc: RPC 协议模块。
- dubbo-serialization: 序列化模块。
2. 项目的启动文件介绍
Dubbo 项目的启动通常涉及以下几个步骤:
-
定义服务接口:
package org.apache.dubbo.samples.api; public interface GreetingsService { String sayHi(String name); }
-
实现服务接口:
package org.apache.dubbo.samples.provider; import org.apache.dubbo.samples.api.GreetingsService; public class GreetingsServiceImpl implements GreetingsService { @Override public String sayHi(String name) { return "Hi, " + name; } }
-
配置服务提供者:
<dubbo:application name="hello-world-app"/> <dubbo:registry address="multicast://224.5.6.7:1234"/> <dubbo:protocol name="dubbo" port="20880"/> <dubbo:service interface="org.apache.dubbo.samples.api.GreetingsService" ref="greetingsService"/> <bean id="greetingsService" class="org.apache.dubbo.samples.provider.GreetingsServiceImpl"/>
-
启动服务提供者:
import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ProtocolConfig; import org.apache.dubbo.config.ServiceConfig; import org.apache.dubbo.samples.api.GreetingsService; import org.apache.dubbo.samples.provider.GreetingsServiceImpl; public class Provider { public static void main(String[] args) throws Exception { ServiceConfig<GreetingsService> service = new ServiceConfig<>(); service.setApplication(new ApplicationConfig("hello-world-app")); service.setRegistry(new RegistryConfig("multicast://224.5.6.7:1234")); service.setProtocol(new ProtocolConfig("dubbo", 20880)); service.setInterface(GreetingsService.class); service.setRef(new GreetingsServiceImpl()); service.export(); System.out.println("Provider started"); System.in.read(); // 按任意键退出 } }