跳到内容

将 Knative Service 用作源

在本教程中,您将使用 CloudEvents Player 应用程序来展示 Knative Eventing 的核心概念。在本教程结束时,您应该会拥有一个如下图所示的架构

The CloudEvents Player acts as both a source and a sink for CloudEvents

上图是 Knative in Action 中的图 6.6。

创建您的第一个源

CloudEvents Player 通过将 Broker 的名称作为环境变量 BROKER_NAME 接收,充当 CloudEvents 的源。如果 Broker 位于不同的命名空间中,也可以设置 BROKER_NAMESPACE 环境变量。或者,您也可以只使用 BROKER_URI

您将通过 CloudEvents Player 应用程序向 Broker 发送 CloudEvents。

创建 CloudEvents Player 服务

运行命令

kn service create cloudevents-player \
--image quay.io/ruben/cloudevents-player:latest

预期输出

Service 'cloudevents-player' created to latest revision 'cloudevents-player-00001' is available at URL:
http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io
  1. 将以下 YAML 复制到名为 cloudevents-player.yaml 的文件中

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: cloudevents-player
    spec:
      template:
        metadata:
          annotations:
            autoscaling.knative.dev/min-scale: "1"
        spec:
          containers:
            - image: quay.io/ruben/cloudevents-player:latest
    

  2. 通过运行命令应用 YAML 文件

    kubectl apply -f cloudevents-player.yaml
    

    预期输出

    service.serving.knative.dev/cloudevents-player created
    

该服务现在正在运行,但它不知道 Broker 的位置,因此让我们在服务和 Broker 之间创建一个 SinkBinding

运行命令

kn source binding create ce-player-binding --subject "Service:serving.knative.dev/v1:cloudevents-player" --sink broker:example-broker

预期输出

Sink binding 'ce-player-binding' created in namespace 'default'.
  1. 将以下 YAML 复制到名为 cloudevents-player-binding.yaml 的文件中

    apiVersion: sources.knative.dev/v1
    kind: SinkBinding
    metadata:
      name: ce-player-binding
    spec:
      sink:
        ref:
          apiVersion: eventing.knative.dev/v1
          kind: Broker
          name: example-broker
      subject:
        apiVersion: serving.knative.dev/v1
        kind: Service
        name: cloudevents-player
    

  2. 通过运行命令应用 YAML 文件

    kubectl apply -f cloudevents-player-binding.yaml
    

    预期输出

    sinkbinding.sources.knative.dev/ce-player-binding created
    

检查 CloudEvents Player

您可以使用 CloudEvents Player 发送和接收 CloudEvents。 如果您在浏览器中打开服务 URL,将显示 创建事件 表单。

服务 URL 为 http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io,例如,对于 kind,为 http://cloudevents-player.default.127.0.0.1.sslip.io

The user interface for the CloudEvents Player

这些字段是什么意思?
字段 描述
事件 ID 一个唯一的 ID。单击循环图标以生成一个新的。
事件类型 一个事件类型。
事件源 一个事件源。
Specversion 表示您正在使用的 CloudEvents 规范(应始终为 1.0)。
消息 CloudEvent 的 data 部分,一个承载您要传递的数据的有效负载。

有关 CloudEvents 规范的更多信息,请查看 CloudEvents 规范

发送事件

尝试使用 CloudEvents Player 界面发送事件

  1. 用您想要的任何数据填写表单。
  2. 确保您的事件源不包含任何空格。
  3. 单击 发送事件

CloudEvents Player Send

单击 会向您显示 Broker 看到的 CloudEvent。

Event Details

想通过命令行发送事件吗?

作为 Web 表单的替代方案,也可以使用命令行发送/查看事件。

发布事件

curl -i http://cloudevents-player.default.${LOADBALANCER_IP}.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!"}'

查看事件

curl http://cloudevents-player.default.${LOADBALANCER_IP}.sslip.io/messages

“状态”列中的 图标表示事件已发送到我们的 Broker...但事件去哪了?目前,无处可去!

Broker 只是事件的接收器。为了将事件发送到任何地方,您必须创建一个 Trigger,它监听您的事件并将其放置在某个位置。而且,您很幸运;您将在下一页创建您的第一个 Trigger!

我们使用分析和 cookie 来了解网站流量。有关您使用我们网站的信息会与 Google 共享以达到此目的。了解更多。