This sample demonstrates how to build and deploy a Java-based microservice as an Event Trigger in SAP BTP, Kyma runtime using CloudEvents SDK.
The sample uses the CloudEvents SDK to deserialize events. Kyma eventing dispatches the CloudEvents to the Spring Boot microservice. The controller uses io.cloudevents:cloudevents-spring
to deserialize the event.
You can find further details in the Spring documentation
The order.created
event from SAP Commerce Cloud is used as an example of a trigger.
This sample demonstrates how to:
- Set up the development Namespace in the Kyma runtime.
- Configure
kubectl
to use theKUBECONFIG
file downloaded from the Kyma runtime. - Set up Event Triggers in the Kyma runtime.
- Create and deploy a Spring Boot application to handle the event using the CloudEvents SDK library.
- Configure the Spring Boot application to use the Event Trigger.
- Use
kubectl
to verify the state. - Observe the logs using
kubectl
.
- SAP BTP, Kyma runtime instance
- Docker
- make
- Gradle
- Kubernetes tooling
- Java 17+
- SAP Commerce Cloud system connected to the Kyma runtime. You can also use the mock. Refer to this blog post to set up the commerce mock.
-
Ensure that the events are configured for the connected application.
Event Trigger is implemented as a Spring Boot application using gradle as a build tool.
- The CloudEvents library is added as a dependency to build.gradle.
implementation('io.cloudevents:cloudevents-spring:2.3.0')
-
A Plain Old Java Object (POJO) is defined for the
order.created
event. The POJO reflects theorder.created
event definition. -
The controller logic unmarshalls the event payload using the CloudEvent SDK APIs.
-
Configure the
KUBECONFIG
file:-
Set
kubectl
to use theKUBECONFIG
file:export KUBECONFIG={path-to-kubeconfig}
-
-
Build and push the image to the Docker repository:
DOCKER_ACCOUNT={your-docker-account} make push-image
-
Update the image name in the Kubernetes Deployment. Refer to the standard Kubernetes Deployment and Service definitions.
This section details out deploying the application and event trigger as standard Kubernetes resources.
To deploy as Helm chart, please refer to Helm Chart Deployment
-
Deploy the application as a Kubernetes Service.
kubectl -n dev apply -f ./k8s/deployment.yaml
-
Verify that the Pods are up and running:
kubectl -n dev get po -l app=sample-event-trigger-java
The expected result shows that the Pod for the sample-event-trigger-java
Deployment is running:
NAME READY STATUS RESTARTS AGE
sample-event-trigger-java-5fb6856f4d-xk64k 2/2 Running 0 45m
- Create the Event Subscription to receive the
order.created
event from the source. The source is the connected SAP Commerce Cloud system. Update subscription.yaml, Replace{your-connected-commerce-application-name}
with your connected commerce application name as shown in Integration -> Applications
kubectl -n dev apply -f ./k8s/subscription.yaml
- Verify that the Subscription is correctly deployed:
kubectl -n dev get subscriptions.eventing.kyma-project.io
NAME READY AGE
sample-event-trigger-java-subscription true 10m
A Helm Chart definition is also available for developers to try out.
To install the helm chart in dev
namespace, run
helm install kymaapp ../helm-charts/sample-event-trigger-java --set image.repository=gabbi/sample-event-trigger-java:0.0.2 --set subscription.application={your-connected-application-as-shown-in-kyma} --set subscription.eventType=order.created -n dev
To verify, the installed chart
helm -n dev ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
dev-gateway dev 1 2020-09-14 17:34:58.607853163 +0000 UTC deployed gateway-0.0.1
kymaapp dev 1 2020-09-14 20:20:03.464428 +0200 CEST deployed sample-event-trigger-java-0.1.0 1.16.0
-
Simulate the event from the SAP Solution. For the purpose of this tutorial, use the mock to simulate the
order.created
event.-
Navigate to
SAP Commerce Cloud - Events
and send theorder.created
event. -
Observe the logs using kubectl:
kubectl -n dev logs deploy/sample-event-trigger-java -c sample-event-trigger-java
The expected logs look as follows:
datacontenttype: application/json specversion: 1.0 id: c3603dcf-a401-4cb4-b5cb-d928f65cc1a8 source: kyma time: 2022-02-02T14:51:05.633Z type: sap.kyma.custom.mpmockccv2adoptteam2tst2.order.created.v1 OrderCreated{orderCode='0202-2'}
Confirm that event is published and compare the naming by looking at the Kyma component eventing-publisher-proxy
kubectl -n kyma-system logs -l app.kubernetes.io/name=eventing-publisher-proxy -c eventing-publisher-proxy
Delete the created resources:
kubectl -n dev delete -f ./k8s/
or, for helm
helm del kymaapp -n dev