This example demonstrates how to publish and consume events using the Event Bus with the event publishing API and push-to-webhook support available in Kyma.
Kyma comes with the NATS Streaming messaging cluster. Instead of interacting with the NATS Streaming cluster directly, use the publish HTTP API and the push subscription support that Kyma offers to enable basic event publishing and event delivery through push notifications to an HTTP endpoint.
Access the event publishing API from the cluster through port 8080
on the following host names:
core-publish.kyma-system
core-publish.kyma-system.svc.cluster.local
- A Docker installation if modification of the image is necessary.
- A kubectl installation.
- Kyma as the target deployment environment.
- An environment to which to deploy the example.
-
Export your environment as variable by replacing the
<environment>
placeholder in the following command and running it:export KYMA_EXAMPLE_ENV="<environment>"
-
Deploy the example subscriber:
kubectl apply -f example-subscriber.yaml -n $KYMA_EXAMPLE_ENV
-
Enable the event activation
kubectl apply -f example-event-activation.yaml -n $KYMA_EXAMPLE_ENV
-
Set your environment on the subscription endpoint
#Linux sed -i "s/<environment>/$KYMA_EXAMPLE_ENV/g" example-subscription.yaml #OSX sed -i '' "s/<environment>/$KYMA_EXAMPLE_ENV/g" example-subscription.yaml
To manually edit example-subscription.yaml, replace the
<environment>
placeholder in the endpoint with your environment. -
Deploy the example subscription:
kubectl apply -f example-subscription.yaml -n $KYMA_EXAMPLE_ENV
-
Deploy the example publisher:
kubectl apply -f example-publisher.yaml -n $KYMA_EXAMPLE_ENV
-
Verify that the example subscriber and publisher Pods are running:
kubectl get pods -n $KYMA_EXAMPLE_ENV
The system eventually applies EgressRule, which allows the example publisher container to download
cURL
. Then, Istio grants access and the example publisher Pod starts. -
Once the example subscriber and publisher Pods are running, access the example publisher container through SSH:
kubectl exec -n $KYMA_EXAMPLE_ENV $(kubectl get pods -n $KYMA_EXAMPLE_ENV -l app=example-publisher --output=jsonpath={.items..metadata.name}) -c example-publisher -i -t -- sh
-
Publish a message:
curl -i \ -H "Content-Type: application/json" \ -X POST http://core-publish.kyma-system:8080/v1/events \ -d '{"source-id": "stage.commerce.kyma.local", "event-type": "test-event-bus", "event-type-version": "v1", "event-time": "2018-11-02T22:08:41+00:00", "data": {"event":{"customer":{"customerID": "1234", "uid": "rick.sanchez@mail.com"}}}}' # or use the fully-qualified event publishing API service name curl -i \ -H "Content-Type: application/json" \ -X POST http://core-publish.kyma-system.svc.cluster.local:3000/v1/events \ -d '{"source-id": "stage.commerce.kyma.local", "event-type": "test-event-bus", "event-type-version": "v1", "event-time": "2018-11-02T22:08:41+00:00", "data": {"event":{"customer":{"customerID": "1234", "uid": "rick.sanchez@mail.com"}}}}'
NOTE: To send multiple events, change the
orderCode
to have a unique value for each POST request.
To view the traces:
- Access the tracing UI.
- Select
webhook-service
. - Click
Find Traces
.
Exit the example publisher container and perform a cleanup using these commands:
exit
kubectl delete deployment,svc,subscription,eventactivation -l example=event-bus -n $KYMA_EXAMPLE_ENV
NATS Streaming does not support topic and subject deletion. So, data such as topics and messages from previous runs accumulates if you repeat the installation steps multiple times.