* dobbo 配置基于URL 驱动的 * * 1. 静态扩展点 加载 指定路径下的文件: * * getExtensionClasses(); 加载下面路径下的 扩展点类 * * META-INF/dubbo * META-INF/dubbo/internal * META-INF/services 下的 org.apache.dubbo.rpc.Protocol中的类 * * 2. ZookeeperDynamicConfigurationFactory * * 自适应扩展点 2.7.7 ExtensionLoader 166 新建 ExtensionLoader() -> 设置 objectFactory * * type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension() * * adaptive 自适应 扩展点 * * ExtensionLoader.getExtensionLoader(Protocol.class) * EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader<T>(type)); * * ZookeeperTransporter * * (1) @Adaptive 类上 * * - name = "默认扩展点的名称"; * - @SPI(name) * * - @SPI("curator") * - public interface ZookeeperTransporter { * - @Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY}) * - ZookeeperClient connect(URL url); * - } * * 设置默认扩展点的名称 * * private void cacheDefaultExtensionName() { * final SPI defaultAnnotation = type.getAnnotation(SPI.class); * if (defaultAnnotation == null) { * return; * } * * String value = defaultAnnotation.value(); * if ((value = value.trim()).length() > 0) { * String[] names = NAME_SEPARATOR.split(value); * if (names.length > 1) { * throw new IllegalStateException("More than 1 default extension name on extension " + type.getName() * + ": " + Arrays.toString(names)); * } * if (names.length == 1) { * cachedDefaultName = names[0]; * } * } * } * * 例2 : @DubboService(proxy = "jdk") 告诉 dubbo 使用 jdk 动态代理 * @DubboService(proxy = "javassist") 告诉 dubbo 使用 javassist 动态代理 * * * (2) @Adaptive 方法上 【动态生成自适应类】 * * (2.1) 根据url 上 参数 例如 com.UserService?protocol=dubbo * class Protocol$Adapter implement Protocol { * protocol = getProtocolByUrl(url); -> dubbo * 通过 getExtension(dubbo); -> DubboProtocol * } * * 生成字节码拼接字符串: * String code = new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate(); * * 参考: Protocol$Adaptive * * (2.2) ExtensionLoader 生成字节码 * private Class<?> createAdaptiveExtensionClass() { * String code = new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate(); * ClassLoader classLoader = findClassLoader(); * org.apache.dubbo.common.compiler.Compiler compiler = ExtensionLoader.getExtensionLoader(org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension(); * // ExtensionLoader 加载 扩展点 * return compiler.compile(code, classLoader); * } * * 3. 激活扩展点 Filter * * 加载 META-INF/dubbo/internal/org.apache.dubbo.rpc.Filter 中 配置的类 * * - @Adaptive(group={provider, consumer}) * 会根据 group 进行过滤满足条件的 * * 4. 服务发布注册 * * ServiceBean -> ServiceConfig, ApplicationEventPublisher, InitializingBean * * ServiceConfig export() 发布服务 * * registry://192.168.0.107:2181/com.xue.he.UserService? * * ServiceConfig.export() -> * Invoker.createInvoker() --> PROXY_FACTORY --> invoker * Protocol --> 获取自适应扩展 private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); * 参照 (2.2) 可知自适应扩展点 协议 生成的是Protocol$Adapter的一个 动态代理的字节码 * --> 根据Url中 protocol 找到对应的 XxxxProtocol 这里 RegistryProtocol * RegistryProtocol * --> registryUrl zookeeper://..... * --> providerUrl dubbo://..... * registry://127.0.0.1:2181/org.apache.dubbo.registry.RegistryService?application=ucenter&dubbo=2.0.2&pid=8200&qos.enable=false®istry=zookeeper&release=2.7.3&simplified=true×tamp=1596093575474 * * 行 607 QosProtocolWrapper(ProtocolListenerWrapper(ProtocolFilterWrapper(DubboProtocol))) 对象 * --- 其实是一个 Protocol$Adaptive * * dubbo_version_2.7.3 * ServiceConfig 607 行跳转至此 * Protocol$Adaptive 56 行 * Exporter<?> exporter = protocol.export(wrapperInvoker); * -> protocol : 自适应的 获取扩展点 * private static final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); * -> ExtensionLoader.getAdaptiveExtension() * -> getAdaptiveExtensionClass() * -> AdaptiveClassCodeGenerator generate() 生成字节码 动态类类 * -> AdaptiveCompiler compile() * * 此时 protocol DubboProtocol / RedisProtocol ... * * * DubboProtocol 323行 serverMap.put(key, createServer(url)); createServer() -> Netty4 的 server * -> .... bindUrl(); * * serverMap key = 10.10.12.51:20880; value= netty4 server [boos Group ; worker Group] * * QosProtocolWrapper 只能监控
dubbo 原理分析整理-服务注册流程梳理
最新推荐文章于 2022-12-21 09:46:56 发布