dubbo服务导出源码流程
2024-03-05 17:58:03 0 举报
dubbo服务导出源码流程主要包括以下几个步骤:首先,获取服务的接口定义和实现类,理解服务的基本功能。接下来,根据服务定义和实现,编写服务的导出方法,将服务的功能封装在一个可重用的模块中。然后,使用Java的序列化技术,将服务的接口定义和实现类序列化为二进制格式,以便在网络上传输。最后,将序列化后的服务代码打包成JAR文件,并提供一个可调用的API,使得消费者可以通过这个API来使用服务。在这个过程中,使用反射和动态代理等技术来提高服务的灵活性和可扩展性。
作者其他创作
大纲/内容
if (!SCOPE_LOCAL.equalsIgnoreCase(scope))
useRegistryAsConfigCenterIfNecessary();
ContextFilter
如果没有指定协议,这里面会默认赋值采用dubbo协议
getScopeModel().getDeployer().start();
InjvmProtocol.export
referServices();
registerShutdownHook();
repository.registerProvider(providerModel);
String pathKey = URL.buildKey(getContextPath(protocolConfig) .map(p -> p + \"/\
AllChannelHandler.received
初始化服务元数据
请求结果返回给consumer
Get an instance of registry based on the address of invoker
使用 registry 作为默认配置中心
封装成provider
MonitorFilter
new AllChannelHandler()
switch (state)
MetadataReportFactory metadataReportFactory = applicationModel.getExtensionLoader(MetadataReportFactory.class).getAdaptiveExtension();
init
this.refresh();
exportLocal(url);
返回consumer调用结果
doRegister(url);
notifyExport(exporter);
exportServiceInternal(sc);
构建url路径
for (ProtocolConfig protocolConfig : protocols)
startConfigCenter();
ProfilerServerFilter
DelegateProviderMetaDataInvoker
applicationDeployer.initialize();
final Registry registry = getRegistry(registryUrl);
new HeaderExchangeHandler()
注册provider
ClassLoaderFilter
EchoFilter
useRegistryAsMetadataCenterIfNecessary();
false
ExtensionLoader<ServiceListener> extensionLoader = this.getExtensionLoader(ServiceListener.class); this.serviceListeners.addAll(extensionLoader.getSupportedExtensionInstances());
放入到map缓存
loadServerProperties(protocolServer);
MetadataReportFactory子类
preProcessRefresh();
这里的handler采用了装饰器设计模式
new HeartbeatHandler()
serviceMetadata.setVersion(getVersion(interfaceConfig)); serviceMetadata.setGroup(getGroup(interfaceConfig)); serviceMetadata.setDefaultGroup(getGroup(interfaceConfig)); serviceMetadata.setServiceInterfaceName(getInterface());
openServer(url);
state == ChannelState.RECEIVED
onModuleStarting();
这个方法里面利用反射机制对传入的DemoService.class进行初始化
startMetadataCenter();
metadataReportInstance.init(validMetadataReportConfigs);
super()
protocol.export(invokerDelegate)
使用 registry 作为元数据配置中心
封装成dubboProtocolServer
真正开始调用动态代理类,执行本地sayhello方法
doExportUrls();
service.export();
channel.send(res);
exportServices();
exported();
getApplicationConfigManager()
result.thenApply(Function.identity());
导出内部服务
反射类包装
listener.onStarting(scopeModel);
ServiceDescriptor serviceDescriptor = registerService(interfaceClass);
MultiMessageHandler.received
ExceptionFilter
registry.register(registeredProviderUrl);
将url路径和类注册
通知模块状态改变
spring容器事件发布
循环协议配置导出服务
初始化
HeartbeatHandler.received
netty4
ServiceDescriptor serviceDescriptor = new ReflectionServiceDescriptor(interfaceClazz);
spi加载
postProcessRefresh();
AbstractProxyInvoker
执行动态代理
provider端和consumer端都是同样的handler处理链
if(!serverService)
JavassistProxyFactory.getInvoker
message instanceof Response如果是consumer端则走这个逻辑
启动配置中心
异步执行
setStarting();
初始化没做什么内容
ClassLoaderCallbackFilter
ExportRemote
ZookeeperRegistry.doRegister(url);
Result result = invoker.invoke(invocation);
HeaderExchangeHandler.received
initServerBootstrap(nettyServerHandler);
filterInvoker.invoke(invocation);
createServer(url)
handler.connected(channel);
TraceFilter
GenericFilter
doInvoke
DubboProtocoll.export
this.init();
FilterChainBuilder.invoke
true
serviceDescriptor = repository.registerService(getInterfaceClass());
设置服务已导出
initModuleDeployers();
唤醒Exporter
我们自己实现的方法
initServiceMetadata(provider);
DubboProtocolServer protocolServer = new DubboProtocolServer(server);
RegistryProtocol.export
if (!SCOPE_REMOTE.equalsIgnoreCase(scope))
message instanceof Request如果是provider端走这个逻辑
case CAUGHT:
configManager = (ConfigManager) this.getExtensionLoader(ApplicationExt.class) .getExtension(ConfigManager.NAME);
case SENT:
ExportLocal
spi加载服务监听器
initialized = true;
initialize();
new DecodeHandler()
注册程序关闭钩子
doOpen();
new MultiMessageHandler()
future.whenComplete
getConfigManager()
if (request.isTwoWay())
InvokerWrapper
Result result = invoker.invoke(inv);
NettyServerHandler.channelRead
接收消息处理handler
FailbackRegistry.register(registeredProviderUrl);
doExport();
分配属性
ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>(); service.setInterface(DemoService.class); service.setRef(new DemoServiceImpl()); service.setApplication(new ApplicationConfig(\"dubbo-demo-api-provider\")); service.setRegistry(new RegistryConfig(\"zookeeper://127.0.0.1:2181\")); service.setMetadataReportConfig(new MetadataReportConfig(\"zookeeper://127.0.0.1:2181\")); service.export();
run
message instanceof String
DefaultModuleDeployer.start()
启动dubbo内部模块
handler.disconnected(channel);
通过spi机制根据url获取注册中心
case DISCONNECTED:
DecodeHandler.received
loadApplicationConfigs();
case CONNECTED:
new ExchangeHandlerAdapter()
protocolSPI.export(invoker);
通过spi机制加载
TimeoutFilter
setApplication
启动元数据中心
AbstractInterfaceConfig.processExtraRefresh
AbstractPeer.received
0 条评论
下一页