4

dapr入门与本地托管模式尝试 - 南瓜慢说

 1 year ago
source link: https://www.cnblogs.com/larrydpk/p/17084890.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

随笔 - 132, 文章 - 0, 评论 - 41, 阅读 -

20万

dapr入门与本地托管模式尝试

Dapr是一个可移植的、事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的、无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言和开发框架。Dapr支持的语言很多,包括C++/Go/Java/JavaScript/Python/Rust/.NET/PHP等。

946674-20230202092750193-1338275842.png

目前,可用的构建块如下:

构建块 说明
服务调用 跨服务调用允许进行远程方法调用(包括重试),不管处于任何位置,只需该服务托管于受支持的环境即可。
状态管理 独立的状态管理,使用键/值对作为存储机制,可以轻松的使长时运行、高可用的有状态服务和无状态服务共同运行在您的应用程序中。 状态存储是可插拔的,目前支持使用Azure CosmosDB、 Azure SQL Server、 PostgreSQL、AWS DynamoDB、Redis 作为状态存储介质。
发布订阅 发布事件和订阅主题。
资源绑定 Dapr的Bindings是建立在事件驱动架构的基础之上的。通过建立触发器与资源的绑定,可以从任何外部源(例如数据库,队列,文件系统等)接收和发送事件,而无需借助消息队列,即可实现灵活的业务场景。
Actors Actor模型 = 状态 + 行为 + 消息。一个应用/服务由多个Actor组成,每个Actor都是一个独立的运行单元,拥有隔离的运行空间,在隔离的空间内,其有独立的状态和行为,不被外界干预,Actor之间通过消息进行交互,而同一时刻,每个Actor只能被单个线程执行,这样既有效避免了数据共享和并发问题,又确保了应用的伸缩性。 Dapr 在Actor模式中提供了很多功能,包括并发,状态管理,用于 actor 激活/停用的生命周期管理,以及唤醒 actor 的计时器和提醒器。
可观测性 Dapr记录指标,日志,链路以调试和监视Dapr和用户应用的运行状况。 Dapr支持分布式跟踪,其使用W3C跟踪上下文标准和开放式遥测技术,可以轻松地诊断在生产环境中服务间的网络调用,并发送到不同的监视工具。
秘密 Dapr 提供了密钥管理,支持与公有云和本地的Secret存储集成,以供应用检索使用。

Sidecar与托管

Dapr是以Sidecar的架构模式来提供对应功能的,这也是Kubernetes中很常用的一种模式:

946674-20230202092750709-1408788463.png

Dapr可以自托管,也可以托管在Kubernetes上,一般我们都会托管在Kubernetes上。

Dapr不是服务网格,它们有重叠的部分,也有各自独特的地方,具体如何选择,还要看具体需求。实际上是可以整合在一起使用的。

946674-20230202092750973-948756433.png

2 dapr本地安装

2.1 下载

我们直接从Github下载:https://github.com/dapr/cli/releases ,解压对某个目录,然后添加到环境变量即可:

export PATH=$PATH:/Users/larry/Software/dapr

测试一下,查看版本:

$ dapr --version
CLI version: 1.9.1 
Runtime version: n/a

2.2 在本地环境初始化dapr

首先要确保已经安装了Docker,并成功启动。

$ docker --version
Docker version 19.03.13, build 4484c46d9d

接着执行以下命令进行初始化:

$ dapr init
⌛  Making the jump to hyperspace...
ℹ️  Container images will be pulled from Docker Hub
ℹ️  Installing runtime version 1.9.3
↘  Downloading binaries and setting up components... 
Dapr runtime installed to /Users/larry/.dapr/bin, you may run the following to add it to your path if you want to run daprd directly:
    export PATH=$PATH:/Users/larry/.dapr/bin                                                                                 ✅  Downloading binaries and setting up components...
✅  Downloaded binaries and completed components set up.
ℹ️  daprd binary has been installed to /Users/larry/.dapr/bin.
ℹ️  dapr_placement container is running.
ℹ️  dapr_redis container is running.
ℹ️  dapr_zipkin container is running.
ℹ️  Use `docker ps` to check running containers.
✅  Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started

注意会在用户目录.dapr安装对应的文件。这时再查看version信息就不一样了:

$ dapr --version
CLI version: 1.9.1 
Runtime version: 1.9.3

查看Docker容器情况,也看到启动了多个实例:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                              NAMES
110165beae0d        daprio/dapr:1.9.3   "./placement"            2 minutes ago       Up 2 minutes             0.0.0.0:50005->50005/tcp           dapr_placement
346be0cdcb7e        redis:6             "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes             0.0.0.0:6379->6379/tcp             dapr_redis
42a8279db947        openzipkin/zipkin   "start-zipkin"           3 minutes ago       Up 3 minutes (healthy)   9410/tcp, 0.0.0.0:9411->9411/tcp   dapr_zipkin

3 尝试一下dapr

3.1 启动一个应用

启动一个空白的应用如下,同时也会启动一个Sidecar,并启用3500端口:

dapr run --app-id myapp --dapr-http-port 3500

3.2 状态管理

我们刚才开启了3500端口,接下来可以更新状态,如下:

$ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Larry Deng"}]' http://localhost:3500/v1.0/state/statestore
$ curl -X POST -H "Content-Type: application/json" -d '[{ "key": "webSite", "value": "www.pkslow.com"}]' http://localhost:3500/v1.0/state/statestore

查看状态:

$ curl http://localhost:3500/v1.0/state/statestore/name
"Larry Deng"

$ curl http://localhost:3500/v1.0/state/statestore/webSite
"www.pkslow.com" 

因为状态是通过Redis来保存的,我们可以直接进去Redis查看:

$ docker exec -it dapr_redis redis-cli
127.0.0.1:6379> keys *
1) "myapp||name"
2) "myapp||webSite"
127.0.0.1:6379> hgetall "myapp||name"
1) "data"
2) "\"Larry Deng\""
3) "version"
4) "1"
127.0.0.1:6379> hgetall "myapp||webSite"
1) "data"
2) "\"www.pkslow.com\""
3) "version"
4) "1"
127.0.0.1:6379> exit

删除测使用以下接口:

curl -v -X DELETE -H "Content-Type: application/json" http://localhost:3500/v1.0/state/statestore/name

3.3 dapr dashboard

Dapr为大家提供了管理界面,以下命令来启动:

$ dapr dashboard
Dapr Dashboard running on http://localhost:8080

打开界面,可以看到我们刚才启动的Application:

946674-20230202092751335-247782564.png

还能查看一些组件、配置等信息:

946674-20230202092751726-1982630423.png

我们还可以查看一些Tracing方面的信息,打开:http://localhost:9411/zipkin/

946674-20230202092752330-2042799599.png

因为本次实验只是一个本地版本的dapr,所以还有很多功能没有体验,我们后续有机会再继续学习。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK