Cloud Events - Java and Spring

A simple web app written in Java using Spring Cloud Function that can receive CloudEvents. It supports running in two modes:

  1. The default mode has the app reply to your input events with the output event, which is simplest for demonstrating things working in isolation, but is also the model for working with the Knative Eventing Broker concept. The input event is modified assigning a new source and type attribute.

  2. K_SINK mode has the app send events to the destination encoded in $K_SINK, which is useful to demonstrate how folks can synthesize events to send to a Service or Broker when not initiated by a Broker invocation (e.g. implementing an event source). The input event is modified assigning a new source and type attribute.

The application will use $K_SINK-mode whenever the environment variable is specified.

Follow the steps below to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the following commands:

  1. git clone -b "release-0.21" https://github.com/knative/docs knative-docs
  2. cd knative-docs/docs/serving/samples/cloudevents/cloudevents-spring

Before you begin

  • A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
  • Docker installed and running on your local machine, and a Docker Hub account configured (we’ll use it for a container registry).

Build and deploy the sample

To build the image, run:

  1. mvn compile jib:build -Dimage=<image_name>

To deploy the Knative Service, edit the service.yaml file and replace <image> with the deployed image name. Then run:

  1. kubectl apply -f service.yaml

Testing the sample

Get the URL for your Service with:

  1. $ kubectl get ksvc
  2. NAME URL LATESTCREATED LATESTREADY READY REASON
  3. cloudevents-spring http://cloudevents-java.xip.io cloudevents-spring-86h28 cloudevents-spring-86h28 True

Then send a CloudEvent to it with:

  1. $ curl \
  2. -X POST -v \
  3. -H "content-type: application/json" \
  4. -H "ce-specversion: 1.0" \
  5. -H "ce-source: http://curl-command" \
  6. -H "ce-type: curl.demo" \
  7. -H "ce-id: 123-abc" \
  8. -d '{"name":"Dave"}' \
  9. http://cloudevents-java.xip.io

You can also send CloudEvents spawning a temporary curl pod in your cluster with:

  1. $ kubectl run curl \
  2. --image=curlimages/curl --rm=true --restart=Never -ti -- \
  3. -X POST -v \
  4. -H "content-type: application/json" \
  5. -H "ce-specversion: 1.0" \
  6. -H "ce-source: http://curl-command" \
  7. -H "ce-type: curl.demo" \
  8. -H "ce-id: 123-abc" \
  9. -d '{"name":"Dave"}' \
  10. http://cloudevents-java.default.svc

You’ll see on the console:

  1. > POST / HTTP/1.1
  2. > Host: localhost:8080
  3. > User-Agent: curl/7.69.1
  4. > Accept: */*
  5. > content-type: application/json
  6. > ce-specversion: 1.0
  7. > ce-source: http://curl-command
  8. > ce-type: curl.demo
  9. > ce-id: 123-abc
  10. > Content-Length: 15
  11. >
  12. < HTTP/1.1 202 Accepted
  13. < ce-specversion: 1.0
  14. < ce-id: 123-abc
  15. < ce-source: https://github.com/knative/docs/docs/serving/samples/cloudevents/cloudevents-spring
  16. < ce-type: curl.demo
  17. < content-type: application/json
  18. < content-length: 15
  19. <
  20. {"name":"Dave"}

Removing the sample app deployment

To remove the sample app from your cluster, delete the service record:

  1. kubectl delete --filename service.yaml

Removing the sample app deployment

To remove the sample app from your cluster, delete the service:

Run:

  1. kubectl delete --filename service.yaml

Run:

  1. kn service delete cloudevents-spring