Knative Connector插件

准备

创建Knative Source和Sink

我们使用 cloudevents-player Knative服务作为例子。如果您不知道如何创建 cloudevents-player Knative服务作为source和sink,请按照这个链接的步骤进行创建。

EventMesh配置文件

  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. }

演示

Knative发布事件消息/EventMesh订阅

步骤1:启动一台EventMesh服务器

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

步骤2:从Knative Source发布一条消息

  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!"}'

步骤3:从EventMesh订阅

  • ExampleConstants.java 文件中设置 public static final String EVENTMESH_HTTP_ASYNC_TEST_TOPIC = "messages";变量
  1. $ cd eventmesh-examples
  2. $ ../gradlew -PmainClass=org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication run

预期结果

以下dataHello CloudEvents!的消息将会打印在EventMesh服务器的控制台上。

  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"}]

EventMessh发布事件消息/Knative订阅

步骤1:启动一台EventMesh服务器

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

步骤2:从EventMesh发布一条消息

我们用Knative Connector的测试程序来演示这个功能。

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

步骤3:从Knative订阅

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

预期结果

以下dataHello Knative from EventMesh!的消息将会打印在EventMesh服务器的控制台上。

  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"}]