Overview

Introduction to Dapr Source Code With a Pub-Sub Sample - 图1
Hello guys, I’ll tell you about the Dapr’s source code. Dapr is a distributed application runtime. On its official website. It’s introduced as: An event-driven, portable runtime for building microservices on cloud and edge. I have followed Dapr for almost one year. When it was published to the public, I wrote two blogs to introduce how I got familiar with Dapr and recorded my steps.

Today, I’m curious about how Dapr have these features: portable, event-driven, etc. so I dig into the source code and wonder if I could learn something from its architecture.

I drew some pictures and stored them here. Hope they are helpful.image.png

Below we would run a sample called pub-sub sample and explain how the components like pub-sub could be portable to Dapr.

Prerequisites

We need to get Dapr installed. If not, follow https://dapr.io/ or my blog. If you have Mac

  1. $ curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash
  1. $ dapr init
  2. Making the jump to hyperspace...
  3. Downloading binaries and setting up
  4. components
  5. Success! Dapr is up and running

hint: you should have docker installed, too.

Get the code

  1. $ git clone https://github.com/abserari/go-sdk

I wrote a simpler example in https://github.com/abserari/go-sdk/example/pubsub

SDK-Source Code

Configuration

In the configuration, we specify the metadata of the component, like our pub-sub component below:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: messagebus
  5. spec:
  6. type: pubsub.redis
  7. metadata:
  8. - name: redisHost
  9. value: 192.168.0.253:32220
  10. - name: redisPassword
  11. value: "123456"

name: messagebus

Dapr needs the name to configure the API.
dapr-api-name.jpg

spec:

This config is to init the components so Dapr could connect.

Pub.go

After we init the Dapr runtime. we could use pub-sub with go-sdk quickly.

We use this function to publish data to target pub-sub components with pubsubName and specific topicName.

  1. client.PublishEvent(ctx, pubsubName, topicName, data)

Sub.go

It’s a little unfamiliar with the way of subscribing to Dapr. you should have a server with some handler to tell Dapr runtime what topic you care about and how to notify you with the specific route when Dapr has a message to publish to the topic your concern.

config subscription

  1. var sub := &common.Subscription{
  2. PubsubName: "messagebus",
  3. Topic: "neworder",
  4. Route: "/orders",
  5. }

add to server

  1. if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
  2. log.Fatalf("error adding topic subscription: %v", err)
  3. }

Run

This https://github.com/abserari/go-sdk/example/pubsub folder contains two go files that use this go-SDK to invoke the Dapr PubSub API.

Architecture

Introduction to Dapr Source Code With a Pub-Sub Sample - 图4

Steps

Preparation

  • Get Dapr installed

Run Subscriber Server

When we use Dapr PubSub to subscribe, we should have an http or gRPC server to receive the requests from Dapr.

Please change directory to pubsub/ and run the following command:

  1. dapr run --app-id sub \
  2. --app-protocol http \
  3. --app-port 8080 \
  4. --port 3500 \
  5. --log-level debug \
  6. --components-path ./config \
  7. go run sub.go

Run Publisher

Publish is more simple than subscribe. Just Publish the data to target pubsub component with its’ name.

After you start a server by above guide. Please change directory to pubsub/ and run the following command:

  1. dapr run --app-id pub \
  2. --log-level debug \
  3. --components-path ./config \
  4. go run pub.go

Result

You would see the log in the terminal which runs the server(subscriber) code.

  1. == APP == 2020/08/23 13:21:58 event - PubsubName: messagebus, Topic: demo, ID: 11acaa82-23c4-4244-8969-7360dae52e5d, Data: ping

Dapr Source Code

Registry

We have to know about the Dapr runtime in the real world. Then we talk about how Dapr runtime registers the components.

Introduction to Dapr Source Code With a Pub-Sub Sample - 图5

It’s an interface in Dapr source code and the different realize of components is in github.com/dapr/components-contrib.

Then Dapr has a registry struct to store the method that is used to create a new component instance.

Runtime

When we run the Dapr. runtime init all the components with YAML files in your path. if in Kubernetes mode, it would read the yaml from Kubernetes.

Introduction to Dapr Source Code With a Pub-Sub Sample - 图6

I’m happy to chat with on your questions about Dapr or this article. Please comment or contact me.