云原生-dapr 入门(二)hello world分析

上篇聊到为什么dapr暴露的服务http地址是 /v1.0/invoke/nodeapp/method/neworder?

得从dapr的一些概念说起( https://github.com/dapr/docs/tree/master/concepts

  • Building blocks

  • Components

  • Configuration

  • Secret 

  • Hosting environments

Building blocks (构建模块)

它是一个Http或者GRPC API,可以从客户代码调用并且使用一个或多个dapr组件。dapr是一组构建模块组成,具有可扩展性,可以添加新的构建模块。

The diagram below shows how building blocks expose a public API that is called from your code, using components to implement the building blocks' capability

下图显示了Building blocks怎么暴露一个公共API,通过一些组件来实现building Block的能力。

以下是Dapr 提供的Building Block

 

Building BlockEndpointDescription
Service-to-Service Invocation/v1.0/invoke

服务间允许应用通过友好的http或者grpc协议进行相互调用。Dapr提供的endpoint,它充当反向代理与内置服务发现的组合,同时利用内置的分布式跟踪和错误处理。

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/subscribePub/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/bindingsA 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/actorsAn 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. See * Actor Overview
ObservabilityN/ADapr system components and runtime emit metrics, logs, and traces to debug, operate and monitor Dapr system services, components and user applications.
Secrets/v1.0/secretsDapr 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.

Service Invocation (服务调用)

使用服务调用,应用程序可以发现使用gRPC或HTTP标准协议的其他应用程序,并可靠地安全地与之通信。

 

Dapr使用sidecar去中心化架构;使用Dapr调用应用程序,可以在任何dapr实例上使用Invoke API。sidecar编程模型鼓励每个应用程序与自己的Dapr实例交互。Dapr实例实现服务发现并负责与其他服务交互。

1. 服务A通过http/grpc去调用服务B。这次调用转到本地的dapr sidecar发起。

2. Dapr(A)使用为给定托管平台安装的name resolution component组件来发现服务B的位置。

3.Dapr(A)转发请求到Dapr(B)(为了性能,dapr之间通过grpc交互。只有服务与dapr sidecar之间是通过http或者grpc)

4. Dapr(B) 转发请求到指定的服务B的endpoints, 服务B处理它自己的业务逻辑。

5. 服务B返回请求结果给服务A,也是通过 dapr(B)返回去的。

6. Dapr(B)->Dapr(A)

7.服务A通过Dapr(A)接收到返回。

 

以上次的hello world 为例:

  1. NodeJS提供的服务,dapr appId  是nodeapp,python 应用发起的neworder调用:(http://localhost:3500/v1.0/invoke/nodeapp/method/neworder),是通过python的dapr sidecar 发起的

  2. Dapr(python) 发现Node.js 的应用是通过多播DNS组件实现的。

  3. Dapr(python) 转发请求到  sidecar Dapr(nodejs).

  4. Dapr(Nodjs) 转发到Node.js APP.(nodejs 处理业务逻辑,记录请求日志以及持久化orderID到Redis)

Steps 5-7 与上面的一样。

 

服务调用提供多个特性,使得远程方法调用更简单。

Namespaces scoping (支持跨名称空间的调用,在所有支持的托管平台上,Dapr appid 遵循有效的FQDN格式,其中包括目标名称空间)

比如:hello world 的例子,下面的字符串包含应用程序ID nodeapp以及应用程序在生产环境中运行的名称空间

localhost:3500/v1.0/invoke/nodeapp.production/method/neworder

这在Kubernetes集群中的跨名称空间调用中特别有用。

Retries(服务调用在调用失败和瞬态错误发生时执行自动重试)

导致重试的错误是:

1.网络错误,包括端点不可用和拒绝连接。

2.由于在调用/被调用Dapr侧更新证书导致的身份验证错误时。

每次调用重试都以1秒的后退间隔执行,直到阈值3次。通过gRPC建立到目标sidecar的连接有5秒超时。

Service-to-service security

Service access security

Observability: Tracing, logging and metrics(默认情况下,跟踪应用程序之间的所有调用,并收集指标,以便为应用程序提供洞察和诊断,这在生产场景中尤其重要,点击查看更多文章)

 

Pluggable service discovery(Dapr可以在任何托管平台上运行。对于受支持的托管平台,这意味着它们拥有为其开发的 name resolution component 组件,以支持服务发现。例如,Kubernetes名称解析组件使用Kubernetes DNS服务解析集群中运行的其他应用程序的位置)

 

Service Invocation API Specification (  服务调用AIP规范)

Dapr为用户提供了调用具有惟一id的其他应用程序的能力。该功能允许应用程序通过命名的标识符相互交互,并将服务发现的重担放在Dapr运行时上。

HTTP Request

POST/GET/PUT/DELETE http://localhost:<daprPort>/v1.0/invoke/<appId>/method/<method-name>
  1. 可以看到支持POST/GET/PUT/DELETE请求
    
  2.  远程调用服务/v1.0/invoke,/method是默认需要带上的。表示你要访问的应用ID是什么,你要调用的方法名是什么
Parameter<参数>Description<描述>
daprPortthe Dapr port
appIdthe App ID associated with the remote app
method-namethe name of the method or url to invoke on the remote app

好像中心一大段跟开头提的问题,不太相关,但有助于理解服务间的调用。

说到底只是要遵循这个调用API规范来发起调用就行,这个调用我理解是为了方便反向代理使用的,没有很大的玄机。

前面讲到跨命名空间访问,

在支持名称空间的托管平台上,Dapr appid遵循有效的FQDN(完全限定域名 Fully qualified domain name)格式,其中包括目标名称空间。

例如,下面的字符串除了应用程序运行的名称空间(production)外还包含应用程序ID (nodeapp)。

nodeapp.production

服务端日志:

ℹ️  Updating metadata for app command: node app.js
✅  You're up and running! Both Dapr and your app logs will appear here.

== APP == Got a new order! Order ID: 43

== APP == Successfully persisted state.

 

  
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值