尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Apache DolphinScheduler 注册中心(Registry)SPI 扩展指南:插件配置、源码原理与自定义实现

Apache DolphinScheduler 注册中心(Registry)SPI 扩展指南:插件配置、源码原理与自定义实现 Apache DolphinScheduler 注册中心RegistrySPI 扩展指南插件配置、源码原理与自定义实现【免费下载链接】dolphinschedulerApache DolphinScheduler is the modern data orchestration platform. Agile to create high performance workflow with low-code项目地址: https://gitcode.com/gh_mirrors/do/dolphinscheduler注册中心是 Apache DolphinScheduler 集群协调的基石负责 Master/Worker 节点元数据存储、上下线感知、任务负载均衡以及故障转移时的全局锁。本指南以官方 SPI 扩展文档为核心结合当前仓库源码完整讲解注册中心插件的配置方式、参数语义并深入剖析dolphinscheduler-registry-api的 SPI 接口设计与自定义插件扩展流程帮助你从会用进阶到能写。注册中心在 DolphinScheduler 中承担什么职责在深入 SPI 之前先明确注册中心的价值。根据 dolphinscheduler-registry/README.mdDolphinScheduler 使用注册中心完成三件事节点元数据存储与上下线感知存储 Master/Worker 的元数据当节点上下线时其他节点能够及时收到通知Worker 元数据与负载均衡存储 Worker 元数据供 Master 做任务分发时的负载均衡全局锁执行故障转移Failover时获取全局锁保证同一时刻只有一个节点执行主备切换逻辑。因此一个合格的注册中心实现必须满足三个基本能力在订阅路径上数据被新增/删除/更新时通知服务端提供创建/释放全局锁的机制当服务端异常下线时自动删除其元数据即临时节点语义。注册中心 SPI 插件架构总览整个注册中心功能划分为三个模块位于 dolphinscheduler-registry 目录下模块作用dolphinscheduler-registry-api定义注册中心 SPI 标准接口与核心模型是所有插件必须依赖的 API 层dolphinscheduler-registry-plugins具体的注册中心实现插件当前提供 zookeeper、etcd、jdbc 三种dolphinscheduler-registry-all聚合导出模块用于将选定的插件实现打包进发行版新增插件时需在此模块的pom.xml中追加依赖其中dolphinscheduler-registry-api是最关键的 SPI 契约层其核心接口与模型如下源码位于 dolphinscheduler-registry-api/src/main/java/org/apache/dolphinscheduler/registry/apiRegistry接口注册中心 SPI 主接口每个插件都必须实现它ConnectionListener接口负责监听客户端与注册中心之间的连接状态变化状态定义见ConnectionStateSubscribeListener接口负责监听指定前缀下子节点的状态变化事件内容定义见EventADD/REMOVE/UPDATE三种类型RegistryClient对Registry的封装为上层Master/Worker/AlertServer提供心跳注册、节点列表查询、分布式锁等业务级能力RegistryException注册中心统一异常。开箱即用的三种注册中心插件dolphinscheduler-registry-plugins目录下目前提供三种官方插件每种插件均以独立的 Maven 模块发布dolphinscheduler-registry-zookeeper默认注册中心基于 Apache Curator ZooKeeperdolphinscheduler-registry-etcd基于 jetcd 的 etcd 注册中心dolphinscheduler-registry-jdbc基于数据库MySQL/PostgreSQL的注册中心无需额外部署中间件。从当前仓库的默认配置可以印证 ZooKeeper 的默认地位dolphinscheduler-master/src/main/resources/application.yaml 与 dolphinscheduler-worker/src/main/resources/application.yaml 中默认配置均为registry.type: zookeeper而 dolphinscheduler-standalone-server/src/main/resources/application.yaml单机模式默认采用registry.type: jdbc直接复用内置数据库无需额外中间件即可启动。如何使用以 ZooKeeper 为例的插件配置官方 SPI 文档以 ZooKeeper 为例给出了最简配置。需要说明的是配置方式经历了演进早期版本通过registry.properties文件位于dolphinscheduler-service/src/main/resources/registry.properties配置内容为registry.plugin.namezookeeper registry.servers127.0.0.1:2181当前仓库已全面迁移到 Spring Boot 风格的application.yaml配置每个服务master/worker/api维护自己的registry配置块。以 dolphinscheduler-worker/src/main/resources/application.yaml 为例registry: type: zookeeper zookeeper: namespace: dolphinscheduler connect-string: localhost:2181 retry-policy: base-sleep-time: 1s max-sleep: 3s max-retries: 5 session-timeout: 60s connection-timeout: 15s block-until-connected: 15s digest: ~配置完成后启动 DolphinScheduler 集群集群即使用 ZooKeeper 作为注册中心存储服务端元数据。官方 dolphinscheduler-registry-zookeeper/README.md 提供了同样的完整示例。配置前缀规则重要官方 SPI 文档强调了一条贯穿始终的规则所有配置信息的前缀都需要加上registry。例如参数base.sleep.time.ms在 registry 中应配置为registry.base.sleep.time.ms100该规则在当前源码中同样成立各插件属性类均以ConfigurationProperties(prefix registry)绑定配置见 ZookeeperRegistryProperties.java、EtcdRegistryProperties.java因此 YAML 中表现为registry.zookeeper.session-timeout、registry.zookeeper.retry-policy.base-sleep-time这样的层级结构。ZooKeeper 插件参数详解ZooKeeper 插件全部参数定义于 ZookeeperRegistryProperties.java默认值与语义如下配置项默认值说明zookeeper.namespacedolphinscheduler所有节点在 ZooKeeper 中挂载的根路径不同集群可用不同 namespace 隔离zookeeper.connect-string无必填ZooKeeper 连接串如127.0.0.1:2181多节点逗号分隔zookeeper.retry-policy.base-sleep-time1s重试基础休眠时间构建ExponentialBackoffRetry时使用zookeeper.retry-policy.max-sleep3s重试最大休眠时间zookeeper.retry-policy.max-retries3最大重试次数zookeeper.session-timeout60s会话超时时间必须为正数zookeeper.connection-timeout15s连接超时时间必须为正数zookeeper.block-until-connected15s启动时阻塞等待连接成功的最大时长zookeeper.digest无ZooKeeper ACL 认证信息配置后启用digest鉴权并应用CREATOR_ALL_ACL权限该属性类实现了Validator接口在 validate() 中强制校验connectString、namespace非空sessionTimeout、connectionTimeout、blockUntilConnected必须为正数启动时还会将生效配置打印到日志方便核对。从配置到连接的源码链路ZooKeeper 插件的接入完全由 Spring Boot 条件装配驱动。ZookeeperRegistryAutoConfiguration上标注了ConditionalOnProperty(prefix registry, name type, havingValue zookeeper)即只有registry.typezookeeper时该自动配置类才会生效见 ZookeeperRegistryAutoConfiguration.java随后以ConditionalOnMissingBean(value Registry.class)的条件创建ZookeeperRegistry并立即调用start()。ZookeeperRegistry的构造过程见 ZookeeperRegistry.java做了三件事将retry-policy三项参数转换为 Curator 的ExponentialBackoffRetry通过CuratorFrameworkFactory.builder()设置connectString、namespace、sessionTimeoutMs、connectionTimeoutMs若配置了digest则追加digest授权与CREATOR_ALL_ACL的 ACL Provider。start()则调用client.blockUntilConnected(blockUntilConnected)同步等待连接建立超时未连接上会抛出RegistryException并关闭客户端。其他插件配置etcd 与 jdbcetcd 插件在 master/worker/api 的application.yaml中配置完整示例见 dolphinscheduler-registry-etcd/README.mdregistry: type: etcd endpoints: http://etcd0:2379, http://etcd1:2379, http://etcd2:2379 # 以下均有默认值 namespace: dolphinscheduler connection-timeout: 9s retry-delay: 60ms # 单位毫秒 retry-max-delay: 300ms retry-max-duration: 1500ms # SSL 选项按需配置 cert-file: deploy/kubernetes/dolphinscheduler/etcd-certs/ca.crt key-cert-chain-file: deploy/kubernetes/dolphinscheduler/etcd-certs/client.crt key-file: deploy/kubernetes/dolphinscheduler/etcd-certs/client.pem # 认证选项按需配置 user: password: authority: load-balancer-policy: etcd 插件参数定义于 EtcdRegistryProperties.java包括endpoints必填、namespace默认dolphinscheduler、connectionTimeout默认 9s、ttl默认 30s临时节点租约时长、重试策略三项、负载均衡策略以及 SSL/认证相关配置。启用逻辑与 ZooKeeper 一致由EtcdRegistryAutoConfiguration上的ConditionalOnProperty(prefix registry, name type, havingValue etcd)控制见 EtcdRegistryAutoConfiguration.java。jdbc 插件jdbc 插件复用 DolphinScheduler 自身的数据库作为注册中心省去额外部署 ZooKeeper/etcd 的成本单机模式standalone即默认使用它。使用步骤详见 dolphinscheduler-registry-jdbc/README.md初始化数据库表MySQL 执行src/main/resources/mysql_registry_init.sqlPostgreSQL 执行src/main/resources/postgresql_registry_init.sql位于 dolphinscheduler-registry-jdbc 模块下修改配置在 master/worker/api 的application.yaml中设置registry: type: jdbc # 心跳刷新间隔默认 3s不能小于 1s heartbeat-refresh-interval: 3s # 心跳超时时间默认 60s必须大于 3 * heartbeat-refresh-interval session-timeout: 60s # Hikari 连接池配置默认复用 DolphinScheduler 自身数据源 hikari-config: jdbc-url: jdbc:mysql://127.0.0.1:3306/dolphinscheduler username: root password: root maximum-pool-size: 5 connection-timeout: 9000 idle-timeout: 600000jdbc 插件的参数校验逻辑位于 JdbcRegistryProperties.javaheartbeatRefreshInterval不得小于 1ssessionTimeout必须大于3 * heartbeatRefreshInterval未显式配置jdbcRegistryClientName时默认取主机名:server.port。该插件的自动装配类JdbcRegistryAutoConfiguration会以MapperScan扫描注册中心专属的 MapperJdbcRegistryDataMapper、JdbcRegistryLockMapper等并基于hikari-config构建独立的SqlSessionFactory见 JdbcRegistryAutoConfiguration.java。注意若使用 MySQL 作为注册中心需要将mysql-connector-java.jar加入 DolphinScheduler 的 classpath发行版不会内置该驱动参见 jdbc 插件 README 的说明。如何扩展实现自定义注册中心插件官方 SPI 文档指出dolphinscheduler-registry-api定义了实现插件的标准扩展插件时只需实现标准接口即可。文档记载的扩展入口是org.apache.dolphinscheduler.registry.api.RegistryFactory从当前仓库源码看该 SPI 已演进为直接实现Registry接口并通过 Spring Boot 条件装配注册为 Bean的方式下面以当前代码为准确认完整扩展步骤。第一步实现RegistrySPI 接口Registry接口定义在 Registry.java共 12 个核心方法自定义插件必须全部实现方法语义start()启动注册中心客户端并建立连接isConnected()当前是否已连接connectUntilTimeout(Duration)在给定超时内阻塞等待连接成功超时抛RegistryExceptionsubscribe(path, SubscribeListener)订阅路径及其子路径数据变化时回调监听器addConnectionStateListener(ConnectionListener)注册连接状态监听器get(key)读取 key 的值key 不存在时抛异常put(key, value, deleteOnDisconnect)写入键值deleteOnDisconnecttrue表示断连时自动删除临时节点语义delete(key)删除 keychildren(key)返回 key 的子节点集合exists(key)key 是否存在acquireLock(key)/acquireLock(key, timeout)获取指定前缀的全局锁可带超时releaseLock(key)释放全局锁Event事件模型见 Event.java包含四个字段被监听的前缀key、事件发生的完整路径path、路径对应的数据data、事件类型typeADD/REMOVE/UPDATE。第二步参考既有插件的实现范式ZooKeeper 插件的 ZookeeperRegistry.java 是最佳的参考范本其关键实现技巧包括临时节点put()中根据deleteOnDisconnect选择CreateMode.EPHEMERAL或CreateMode.PERSISTENT并配合orSetData()实现存在即更新订阅使用 Curator 的TreeCache监听整棵子树通过内部类EventAdaptor将TreeCacheEvent转换为统一的EventNODE_ADDED→ADD、NODE_UPDATED→UPDATE、NODE_REMOVED→REMOVE分布式锁基于InterProcessMutex实现并用ThreadLocal缓存每个线程已获取的锁由于 etcd/jdbc 无法实现可重入锁这里做了兼容处理——同一线程重复获取同一把锁时直接返回true多次获取只需释放一次断连清理close()时逐个关闭TreeCache与 Curator 客户端。第三步编写自动装配类并接入条件开关参照 ZookeeperRegistryAutoConfiguration.java 的结构Configuration(proxyBeanMethods false) ConditionalOnProperty(prefix registry, name type, havingValue your-plugin) public class YourRegistryAutoConfiguration { Bean ConditionalOnMissingBean(value Registry.class) public YourRegistry yourRegistry(YourRegistryProperties properties) { YourRegistry registry new YourRegistry(properties); registry.start(); return registry; } }同时提供带ConfigurationProperties(prefix registry)的属性类可参考 ZookeeperRegistryProperties.java 或 EtcdRegistryProperties.java这样所有配置项自然遵循registry前缀规则。装配完成后RegistryConfiguration见 RegistryConfiguration.java会自动把该RegistryBean 包装成RegistryClient供上层使用。第四步在聚合模块中登记依赖将新插件依赖添加到dolphinscheduler-registry-all的pom.xml使其随发行版打包同时dolphinscheduler-registry-plugins的父pom.xml中加入新模块。官方 README 明确说明新增注册中心时必须在dolphinscheduler-registry-all的 pom.xml 中追加依赖。第五步复用统一测试基类验证dolphinscheduler-registry-it模块提供了跨插件的统一测试基类RegistryTestCase见 RegistryTestCase.java覆盖isConnected、connectUntilTimeout、subscribeADD/UPDATE/REMOVE 全链路、连接状态监听、get/put/delete、children、exists、加锁解锁等全部 SPI 行为。ZooKeeper 插件的测试 ZookeeperRegistryTestCase.java 通过 Testcontainers 拉起zookeeper:3.8容器并将registry.zookeeper.connect-string指向映射端口后继承该基类即可跑通全部用例。自定义插件继承同一个基类即可获得与其他插件一致的回归保障。上层如何使用注册中心RegistryClient 与节点路径了解 SPI 后再看上层如何消费它。RegistryClient见 RegistryClient.java在构造时即通过RegistryNodeType初始化三个常驻路径/nodes/master、/nodes/worker、/nodes/alert-server见 RegistryNodeType.java。完整的节点路径规划如下用途路径全量节点/nodesMaster 节点/nodes/masterWorker 节点/nodes/workerAlertServer 节点/nodes/alert-serverMaster 节点选举锁/lock/master-nodeMaster 故障转移锁/lock/master-failoverMaster 任务组协调器锁/lock/master-task-group-coordinatorAlertServer 节点锁/lock/alertRegistryClient提供的典型能力包括persistEphemeral(key, value)注册临时心跳节点对应Registry.put(key, value, true)、getServerList(RegistryNodeType)按心跳 JSON 解析出 Master/Worker/AlertServer 的存活列表对应MasterHeartBeat、WorkerHeartBeat等模型、getLock/releaseLock获取全局锁、subscribe订阅节点变化。这也印证了 README 中通知、负载均衡、全局锁三大用途在代码层面的落地。FAQ注册中心连接超时怎么办官方 SPI 文档给出最直接的建议增大相关超时参数。结合上文参数表可按现象分层处理启动即报连接失败如zookeeper connect failed in ...检查registry.zookeeper.connect-string是否正确可达并适当调大block-until-connected默认 15s它是start()阶段阻塞等待连接建立的最大时长见 ZookeeperRegistry.java运行期频繁断连/重连调大session-timeout默认 60s与connection-timeout默认 15s并检查retry-policy三项base-sleep-time、max-sleep、max-retries是否足以应对瞬时网络抖动业务侧等待连接超时Cannot connect to registry in ... s对应Registry.connectUntilTimeout(timeout)的调用点可结合调用方传入的超时时间与上述连接参数综合评估jdbc 插件场景若使用 jdbc 注册中心出现节点被误判离线重点检查heartbeat-refresh-interval默认 3s与session-timeout默认 60s必须大于 3 倍心跳间隔的匹配关系以及hikari-config连接池参数maximum-pool-size、connection-timeout、idle-timeout是否过小。所有超时类参数都遵循本文所述的registry前缀规则配置改动后重启对应服务生效。延伸阅读注册中心设计初衷与模块划分dolphinscheduler-registry/README.mdSPI 契约源码Registry.java、RegistryClient.java、RegistryNodeType.java各插件配置与用法dolphinscheduler-registry-plugins下各插件模块的 READMEzookeeper / etcd / jdbc默认配置示例dolphinscheduler-master/src/main/resources/application.yaml、dolphinscheduler-worker/src/main/resources/application.yaml统一 SPI 测试基类RegistryTestCase.java【免费下载链接】dolphinschedulerApache DolphinScheduler is the modern data orchestration platform. Agile to create high performance workflow with low-code项目地址: https://gitcode.com/gh_mirrors/do/dolphinscheduler创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表