Knative Connector

Prerequisite

Create Knative Source and Sink

We use the cloudevents-player Knative service as an example. If you do not know how to create cloudevents-player Knative service as source and sink, please follow the steps in this link.

Set up EventMesh Configuration

  1. plugins {
  2. id 'application'
  3. }
  4. application {
  5. mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : 'org.apache.eventmesh.starter.StartUp'
  6. applicationDefaultJvmArgs = [
  7. '-Dlog4j.configurationFile=../eventmesh-runtime/conf/log4j2.xml', '-Deventmesh.log.home=../eventmesh-runtime/logs', '-Deventmesh.home=../eventmesh-runtime', '-DconfPath=../eventmesh-runtime/conf'
  8. ]
  9. }
  10. dependencies {
  11. implementation project(":eventmesh-connector-plugin:eventmesh-connector-knative")
  12. implementation project(":eventmesh-runtime")
  13. }
  1. plugins {
  2. id 'application'
  3. }
  4. application {
  5. mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : 'NULL'
  6. }

Demo

Publish an Event Message from Knative and Subscribe from EventMesh

Step 1: Start an Eventmesh-Runtime Server

  1. $ cd eventmesh-starter
  2. $ ../gradlew -PmainClass=org.apache.eventmesh.starter.StartUp run

Step 2: Publish an Event Message from Knative

  1. $ curl -i http://cloudevents-player.default.127.0.0.1.sslip.io -H "Content-Type: application/json" -H "Ce-Id: 123456789" -H "Ce-Specversion: 1.0" -H "Ce-Type: some-type" -H "Ce-Source: command-line" -d '{"msg":"Hello CloudEvents!"}'

Step 3: Subscribe from an EventMesh

  • Set public static final String EVENTMESH_HTTP_ASYNC_TEST_TOPIC = "messages"; in ExampleConstants.java file.
  1. $ cd eventmesh-examples
  2. $ ../gradlew -PmainClass=org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication run

Expected Result

The following message with data field as Hello CloudEvents! will be printed on the console of EventMesh server.

  1. 2022-09-05 16:37:58,237 INFO [eventMesh-clientManage-] DefaultConsumer(DefaultConsumer.java:60) - \
  2. [{"event":{"attributes":{"datacontenttype":"application/json","id":"123456789","mediaType":"application/json",\
  3. "source":"command-line","specversion":"1.0","type":"some-type"},"data":{"msg":"Hello CloudEvents!"},"extensions":{}},\
  4. "id":"123456789","receivedAt":"2022-09-05T10:37:49.537658+02:00[Europe/Madrid]","type":"RECEIVED"}]

Publish an Event Message from EventMesh and Subscribe from Knative

Step 1: Start an Eventmesh-Runtime Server

  1. $ cd eventmesh-starter
  2. $ ../gradlew -PmainClass=org.apache.eventmesh.starter.StartUp run

Step 2: Publish an Event Message from EventMesh

We use a test program to demo this function.

  1. $ cd eventmesh-connector-plugin/eventmesh-connector-knative
  2. $ ../../gradlew clean test --tests KnativeProducerImplTest.testPublish

Step 3: Subscribe from Knative

  1. $ curl http://cloudevents-player.default.127.0.0.1.sslip.io/messages

Expected Result

The following message with data field as Hello Knative from EventMesh! will be printed on the console of EventMesh server.

  1. 2022-09-05 16:52:41,633 INFO [eventMesh-clientManage-] DefaultConsumer(DefaultConsumer.java:60) - \
  2. [{"event":{"attributes":{"datacontenttype":"application/json","id":"1234","mediaType":"application/json",\
  3. "source":"java-client","specversion":"1.0","type":"some-type"},"data":{"msg":["Hello Knative from EventMesh!"]},"extensions":{}},"id":"1234","receivedAt":"2022-09-05T10:52:32.999273+02:00[Europe/Madrid]","type":"RECEIVED"}]