Subscribe to Multiple Event Types ​
Configure a single Subscription to receive events of multiple types.
Prerequisites ​
- You have the Eventing module in your Kyma cluster.
- You have access to Kyma dashboard. Alternatively, if you prefer CLI, you need
kubectlandcurl. - Optionally, you have the CloudEvents Conformance Tool for publishing events.
- You have an inline Function as event sink (see Create and Modify an Inline Function).
TIP
Use Istio sidecar proxies for reliability, observability, and security (see Istio Service Mesh).
Context ​
To subscribe to multiple events, you need a Subscription custom resource (CR). The following example shows how to subscribe to events of two types: order.received.v1 and order.changed.v1.
If you want to subscribe to more event types, add more types to your subscription.
Procedure ​
Create a subscription with multiple event types.
In Kyma dashboard, find the default namespace, go to Configuration and create a Subscription with the following parameters:
- Subscription name:
lastorder-sub - Types:
order.received.v1andorder.changed.v1 - Service:
lastorder(the sink is populated automatically) - Type matching::
standard - Source:
myapp
- Subscription name:
With kubectl, run:
bashcat <<EOF | kubectl apply -f - apiVersion: eventing.kyma-project.io/v1alpha2 kind: Subscription metadata: name: lastorder-sub namespace: default spec: sink: 'http://lastorder.default.svc.cluster.local' source: myapp types: - order.received.v1 - order.changed.v1 EOF
Verify that the subscription is ready.
- In Kyma dashboard, the status must be
READY. - Alternatively, run
kubectl get subscriptions lastorder-sub -o=jsonpath="{.status.ready}"and see if the response istrue.
- In Kyma dashboard, the status must be
Port-forward the Eventing Publisher Proxy to localhost, using port
3000:bashkubectl -n kyma-system port-forward service/eventing-publisher-proxy 3000:80To publish the first event (type
order.received.v1) to trigger your Function, open another terminal window:With the CloudEvents Conformance Tool, run:
bashcloudevents send http://localhost:3000/publish \ --type order.received.v1 \ --id cc99dcdd-6f6d-43d6-afef-d024eb276584 \ --source myapp \ --datacontenttype application/json \ --data "{\"orderCode\":\"3211213\", \"orderStatus\":\"received\"}" \ --yamlWith
curl, run:bashcurl -v -X POST \ -H "ce-specversion: 1.0" \ -H "ce-type: order.received.v1" \ -H "ce-source: myapp" \ -H "ce-eventtypeversion: v1" \ -H "ce-id: cc99dcdd-6f6d-43d6-afef-d024eb276584" \ -H "content-type: application/json" \ -d "{\"orderCode\":\"3211213\", \"orderStatus\":\"received\"}" \ http://localhost:3000/publish
Publish the second event (type
order.changed.v1) to trigger your Function.With the CloudEvents Conformance Tool, run:
bashcloudevents send http://localhost:3000/publish \ --type order.changed.v1 \ --id 94064655-7e9e-4795-97a3-81bfd497aac6 \ --source myapp \ --datacontenttype application/json \ --data "{\"orderCode\":\"3211213\", \"orderStatus\":\"changed\"}" \ --yamlWith
curl, run:bashcurl -v -X POST \ -H "ce-specversion: 1.0" \ -H "ce-type: order.changed.v1" \ -H "ce-source: myapp" \ -H "ce-eventtypeversion: v1" \ -H "ce-id: 94064655-7e9e-4795-97a3-81bfd497aac6" \ -H "content-type: application/json" \ -d "{\"orderCode\":\"3211213\", \"orderStatus\":\"changed\"}" \ http://localhost:3000/publish
To verify that the event was delivered, check the logs of the Function.
In Kyma dashboard, return to the view of your
lastorderFunction and go to Code > Replicas of the Function. Select your replica and under Containers, view the logs.With
kubectl, run:bashkubectl logs \ -n default \ -l serverless.kyma-project.io/function-name=lastorder,serverless.kyma-project.io/resource=deployment \ -c function
Result ​
You see the received event in the logs:
Received event: { orderCode: '3211213', orderStatus: 'received' }
Received event: { orderCode: '3211213', orderStatus: 'changed' }