Dapr 介绍

发布时间: 更新时间: 总字数:1089 阅读时间:3m 作者: IP上海 分享 网址

Dapr是一个用于构建可移植和可靠的微服务的api,支持更快地构建连接的分布式应用程序。

介绍

  • Dapr(Distributed Application Runtime) 是一个为应用提供分布式能力的运行时
  • 2019年10月微软主动开源
dapr arch

摘自

  • Dapr 主要特性

    • 服务调用
    • 状态管理
    • 发布订阅
    • 资源绑定
    • 内置可观测性支持
    • 密钥管理
    • 跨平台虚拟 Actor
  • 分布式应用需求

    • 服务发现、服务注册、服务治理、弹性伸缩
    • 服务调用:重试、熔断、限流
    • 数据状态:数据读写、缓存、一致性
    • 发布订阅:服务解耦、异步处理
    • 配置管理:服务参数、中间件参数、账号信息登
  • Dapr 通过 SideCar 实现服务发现

    • k8s: Service(k8s DNS 服务解析)
    • 虚拟机:使用 mDNS 轮训负载均衡
  • Dapr 以 SideCar 架构的方式公开 API

    • 引入 SideCar 前,业务逻辑和非业务逻辑代码(框架、SDK)混合在一个进程内
    • 引入 SideCar 后,客户端 SDK 的功能剥离,业务进程专注于业务逻辑;SDK 的大部分功能被拆解为独立进程,以 SideCar 的模式运行
    • SideCar 使 Service Mesh 实现了业务和框架逻辑剥离的模板

Dapr 与应用程序一起作为sidecar运行,在自托管模式下,它是您本地机器上的一个进程

安装

Dapr CLI

wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
  • help
$ 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.
  • version
$ 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"
  • 查看状态如何在 Redis 中存储
$ 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/

Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数