Dapr
是一个用于构建可移植和可靠的微服务的api,支持更快地构建连接的分布式应用程序。
介绍
Dapr(Distributed Application Runtime)
是一个为应用提供分布式能力的运行时- 2019年10月微软主动开源
摘自
Dapr 与应用程序一起作为sidecar运行,在自托管模式下,它是您本地机器上的一个进程
安装
Dapr CLI
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
$ dapr
__
____/ /___ _____ _____
/ __ / __ '/ __ \/ ___/
/ /_/ / /_/ / /_/ / /
\__,_/\__,_/ .___/_/
/_/
===============================
Distributed Application Runtime
Usage:
dapr [command]
Available Commands:
annotate Add dapr annotations to a Kubernetes configuration. Supported platforms: Kubernetes
build-info Print build info of Dapr CLI and runtime
completion Generates shell completion scripts
components List all Dapr components. Supported platforms: Kubernetes
configurations List all Dapr configurations. Supported platforms: Kubernetes
dashboard Start Dapr dashboard. Supported platforms: Kubernetes and self-hosted
help Help about any command
init Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hosted
invoke Invoke a method on a given Dapr application. Supported platforms: Self-hosted
list List all Dapr instances. Supported platforms: Kubernetes and self-hosted
logs Get Dapr sidecar logs for an application. Supported platforms: Kubernetes
mtls Check if mTLS is enabled. Supported platforms: Kubernetes
publish Publish a pub-sub event. Supported platforms: Self-hosted
run Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted
status Show the health status of Dapr services. Supported platforms: Kubernetes
stop Stop Dapr instances and their associated apps. Supported platforms: Self-hosted
uninstall Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hosted
upgrade Upgrades or downgrades a Dapr control plane installation in a cluster. Supported platforms: Kubernetes
version Print Dapr runtime and Cli version.
Flags:
-h, --help help for dapr
--log-as-json Log output in JSON format
-v, --version version for dapr
Use "dapr [command] --help" for more information about a command.
$ dapr --version
CLI version: 1.9.1
Runtime version: n/a
使用
初始化
创建开发环境
$ dapr init
...
x Downloading binaries and setting up components...
...
Dapr runtime installed to /root/.dapr/bin, you may run the following to add it to your path if you want to run daprd direct
...
daprd binary has been installed to /root/.dapr/bin.
dapr_placement container is running.
dapr_redis container is running.
dapr_zipkin container is running.
...
查看启动的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
996de92b0863 daprio/dapr:1.9.6 "./placement" 13 hours ago Up 13 hours 0.0.0.0:50005->50005/tcp, :::50005->50005/tcp dapr_placement
ec513669d37f openzipkin/zipkin "start-zipkin" 13 hours ago Up 13 hours (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp, :::9411->9411/tcp dapr_zipkin
a31c0a61a88e redis:6 "docker-entrypoint.s…" 13 hours ago Up 13 hours 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp dapr_redis
查看创建的目录
$ tree -L 2 ~/.dapr/
/root/.dapr/
├── bin
│ ├── daprd
│ ├── dashboard
│ └── web
├── components
│ ├── pubsub.yaml
│ └── statestore.yaml
└── config.yaml
3 directories, 5 files
$ tree -L 2 /root/.dapr/bin
/root/.dapr/bin
├── daprd
├── dashboard
└── web
└── dist
2 directories, 2 files
- 运行 Dapr sidecar,监听 3500 端口,并使用 init 中创建的默认组件定义(
$HOME/.dapr/components
)
$ dapr run --app-id myapp --dapr-http-port 3500
Starting Dapr with id myapp. HTTP Port: 3500. gRPC Port: 41615
...
使用内置 API
$ curl -i -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore
HTTP/1.1 204 No Content
Server: fasthttp
Traceparent: 00-c0458862350eb569aaad1e7fec694bb8-ef5f0f145d95604c-01
$ curl -i http://localhost:3500/v1.0/state/statestore/name
HTTP/1.1 200 OK
Server: fasthttp
Content-Type: application/json
Content-Length: 13
Etag: 2
Traceparent: 00-4ba7b74e3357d33153172b1e7a187ba8-c10e5f0e31a850e4-01
"Bruce Wayne"
$ docker exec -it dapr_redis redis-cli
127.0.0.1:6379> KEYS *
1) "myapp||name"
127.0.0.1:6379> hgetall "myapp||name"
1) "data"
2) "\"Bruce Wayne\""
3) "version"
4) "2"
- JSON 密钥存储(本地 json 文件保存的示例)
https://docs.dapr.io/zh-hans/getting-started/get-started-component/