Skip to main content
版本:v1.9.0

Knative Connector插件

准备

创建Knative Source和Sink

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

EventMesh配置文件

plugins {
id 'application'
}

application {
mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : 'org.apache.eventmesh.starter.StartUp'
applicationDefaultJvmArgs = [
'-Dlog4j.configurationFile=../eventmesh-runtime/conf/log4j2.xml', '-Deventmesh.log.home=../eventmesh-runtime/logs', '-Deventmesh.home=../eventmesh-runtime', '-DconfPath=../eventmesh-runtime/conf'
]
}

dependencies {
implementation project(":eventmesh-connector-plugin:eventmesh-connector-knative")
implementation project(":eventmesh-runtime")
}
plugins {
id 'application'
}

application {
mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : 'NULL'
}

演示

Knative发布事件消息/EventMesh订阅

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

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

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

$ 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";变量
$ cd eventmesh-examples
$ ../gradlew -PmainClass=org.apache.eventmesh.http.demo.sub.SpringBootDemoApplication run

预期结果

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

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

EventMessh发布事件消息/Knative订阅

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

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

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

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

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

步骤3:从Knative订阅

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

预期结果

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

2022-09-05 16:52:41,633 INFO  [eventMesh-clientManage-] DefaultConsumer(DefaultConsumer.java:60) - \
[{"event":{"attributes":{"datacontenttype":"application/json","id":"1234","mediaType":"application/json",\
"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"}]