Knative Eventing组件是一个实现了事件驱动的一个组件,通过事件发布/订阅的机制,使事件的生产者和消费者充分解耦。当一个事件产生时,事件的发出者并不用关心该事件是否被消费,或者被哪个消费者消费。事件消费者也不依赖与生产者,各自都具有独立性。
Source(源)
Souce是Knative Eventing组件中的消息生产者,负责产生事件然后发出,接收事件的对象可以是Knative Service、K8s Service、Channel或者Broker。示例中是将一个消息发送到了k8s service 如下:
apiVersion: sources.knative.dev/v1
kind: ApiServerSource
metadata:
name: testevents
namespace: apiserversource-example
spec:
serviceAccountName: events-sa
mode: Resource
resources:
- apiVersion: v1
kind: Event
sink:
ref:
apiVersion: v1
kind: Service
name: event-display
Channels(通道)
Channels是Knative定义的k8s资源,定义了单个事件的转发和持久层,Channels支持多种类型的队列,Knative提供默认基于内存的Channels,同时也可以使用一些开源的队列,例如Kafka等。Channels定义如下:
apiVersion: messaging.knative.dev/v1
kind: Channel
metadata:
name: my-channel
namespace: default
Subscription(订阅)
Subscription是事件的消费者,事件Source通过Channels最终会被Subscription来消费。Subscription的定义如下:
apiVersion: messaging.knative.dev/v1
kind: Subscription
metadata:
name: with-dead-letter-sink
spec:
channel:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
name: default
delivery:
deadLetterSink:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: error-handler
subscriber:
uri: http://doesnotexist.default.svc.cluster.local