Send Review Comment to Broker

Image

The main dashboard of our bookstore features a comment section where readers can view comments from others and submit their own through a text input area. While the process appears straightforward - click a button, and the comment is posted - the underlying mechanism is powered by a robust event-driven architecture.

What Knative features will we learn about?

  • Knative Eventing Broker
  • Knative Eventing Sink
  • Knative SinkBinding

What does the deliverable look like?

Image

In simple words: after the user clicks on the submit button in the frontend, the comment will show up in the event-display service.

Implementation

Step 0: Know the basics

Image

In the world of microservices and REST APIs, we often refer to requests as the primary method of communication between services. However, in an event-driven architecture, the smallest unit is an event. Knative Eventing adheres to the CloudEvents specification, making it essential to understand this concept before moving forward. Learn more about CloudEvents here before proceeding!

Step 1: Understand Book Review Service

Image

The book review service is our Node.js API server, playing a crucial role in our event-driven architecture. It’s essential to understand how it operates, as it receives events and processes them appropriately.

Image

Key Concepts: Broker and SinkBinding

Before we dive into the code, let’s clarify two important concepts:

  • Broker: Acts as the central point in the event-driven architecture, routing events to the correct destinations.
  • SinkBinding: This Knative Eventing component automatically injects the Broker’s address into the environment variable K_SINK, ensuring that the Node.js server always has the correct address without manual updates.

You will get a deeper understanding along the way.

Image

Let’s examine the node-server/index.js file, starting with the /add function. When a user submits a comment through the frontend, it is first received by this endpoint.

  1. app.post('/add', async (req, res) => {
  2. try {
  3. const receivedEvent = HTTP.toEvent({headers: req.headers, body: req.body});
  4. const brokerURI = process.env.K_SINK;
  5. if (receivedEvent.type === 'new-review-comment') {
  6. const response = await fetch(brokerURI, {
  7. method: 'POST',
  8. headers: {
  9. 'Content-Type': 'application/cloudevents+json',
  10. 'ce-specversion': receivedEvent.specversion,
  11. 'ce-type': receivedEvent.type,
  12. 'ce-source': receivedEvent.source,
  13. 'ce-id': receivedEvent.id,
  14. },
  15. body: JSON.stringify(receivedEvent.data),
  16. });
  17. }
  18. } catch (err) {
  19. console.error(err);
  20. }
  21. });

Receiving Events: The /add endpoint receives events from the frontend. It uses CloudEvents’ SDK to convert the incoming request into a CloudEvent object:

  1. const receivedEvent = HTTP.toEvent({headers: req.headers, body: req.body});

Determining Broker Address: The Broker’s address is dynamically assigned in the cluster. The Node.js server retrieves this address from the environment variable K_SINK:

  1. const brokerURI = process.env.K_SINK;

You may wonder who told the environment variable about the address? That’s Knative SinkBinding.

Event Filtering: The service checks the event type. If it’s a new-review-comment, it forwards the event to the Broker:

  1. if (receivedEvent.type === 'new-review-comment') {
  2. // logic that forwards the event, see below
  3. }

Forwarding Events Logic: The event is forwarded to the Broker with the appropriate CloudEvent headers:

  1. const response = await fetch(brokerURI, {
  2. method: 'POST',
  3. headers: {
  4. 'Content-Type': 'application/cloudevents+json',
  5. 'ce-specversion': receivedEvent.specversion,
  6. 'ce-type': receivedEvent.type,
  7. 'ce-source': receivedEvent.source,
  8. 'ce-id': receivedEvent.id,
  9. },
  10. body: JSON.stringify(receivedEvent.data),
  11. });

Image

Exploring Other Functions

The Node.js server contains other functions that follow a similar pattern, with detailed comments explaining their functionality:

  • /insert: Receives CloudEvents and inserts the payload into the PostgreSQL database.
  • /comment: Creates a WebSocket connection with the frontend to transport comments from the database.

Step 2: Create Broker

Image

The Broker acts as a router in your event-driven application, receiving events and routing them to the correct destination.

  • 1: Create a new YAML file named node-server/config/200-broker.yaml and add the following content:

node-server/config/200-broker.yaml

  1. apiVersion: eventing.knative.dev/v1
  2. kind: Broker
  3. metadata:
  4. name: bookstore-broker
  • 2: Apply the YAML file:
  1. kubectl apply -f node-server/config/200-broker.yaml

You will see the following output:

  1. broker.eventing.knative.dev/bookstore-broker created

Alternatively, use the Knative CLI kn to create the Broker:

  1. kn broker create bookstore-broker

You will see the following output:

  1. Broker 'bookstore-broker' successfully created in namespace 'default'.

Verify

Running the following command to list the Brokers:

  1. kubectl get brokers

You should see the Broker bookstore-broker with READY status as True.

  1. NAME URL AGE READY REASON
  2. bookstore-broker http://broker-ingress.knative-eventing.svc.cluster.local/default/bookstore-broker 7m30s True

Troubleshooting

If there are issues, use the following command to diagnose:

  1. kubectl describe broker bookstore-broker

Step 3: Create a SinkBinding between the Node.js server and Broker

Image

Hardcoding URLs to connect with Kubernetes services in your application can be limiting and inflexible. A SinkBinding dynamically injects the URL of the Kubernetes service into your application.

