Introduction to Dapr Source Code With a Pub-Sub Sample
Tags: dapr, kubernetes, servicemesh
Published: 2020-08-24 08:57:52

# Introduction to Dapr Source Code With a Pub-Sub Sample



<a name="UVBJQ"></a>
## Overview
![](https://cdn.nlark.com/yuque/0/2020/svg/176280/1598163232910-e2fbc481-ba92-44be-8199-98e3d1cfd1e9.svg#align=left&display=inline&height=270&margin=%5Bobject%20Object%5D&originHeight=270&originWidth=367&size=0&status=done&style=none&width=367)<br />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.](https://www.yuque.com/abser/blog/qbw6vc/edit#PBQVJ) 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.<br />

- [分布式应用运行时 Dapr X Kubernetes](https://www.yuque.com/abser/blog/eh303f)
- [微服务 Dapr HelloWorld](https://www.yuque.com/abser/blog/fa0ntp)



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.<br />
<br />I drew some pictures and stored them [here](https://www.yuque.com/abser/architecture/artboards/151574). Hope they are helpful.![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/bo86seho2xyi3cnt9w9f.png)<br />
<br />Below we would run a sample called pub-sub sample and explain how the components like pub-sub could be portable to Dapr.<br />

<a name="5oPXI"></a>
## Prerequisites
We need to get Dapr installed. If not, follow [https://dapr.io/](https://dapr.io/) or my blog. If you have Mac
```bash
$ curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash
```
```bash
 $  dapr init
⌛  Making the jump to hyperspace...
    Downloading binaries and setting up
    components
✅  Success! Dapr is up and running
```
hint: you should have docker installed, too.<br />

<a name="IUhUs"></a>
### Get the code
```bash
$ git clone https://github.com/abserari/go-sdk
```
I wrote a simpler example in [https://github.com/abserari/go-sdk/example/pubsub](https://github.com/abserari/go-sdk)<br />

<a name="LB9ty"></a>
## SDK-Source Code
<a name="18Xxh"></a>
### Configuration
In the configuration, we specify the metadata of the component, like our pub-sub component below:
```yaml
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: messagebus
spec:
  type: pubsub.redis
  metadata:
  - name: redisHost
    value: 192.168.0.253:32220
  - name: redisPassword
    value: "123456"
```
<a name="uey3W"></a>
#### name: messagebus
Dapr needs the name to configure the API<br />![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5wgzo6y1hnwhpedgd4nj.jpg)
<a name="yCss7"></a>
#### spec:
This config is to init the components so Dapr could connect.

<a name="haBUS"></a>
### 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.
```go
client.PublishEvent(ctx, pubsubName, topicName, data)
```
<a name="MqbhI"></a>
### 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.<br />

<a name="rNXVa"></a>
#### config subscription
```go
var sub := &common.Subscription{
	PubsubName: "messagebus",
	Topic:      "neworder",
	Route:      "/orders",
}
```
<a name="XQFiB"></a>
#### add to server
```go
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
    log.Fatalf("error adding topic subscription: %v", err)
}
```


<a name="036Fj"></a>
## 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.
<a name="dPLi8"></a>
### Helpful
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/20q9gw4mvmkvhlnxzhv4.jpg)

<a name="4iHpu"></a>
### Steps
<a name="2yhqT"></a>
#### Preparation

- Get dapr installed



<a name="Bmhb2"></a>
#### 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:
```bash
dapr run --app-id sub \ 
         --app-protocol http \ 
         --app-port 8080 \ 
         --port 3500 \ 
         --log-level debug \ 
         --components-path ./config \ 
         go run sub.go
```


<a name="3581a0e8"></a>
#### 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:
```bash
dapr run --app-id pub \ 
         --log-level debug \ 
         --components-path ./config \ 
         go run pub.go
```


<a name="Xlmpf"></a>
### Result
You would see the log that in the terminal which runs the server(subscriber) code.
```bash
== APP == 2020/08/23 13:21:58 event - PubsubName: messagebus, Topic: demo, ID: 11acaa82-23c4-4244-8969-7360dae52e5d, Data: ping
```
<a name="oz10g"></a>
## Dapr Source Code


<a name="bFSW1"></a>
### Registry
We have to know about the Dapr runtime in the real world. then we tell about how Dapr runtime registers the components.![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5ha5kfsgr8khhgfyfubl.jpg)

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

<br />Then Dapr has a registry struct to store the method that use to new a component instance.
<a name="xQg2u"></a>
### Runtime

<br />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.<br />![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y7gmxygb55so5ihidgqr.jpeg)
<br />I'm happy to chat with your question on dapr or this article. Please comment or contact me.