微软的分布式应用框架 Dapr Helloworld

本文介绍了如何使用 Dapr 运行时构建微服务,重点是 Node.js 应用。内容涵盖 Dapr 的概念、环境设置、HelloWorld 应用的架构、代码下载、状态管理以及使用 Curl、Vscode 和 Postman 进行 POST 和 GET 请求的演示。Dapr 提供了事件驱动的 Pub-Sub 系统、状态管理和多语言支持,旨在简化跨平台的微服务开发。
摘要由CSDN通过智能技术生成

Dapr HelloWorld

Dapr

Distributed Application Runtime. An event-driven, portable runtime for building microservices on cloud and edge.

分布式应用运行时、事件驱动、为云和边缘构建微服务提供便携化运行时。

我现在也不是很懂。

dapr/dapr

GitHub

Dapr is a portable, serverless, event-driven runtime that makes it easy for developers to build resilient, stateless and stateful microservices that run on the cloud and edge and embraces the diversity of languages and developer frameworks.

Dapr codifies the best practices for building microservice applications into open, independent, building blocks that enable you to build portable applications with the language and framework of your choice. Each building block is independent and you can use one, some, or all of them in your application.

比上面的介绍多了 stateless or stateful 的标签。学《计算理论》的时候接触过一些状态机。

”状态时万恶之源“

注意提到了多语言和多开发者框架,我认为这是他选择的通过通信共享信息,即 HTTPGRPC 支持多语言等特性。微软想通过这个设定一个构建微服务应用的规则。从根本上确立你开发的每一个应用的独立性。

下面进行一个 QuickStart

环境

  1. Install Docker(微服务已经离不开容器化了)
  2. Install Dapr
  3. Node.js version 8 or greater(这个 Helloworld 是 node 应用)

On MacOS

Install the latest darwin Dapr CLI to /usr/local/bin

curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash

有条件可以加速

执行初始化(会启动 docker 容器)

$ dapr init
⌛  Making the jump to hyperspace...
Downloading binaries and setting up components
✅  Success! Dapr is up and running

$ docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                        NAMES
b3a5600e672f        redis                "docker-entrypoint.s…"   44 hours ago        Up 44 hours         0.0.0.0:6379->6379/tcp       xenodochial_hofstadter
e5010ba0c33f        daprio/dapr          "./placement"            44 hours ago        Up 44 hours         0.0.0.0:50005->50005/tcp     dapr_placement

HelloWorld

Application Architecture

image.png
能够看到暴露两个 endpoint 是 HTTP 访问,一个创建一个查询。

主要看我们使用 Dapr 的交互。在图中它作为 Runtime

  • 提供 Dapr API 给多语言调用。
  • 提供 状态管理 By state stores

Download Code

下载并进入相应文件夹

git clone https://github.com/dapr/samples.git
cd samples/1.hello-world

Cat app.js

// $ cat app.js 
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------

const express = require('express');
const bodyParser = require('body-parser');
require('isomorphic-fetch');

const app = express();
app.use(bodyParser.json());

const daprPort = process.env.DAPR_HTTP_PORT || 3500;
const stateUrl = `http://localhost:${
     daprPort}/v1.0/state`;
const port = 3000;

app.get('/order', (_req, res) => 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值