Learn more about SinkBinding here and the spec schema!

Create a SinkBinding:

  • 1: Create a new YAML file named node-server/config/300-sinkbinding.yaml and add the following content:

node-server/config/300-sinkbinding.yaml

  1. apiVersion: sources.knative.dev/v1
  2. kind: SinkBinding
  3. metadata:
  4. name: node-sinkbinding
  5. spec:
  6. subject:
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. selector:
  10. matchLabels:
  11. app: node-server
  12. sink: # In this case, the sink is our Broker, which is the eventing service that will receive the events
  13. ref:
  14. apiVersion: eventing.knative.dev/v1
  15. kind: Broker
  16. name: bookstore-broker
  • 2: Apply the YAML file:
  1. kubectl apply -f node-server/config/300-sinkbinding.yaml

You will see the following output:

  1. sinkbinding.sources.knative.dev/node-sinkbinding created

Verify

Running the following command to list the sinkbindings:

  1. kubectl get sinkbindings

You should see the sinkbinding node-sinkbinding with READY status as True.

  1. NAME SINK AGE READY REASON
  2. node-sinkbinding http://broker-ingress.knative-eventing.svc.cluster.local/default/bookstore-broker 2m43s True

Step 4: Create event-display service

Image

Event display is a debugging tool in Knative Eventing that allows you to use it as a temporary destination (a.k.a sink) for your event to go.

Create an Event Display Service:

  • 1: Create a new YAML file named node-server/config/100-event-display.yaml and add the following content:

node-server/config/100-event-display.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: event-display
  5. spec:
  6. replicas: 1
  7. selector:
  8. matchLabels:
  9. app: event-display
  10. template:
  11. metadata:
  12. labels:
  13. app: event-display
  14. spec:
  15. containers:
  16. - name: event-display
  17. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
  18. ports:
  19. - containerPort: 8080
  20. ---
  21. apiVersion: v1
  22. kind: Service
  23. metadata:
  24. name: event-display
  25. spec:
  26. selector:
  27. app: event-display
  28. ports:
  29. - protocol: TCP
  30. port: 80
  31. targetPort: 8080
  32. type: ClusterIP
  • 2: Apply the YAML file:
  1. kubectl apply -f node-server/config/100-event-display.yaml

You will see the following output:

  1. deployment.apps/event-display created
  2. service/event-display created

Verify

Running the following command to list the pods:

  1. kubectl get pods

You should see the pod event-display-XXXXXXX-XXXXX in “Running” status.

  1. NAME READY STATUS RESTARTS AGE
  2. bookstore-frontend-7b879ffb78-9bln6 1/1 Running 0 91m
  3. event-display-55967c745d-bxrgh 1/1 Running 0 4m44s
  4. node-server-644795d698-r9zlr 1/1 Running 0 4m43s

Step 5: Create a Trigger that connects the Broker and event display

Image

A Trigger is able to forward the event to the correct destination based on the CloudEvent’s attributes. It is the connector between the Broker and the event destination.

A Filter in the Trigger will filter the events based on the filter condition. You will specify your filter condition in the Trigger’s YAML file. If no filter is specified, the Trigger will forward all the events that the Broker received.

Image

There is also a concept called Channel in Knative, and generally speaking, you can treat Broker & Trigger without filter the same as Channel & Subscription.

Learn more about Broker & Trigger here!

Create a Trigger:

Image

Here we are creating a Trigger that will send all the events to event-display.

Image

  • 1: Create a new YAML file named node-server/config/200-log-trigger.yaml and add the following content:

node-server/config/200-log-trigger.yaml

  1. # This Trigger subscribes to the Broker and will forward all the events that it received to event-display.
  2. apiVersion: eventing.knative.dev/v1
  3. kind: Trigger
  4. metadata:
  5. name: log-trigger
  6. spec:
  7. broker: bookstore-broker
  8. subscriber:
  9. ref:
  10. apiVersion: v1
  11. kind: Service
  12. name: event-display
  • 2: Apply the YAML file:
  1. kubectl apply -f node-server/config/200-log-trigger.yaml

You will see the following output:

  1. trigger.eventing.knative.dev/log-trigger created

Verify

Running the following command to list the Triggers:

  1. kubectl get triggers

The Trigger log-trigger should have READY status as True.

  1. NAME BROKER SUBSCRIBER_URI AGE READY REASON
  2. log-trigger bookstore-broker http://event-display.default.svc.cluster.local 6m2s True

Validate

Image

Open the logs of the event-display with the following command:

  1. kubectl logs -l=app=event-display -f

Verify

Type something in the comment box in the UI and click the submit button. The comment should appear in the event-display service with the following output:

  1. ☁️ cloudevents.Event
  2. Validation: valid
  3. Context Attributes,
  4. specversion: 1.0
  5. type: new-review-comment
  6. source: bookstore-eda
  7. id: unique-comment-id
  8. datacontenttype: application/json
  9. Extensions,
  10. knativearrivaltime: 2024-05-19T05:27:36.232562628Z
  11. Data,
  12. {
  13. "reviewText": "test"
  14. }

Next Step

Image

Please make sure you pass the Validate test before proceeding.

Go to Deploy ML workflow: Sentiment Analysis 1 - Send Comments to Broker - 图17