Knative Eventing的Broker和Trigger对象可实现对事件进行过滤,事件的消费方可以利用这种机制只获取自己感兴趣的事件,而不是接收所有 Channel 中的事件信息。本文将向Broker发送两种不同的测试包文(报文头有区别),通过在两个Tigger中设置不同的Filter获取对应类型的Event,然后分别发给后台对应的Knative Serving的服务处理。
在Knative中当对一个项目打knative-eventing-injection=enabled标签,Knative Eventing就会为项目提供一个名为default的Broker。
$ oc label namespace knativetutorial knative-eventing-injection=enabled
$ oc -nknativetutorial get broker
NAME READY REASON URL AGE
default True http://default-broker.knativetutorial.svc.cluster.local 65m
$ oc -nknativetutorial get pod
NAME READY STATUS RESTARTS AGE
default-broker-filter-b944cfc76-fn4tp 1/1 Running 0 89m
default-broker-ingress-6f668f8b94-cfjm9 1/1 Running 0 89m
$ oc -nknativetutorial get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default-broker ClusterIP 172.30.106.246 <none> 80/TCP,9090/TCP 89m
default-broker-filter ClusterIP 172.30.177.241 <none> 80/TCP,9090/TCP 89m
default-kne-trigger-kn-channel ExternalName <none> imc-dispatcher.knative-eventing.svc.cluster.local <none> 89m
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: eventingaloha
spec:
template:
metadata:
name: eventingaloha-v1
annotations:
autoscaling.knative.dev/target: "1"
spec:
containers:
- image: quay.io/burrsutter/eventinghello:0.0.1
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: eventingbonjour
spec:
template:
metadata:
name: eventingbonjour-v1
annotations:
autoscaling.knative.dev/target: "1"
spec:
containers:
- image: quay.io/burrsutter/eventinghello:0.0.1
$ oc -n knativetutorial apply -f eventing-aloha-sink.yaml
$ oc -n knativetutorial apply -f eventing-bonjour-sink.yaml
$ oc --namespace knativetutorial get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
eventingaloha ExternalName <none> kourier-internal.knative-serving-ingress.svc.cluster.local <none> 55m
eventingaloha-v1 ClusterIP 172.30.179.201 <none> 80/TCP 55m
eventingaloha-v1-private ClusterIP 172.30.165.146 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 55m
eventingbonjour ExternalName <none> kourier-internal.knative-serving-ingress.svc.cluster.local <none> 54m
eventingbonjour-v1 ClusterIP 172.30.11.132 <none> 80/TCP 55m
eventingbonjour-v1-private ClusterIP 172.30.64.59 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 55m
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
name: my-cjs
spec:
schedule: "*/2 * * * *"
data: '{"message": "From CronJob Source"}'
sink:
ref:
apiVersion: eventing.knative.dev/v1alpha1
kind: Broker
name: default
$ oc -n knativetutorial apply -f event-source-broker.yaml
$ oc -n knativetutorial get cronjobsources
NAME READY AGE
my-cjs True 90s
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
name: helloaloha
spec:
broker: default
#filter:
# attributes:
# type: aloha
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: eventingaloha
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
name: hellobonjour
spec:
broker: default
#filter:
# attributes:
# type: bonjour
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: eventingbonjour
$ oc -n knativetutorial apply -f trigger-helloaloha.yaml
$ oc -n knativetutorial apply -f trigger-hellobonjour.yaml
$ oc -n knativetutorial get trigger
NAME READY REASON BROKER SUBSCRIBER_URI AGE
helloaloha True default http://eventingaloha.knativetutorial.svc.cluster.local 66m
hellobonjour True default http://eventingbonjour.knativetutorial.svc.cluster.local 66m
由于名为my-cjs的CronJobSource会定时向Broker发请求,而且此时2个Trigger都没有Filter,因此2个Knative Serving的服务可同时接收到定时请求。
$ oc -n knativetutorial apply -f trigger-helloaloha.yaml
$ oc -n knativetutorial apply -f trigger-hellobonjour.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: curler
name: curler
spec:
containers:
-- name: curler
image: fedora:29
tty: true
$ oc -n knativetutorial apply -f curler.yaml
$ oc -n knativetutorial exec -it curler -- /bin/bash
curl -v "http://default-broker.knativetutorial.svc.cluster.local" \
-X POST \
-H "Ce-Id: say-hello" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: aloha" \
-H "Ce-Source: mycurl" \
-H "Content-Type: application/json" \
-d '{"key":"from a curl"}'
4.等eventingaloha和eventingalohb的Knative Serving都变为0后,在curler 容器中执行命令,向broker发送Header带有"Ce-Type: bonjour"的请求,然后确认和bonjour相关的服务运行起来。
curl -v "http://default-broker.knativetutorial.svc.cluster.local" \
-X POST \
-H "Ce-Id: say-hello" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: bonjour" \
-H "Ce-Source: mycurl" \
-H "Content-Type: application/json" \
-d '{"key":"from a curl"}'