dapr调研实践

发布时间 2023-12-24 10:12:05作者: 大米粒汪汪叫

Dapr概述

Dapr 是什么?

多运行时架构的微服务框架

分布式能力抽象、一套基准代码、通过端口绑定提供服务

Dapr 是一个在云和边缘构建微服务用的事件驱动的,可移植的运行时。更复杂的来说,"Dapr 是一个可移植的,事件驱动的运行时,使开发人员可以轻松创建在云和边缘上运行的有弹性,无状态和有状态的应用程序,支持语言和开发人员框架的多样性"。

  • 分布式程序(Distributed Application)指的是什么?
    • 跟微服务是一个意思。大的系统分成彼此独立的小的模块,模块和模块之间通过API互相通信,这样每个模块可以用不同的语言开发,一个模块升级的时候不会影响到别的模块。
  • 云和边缘(cloud and edge)指的是什么?
    • 这里的云和边缘指的是 Dapr 的 App 可以跑在 AWS,Azure,GCP 等云服务器上,也可以跑在本地的服务器上。
  • 事件驱动(event-driven)指的是什么?
    • 可以理解成 Dapr 在没有监听(Listening)到请求到来的时候会一直处于待机的状态,什么也不做,只有监听到请求事件来了才开始处理。
  • 可移植(portable)指的是什么?
    • 就是说写的程序和运行的环境,用的中间件无关。比如说原来跑在 AWS 上,现在想跑在 Azure 上,Nosql 数据库原来用 DynamoDB,现在想用 CosmosDB,消息中间件原来用 SNS/SQS,现在想用 Service Bus,没问题,只要在 Dapr 设定这边做一下切换,程序无需改动。
  • 运行时(runtime)指的是什么?
    • 运行时指的是 Dapr 的运行环境。Dapr 的 Control Plane(不知道怎么翻译,直接用英文,就是 Dapr 管理用的模块)会单独启动,同时你的程序在启动的时候 Dapr 会在你的程序上挂一个 Sidecar(所谓的边车模式),你的程序就可以通过 Sidecar 和 Dapr 的 Control Plane 联系上。所有挂有 Dapr Sidecar 的各个微服务之间就可以互相调用了,也可以通过 Dapr 调用各种中间件。
  • 有弹性(resilient)指的是什么?
    • 指的是可以从故障中自动恢复的能力,比如说超时、重试等。不会卡住或陷入一种死循环。
  • 无状态和有状态(stateless and stateful)指的是什么?
    • 无状态指的是一个微服务经过计算得到结果,返回给调用者以后这个值在微服务这边是不保存的(DB,内存等)。有状态指的是在微服务这边要把这个结果保存起来。
  • 支持语言的多样性(the diversity of languages)指的是什么?
    • 指的是 Dapr 有各种语言的 SDK,比如 java,python,go,.net 等都支持。
  • 开发人员框架(developer frameworks)指的是什么?
    • 指的是 Dapr 跟框架无关,你可以把各种语言的各种框架(比如 java 的 spring boot 框架)和 Dapr(API 或者 SDK)混合使用。

对比Layotto

Layotto 是由蚂蚁集团 2021 年开源的一个实现 Multi-Runtime 架构的项目,核心思想是在 Service Mesh 的数据面 (MOSN) 里支持 Dapr API 和 WebAssembly 运行时,实现一个 Sidecar 同时作为 Service Mesh 数据面、多运行时 Runtime、FaaS 运行时。

对比Servicemesh

dapr

Dapr在Servicemesh的基础上进一步扩展Sidecar模式的使用场景,一方面提供天然的多语言解决方案,满足云原生下应用对分布式能力的要求,帮助应用轻量化和serverless化,另一方面提供面向应用的分布式能力抽象层和标准API,为多云、混合云部署提供绝佳的可移植性。

开始入门

1、安装 Dapr命令行(Dapr CLI

#不指定版本安装
#mac os
curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash

#linux os
wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash


#验证dapr安装成功
dapr -h

2、初始化Dapr

安装podman:https://podman.io/docs/installation

brew install podman

podman machine init
podman machine start

podman info

#stop
podman machine stop

使用Podman以自托管模式运行Dapr:https://docs.dapr.io/operations/hosting/self-hosted/self-hosted-with-podman/

dapr init --container-runtime podman

dapr run --app-id myapp --app-port 5000 -- dotnet run
#或者
dapr run --app-id abcd --app-port 14001 --dapr-http-port 14002 --dapr-grpc-port 14003 

dapr uninstall --container-runtime podman --all

 

测试存储

curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "zbs"}]' http://localhost:14002/v1.0/state/statestore

curl http://localhost:14002/v1.0/state/statestore/name

3、dapr管理界面

dapr dashboard running on http://localhost:8080

 

dpar作为边车与应用程序一起运行。在自托管模式下,这意味着它是本地机器上的进程

Dapr初始化包括:

  1. Running a Redis container instance to be used as a local state store and message broker.运行一个Redis容器实例,用作本地状态存储和消息代理。
  2. Running a Zipkin container instance for observability.为可观察性运行Zipkin容器实例。
  3. Creating a default components folder with component definitions for the above.创建一个包含上述组件定义的默认组件文件夹。
  4. Running a Dapr placement service container instance for local actor support.运行Dapr放置服务容器实例以支持本地参与者。

 

 

 

 

参考文档

Dapr官网:https://docs.dapr.io/getting-started/install-dapr-selfhost/

下一代微服务框架 Dapr 中文入门指南:https://blog.csdn.net/easylife206/article/details/126515533

Dapr、Layotto 这种多运行时架构:https://blog.csdn.net/SOFAStack/article/details/125086931

dapr入门:https://ata.atatech.org/articles/11000231642?spm=ata.23639746.0.0.7bbcf6c6ocgw8o