1.概述
dubbo服务启动包括两种方式:dubbo可以有两种启动方式,一种是用bootstrap启动,一种直接用export启动,这里介绍bootstrap启动。
2.源码
2.1 核心类关系
Dubbo核心类关系(回头在补充成类图):
DubboBootstrap
ApplicationModel
Environment
ServiceRepository
ConfigManager
ApplicationConfig
MonitorConfig
ModuleConfig
MetricsConfig
SslConfig
ConfigCenterConfig
MetadataReportConfig
ProviderConfig
ConsumerConfig
ProtocolConfig
RegistryConfig
ServiceConfigBase
ReferenceConfigBase
2.2 源码
dubbo的bootstrap启动其中包括几个步骤:
1.初始化
2.暴露dubbo服务&暴露元信息
3.引用服务
4.等待关闭
其中服务导出/暴露、服务引入等将在其他文章中单独分析
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider"))
.registry(new RegistryConfig("zookeeper://1270.0.1:2181"))
.service(service)
.start()
.await();
/**
* 构造
*/
private DubboBootstrap() {
configManager = ApplicationModel.getConfigManager();
environment = ApplicationModel.getEnvironment();
DubboShutdownHook.getDubboShutdownHook().register();
ShutdownHookCallbacks.INSTANCE.addCallback(new ShutdownHookCallback() {
@Override
public void callback() throws Throwable {
DubboBootstrap.this.destroy();
}
});
}
/**
* 启动
*/
public DubboBootstrap start() {
if (started.compareAndSet(false, true)) {
ready.set(false);
initialize();
if (logger.isInfoEnabled()) {
logger.info(NAME + " is starting...");
}
// 1. export Dubbo Services 暴露dubbo服务
exportServices();
// Not only provider register
if (!isOnlyRegisterProvider() || hasExportedServices()) {
// 2. export MetadataService
exportMetadataService();
//3. Register the local ServiceInstance if required
registerServiceInstance();
}
referServices();
if (asyncExportingFutures.size() > 0) {
new Thread(() -> {
try {
this.awaitFinish();
} catch (Exception e) {
logger.warn(NAME + " exportAsync occurred an exception.");
}
ready.set(true);
if (logger.isInfoEnabled()) {
logger.info(NAME + " is ready.");
}
}).start();
} else {
ready.set(true);
if (logger.isInfoEnabled()) {
logger.info(NAME + " is ready.");
}
}
if (logger.isInfoEnabled()) {
logger.info(NAME + " has started.");
}
}
return this;
}
先贴下代码,回头在细化
/**
* Initialize
*/
private void initialize() {
if (!initialized.compareAndSet(false, true)) {
return;
}
ApplicationModel.initFrameworkExts();
startConfigCenter();
useRegistryAsConfigCenterIfNecessary();
loadRemoteConfigs();
checkGlobalConfigs();
initMetadataService();
initEventListener();
if (logger.isInfoEnabled()) {
logger.info(NAME + " has been initialized!");
}
}
DubboBootstrap.exportServices();
private void exportServices() {
configManager.getServices().forEach(sc -> {
// TODO, compatible with ServiceConfig.export()
ServiceConfig serviceConfig = (ServiceConfig) sc;
serviceConfig.setBootstrap(this);
if (exportAsync) {
ExecutorService executor = executorRepository.getServiceExporterExecutor();
Future<?> future = executor.submit(() -> {
// 导出服务
sc.export();
exportedServices.add(sc);
});
asyncExportingFutures.add(future);
} else {
sc.export();
exportedServices.add(sc);
}
});
}
3.总结
先简单过下代码和系统设计,回头细化后在补充
本文主要探讨了Dubbo服务的两种启动方式,重点关注使用Bootstrap启动的过程。详细分析了启动时的核心类关系,包括DubboBootstrap、ApplicationModel及其相关配置类。服务启动涉及初始化、服务暴露与元信息、服务引用及等待关闭等关键步骤。后续文章将对服务导出和引入进行深入剖析。
837

被折叠的 条评论
为什么被折叠?



