从今天开始介绍下dapr(是一个可移植的、事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的、无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言和开发框架);
任何语言,任何框架,任何地方
如今,我们正经历着上云浪潮。 开发人员习惯于 Web + 数据库应用架构(例如经典 3 层设计),但对天然支持分布式的微服务应用架构却感觉陌生。 成为分布式系统专家很难,并且你也不需要这么做。 开发人员希望专注于业务逻辑,同时希望平台为其提供可伸缩的、弹性的、可维护的和云原生架构的其他功能。
这就是Dapr所要解决的。 Dapr 将构建微服务应用的 最佳实践 设计成开放、独立和模块化的方式,让你能够使用任意的开发语言和框架构建可移植的应用程序。 每个构建块都是完全独立的,您可以采用其中一个、多个或全部来构建你的应用。
此外,Dapr 是和平台无关的,这意味着您可以在本地、Kubernetes 集群或者其它集成 Dapr 的托管环境中运行应用程序。 这使得您能够在云平台和边缘计算中运行微服务应用。
使用 Dapr,您可以使用任何语言、框架轻松构建微服务应用,运行在任何地方。
云平台和边缘计算的微服务构建块
在设计微服务应用时,需要考虑很多因素。 Dapr提供了一些常用功能的最佳实践,开发人员可以使用标准模式进行微服务应用的构建,并部署到任意环境中。 Dapr 通过提供分布式构建块来实现此目的。
快速入门
步骤一:安装Dapr CLI脚手架
curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash
备注:如报超时,解决方式:通过浏览器登出github账号,然后重新登录,再次重试下载并安装
步骤二:验证安装
dapr
输出显示如下所示
__
____/ /___ _____ _____
/ __ / __ '/ __ \/ ___/
/ /_/ / /_/ / /_/ / /
\__,_/\__,_/ .___/_/
/_/
===============================
Distributed Application Runtime
Usage:
dapr [command]
Available Commands:
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
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
在初始化dapr之前,本机需要安装docker环境(必备)
dapr init
备注:如报超时,解决方式:通过浏览器登出github账号,然后重新登录,再次重试下载并安装
初始化后,会在安装并运行在三个容器
报如下错误的话,请指定对应版本:
通过dapr init -h 获取相关命令信息
步骤四:验证版本
dapr --version
输出应该看起来像这样:
CLI version: 1.7.0
Runtime version: 1.7.1
步骤五:验证容器正在运行
通过docker的图形化界面:
通过docker命令:
步骤六:验证组件目录已初始化
ls $HOME/.dapr
我的本地运行效果
定义一个组件
第一步:运行Dapr sidecar
dapr run --app-id myapp --dapr-http-port 3500
输出效果:
第二步:保存状态
curl -X POST -H "Content-Type: application/json" -d '[{ "key": "name", "value": "Bruce Wayne"}]' http://localhost:3500/v1.0/state/statestore
第三步:获取状态
curl http://localhost:3500/v1.0/state/statestore/name
输出效果:
第四步:查看状态如何在 Redis 中存储
通过命令进入到容器查看信息:
docker exec -it dapr_redis redis-cli
通docker图形化工具进入容器
通过redis连接工具查看
总结:本篇文章,主要介绍了dapr的入门,如需了解概念信息,请参考官网。通过本地安装dapr,并通过dapr操作redis, 体验了dapr,下篇文章分享下dapr的hello world