暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Dapr整体介绍

编程阁楼 2021-04-10
2198
今天我们继续对Dapr框架进行介绍。部分内容摘自官方文档。

Dapr是什么

Dapr helps developers build event-driven, resilient distributed applications. Whether on-premises, in the cloud, or on an edge device, Dapr helps you tackle the challenges that come with building microservices and keeps your code platform agnostic.
简单来说,Dapr(Distributed Application Runtime)是一个事件驱动、可移植的分布式应用运行时,用 于在云上或边缘计算上构建微服务。

Dapr愿景

Any language, any framework, anywhere.
Deploying and running a Dapr enabled application into your Kubernetes cluster is as simple as adding a few annotations to the deployment schemes.

Dapr的架构

在k8s集群中安装成功Dapr会默认安装一下service组件:
  • dapr-dashboard
  • dapr-operator
  • dapr-placement-server
  • dapr-sentry
  • dapr-sidecar-injector
$ kubectl get pods -n dapr-system
NAME                                   READY   STATUS   RESTARTS   AGE
dapr-dashboard-6856779c8f-fpvfk         1/1     Running   0         4d16h
dapr-operator-58887bbd98-zmfg9          1/1     Running   1         4d16h
dapr-placement-server-0                 1/1     Running   3         4d16h
dapr-sentry-74d565cc55-hgcdq            1/1     Running   0         4d16h
dapr-sidecar-injector-965897c99-h9hsh   1/1     Running   0         4d16h

Dapr核心概念1:building block

A building block is an HTTP or gRPC API that can be called from your code and uses one or more Dapr components.
Building blocks address common challenges in building resilient, microservices applications and codify best practices and patterns. Dapr consists of a set of building blocks, with extensibility to add new building blocks.

官方提供的building block有:

Building BlockEndpointDescription
Service-to-service invocation
/v1.0/invoke
Service invocation enables applications to communicate with each other through well-known endpoints in the form of http or gRPC messages. Dapr provides an endpoint that acts as a combination of a reverse proxy with built-in service discovery, while leveraging built-in distributed tracing and error handling.
State management
/v1.0/state
Application state is anything an application wants to preserve beyond a single session. Dapr provides a key/value-based state API with pluggable state stores for persistence.
Publish and subscribe
/v1.0/publish
/v1.0/subscribe
Pub/Sub is a loosely coupled messaging pattern where senders (or publishers) publishes messages to a topic, to which subscribers subscribe. Dapr supports the pub/sub pattern between applications.
Resource bindings
/v1.0/bindings
A binding provides a bi-directional connection to an external cloud/on-premise service or system. Dapr allows you to invoke the external service through the Dapr binding API, and it allows your application to be triggered by events sent by the connected service.
Actors
/v1.0/actors
An actor is an isolated, independent unit of compute and state with single-threaded execution. Dapr provides an actor implementation based on the Virtual Actor pattern which provides a single-threaded programming model and where actors are garbage collected when not in use.
Observability
N/A
Dapr system components and runtime emit metrics, logs, and traces to debug, operate and monitor Dapr system services, components and user applications.
Secrets
/v1.0/secrets
Dapr offers a secrets building block API and integrates with secret stores such as Azure Key Vault and Kubernetes to store the secrets. Service code can call the secrets API to retrieve secrets out of the Dapr supported secret stores.

Dapr核心概念2:Components

Dapr使用CustomResourceDefinition定义和注册组件。所有组件都定义为一个CRD,可以应用于Dapr运行的任何
宿主环境,而不仅仅是Kubernetes。
1. Components定义模板:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: [COMPONENT-NAME]
namespace: [COMPONENT-NAMESPACE]
spec:
type: [COMPONENT-TYPE]
version: v1
initTimeout: [TIMEOUT-DURATION]
ignoreErrors: [BOOLEAN]
metadata:
- name: [METADATA-NAME]
  value: [METADATA-VALUE]
2. 属性说明:
FieldRequiredDetailsExample
apiVersion
Y
The version of the Dapr (and Kubernetes if applicable) API you are calling
dapr.io/v1alpha1
kind
Y
The type of CRD. For components is must always be Component
Component
metadata
-
Information about the component registration

metadata.name
Y
The name of the component
prod-statestore
metadata.namespace
N
The namespace for the component for hosting environments with namespaces
myapp-namespace
spec
-
Detailed information on the component resource

spec.type
Y
The type of the component
state.redis
spec.version
Y
The version of the component
v1
spec.initTimeout
N
The timeout duration for the initialization of the component. Default is 30s
5m
, 1h
, 20s
spec.ignoreErrors
N
Tells the Dapr sidecar to continue initialization if the component fails to load. Default is false
false
spec.metadata
-
A key/value pair of component specific configuration. See your component definition for fields

3. 一个配置案例:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: messagebus
spec:
type: pubsub.mqtt
version: v1
metadata:
  - name: consumerID
    value: "{uuid}"
  - name: url
    value: "tcp://admin:public@localhost:1883"
  - name: qos
    value: 1
  - name: retain
    value: "false"
  - name: cleanSession
    value: "false"
文章转载自编程阁楼,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论