Go
Dapr offers packages to help with the development of Go pluggable components.
Prerequisites
- Go 1.19 or later
- Dapr 1.9 CLI or later
- Initialized Dapr environment
- Linux, Mac, or Windows (with WSL)
Note
Development of Dapr pluggable components on Windows requires WSL. Not all languages and SDKs expose Unix Domain Sockets on “native” Windows.
Application creation
Creating a pluggable component starts with an empty Go application.
mkdir example
cd component
go mod init example
Import Dapr packages
Import the Dapr pluggable components SDK package.
go get github.com/dapr-sandbox/components-go-sdk@v0.1.0
Create main package
In main.go
, import the Dapr plugggable components package and run the application.
package main
import (
dapr "github.com/dapr-sandbox/components-go-sdk"
)
func main() {
dapr.MustRun()
}
This creates an application with no components. You will need to implement and register one or more components.
Implement and register components
- Implementing an input/output binding component
- Implementing a pub/sub component
- Implementing a state store component
Note
Only a single component of each type can be registered with an individual service. However, multiple components of the same type can be spread across multiple services.
Test components locally
Create the Dapr components socket directory
Dapr communicates with pluggable components via Unix Domain Sockets files in a common directory. By default, both Dapr and pluggable components use the /tmp/dapr-components-sockets
directory. You should create this directory if it does not already exist.
mkdir /tmp/dapr-components-sockets
Start the pluggable component
Pluggable components can be tested by starting the application on the command line.
To start the component, in the application directory:
go run main.go
Configure Dapr to use the pluggable component
To configure Dapr to use the component, create a component YAML file in the resources directory. For example, for a state store component:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <component name>
spec:
type: state.<socket name>
version: v1
metadata:
- name: key1
value: value1
- name: key2
value: value2
Any metadata
properties will be passed to the component via its Store.Init(metadata state.Metadata)
method when the component is instantiated.
Start Dapr
To start Dapr (and, optionally, the service making use of the service):
dapr run --app-id <app id> --resources-path <resources path> ...
At this point, the Dapr sidecar will have started and connected via Unix Domain Socket to the component. You can then interact with the component either:
- Through the service using the component (if started), or
- By using the Dapr HTTP or gRPC API directly
Create container
Pluggable components are deployed as containers that run as sidecars to the application (like Dapr itself). A typical Dockerfile
for creating a Docker image for a Go application might look like:
FROM golang:1.20-alpine AS builder
WORKDIR /usr/src/app
# Download dependencies
COPY go.mod go.sum ./
RUN go mod download && go mod verify
# Build the application
COPY . .
RUN go build -v -o /usr/src/bin/app .
FROM alpine:latest
# Setup non-root user and permissions
RUN addgroup -S app && adduser -S app -G app
RUN mkdir /tmp/dapr-components-sockets && chown app /tmp/dapr-components-sockets
# Copy application to runtime image
COPY --from=builder --chown=app /usr/src/bin/app /app
USER app
CMD ["/app"]
Build the image:
docker build -f Dockerfile -t <image name>:<tag> .
Note
Paths for COPY
operations in the Dockerfile
are relative to the Docker context passed when building the image, while the Docker context itself will vary depending on the needs of the application being built. In the example above, the assumption is that the Docker context is the component application directory.
Next steps
- Advanced techniques with the pluggable components Go SDK
- Learn more about implementing: