目录
1.概述
connector承担的职责 是 zeebe的task跟外部系统通信,其中outboundConnector则是向外部系统发送信息,目前git社区已经支持其中包括 kafka 、rabbitMQ、rest-http、aws、eventBridge等。
在代码实现层面,每一个 outboundConnector都是一个 JobHandler,跟我们对接zeebeClient直接定义的jobHandler没什么差别。
2. outboundConnector的发现、注册
outboundConnector一共有三种加载方式:
1.通过spi加载
2.通过配置文件指定(会覆盖 @OutboundConnector 中指定的属性)
3.通过spring 的beanProcessor扫描注解 @OutboundConnector
实际上这三种方式是同时生效的,相互配合
2.1 通过spi、配置文件发现connector
这两种方式存在一定的联系,因此合在一起说明
关键类: OutboundConnectorDiscovery
从上述代码可知,两种方式是互斥的,只有一种会生效,若在配置中指定了outboundConnector则不再使用spi的加载方式。
在创建 OutboundConnectorFactory 实例时,调用了上述方法,注入了通过两种方式加载得到的connector信息
io.camunda.connector.runtime.outbound.OutboundConnectorRuntimeConfiguration#outboundConnectorFactory
通过spi的加载是一种默认的、自动的方式,官方封装connector都支持该种方式,不再赘述,以下解释下如果使用配置文件的方式:
对于outboundConnector的配置示例:
CONNECTOR_SLACK_FUNCTION=io.camunda.connector.runtime.util.outbound.SlackFunction
CONNECTOR_SLACK_TYPE=io.camunda.connector:SLACK
CONNECTOR_SLACK_INPUT_VARIABLES=foo,bar
CONNECTOR_SLACK_TIMEOUT=10000 # optional
2.2 通过扫描注解 @OutboundConnector 发现
关键方法:io.camunda.zeebe.spring.client.annotation.processor.ZeebeAnnotationProcessorRegistry#postProcessAfterInitialization
以rest-connector为例说明: 由 OutboundConnectorAnnotationProcessor 进行处理
最终connector具体实例被缓存在 DefaultOutboundConnectorFactory 中
综上可知,所有的connector都被缓存在 DefaultOutboundConnectorFactory 中
3.实例化outboundConnector
io.camunda.zeebe.spring.client.annotation.processor.ZeebeClientEventListener#handleStart
借助spring的event机制,在ZeebeClient 实例创建时触发事件,并进行connector的实例化
——>io.camunda.zeebe.spring.client.annotation.processor.ZeebeAnnotationProcessorRegistry#startAll
——>io.camunda.zeebe.spring.client.annotation.processor.ZeebeAnnotationProcessorRegistry#startAll
——>io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorAnnotationProcessor#start
——>io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorManager#start
注意: 在执行 JobWorkerManager#openWorker 方法时,调用了 JobWorkerManager#openWorker 方法,该方法将jobHandler注册到了zeebeClient上
4.outboundConnector逻辑的执行
对接zeebeClient时,具体的执行逻辑封装在 JobHandler 中,对于outBoundConnector而言,则是封装成了 SpringConnectorJobHandler
io.camunda.connector.runtime.outbound.lifecycle.OutboundConnectorManager#openWorkerForOutboundConnector
SpringConnectorJobHandler 是 ConnectorJobHandler 的子类,多了 metrics的收集,核心逻辑还是在 ConnectorJobHandler 中,
在ConnectorJobHandler#handle 调用 inboundFunction的逻辑
之后将response处理后,调用 ConnectorJobHandler#completeJob , 即完成了跟zeebe broker的通信,完成task