Dapr HelloWorld
Dapr
Distributed Application Runtime. An event-driven, portable runtime for building microservices on cloud and edge.
分布式应用运行时、事件驱动、为云和边缘构建微服务提供便携化运行时。
我现在也不是很懂。
dapr/dapr
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 的标签。学《计算理论》的时候接触过一些状态机。
”状态时万恶之源“
注意提到了多语言和多开发者框架,我认为这是他选择的通过通信共享信息,即 HTTP
和 GRPC
支持多语言等特性。微软想通过这个设定一个构建微服务应用的规则。从根本上确立你开发的每一个应用的独立性。
下面进行一个 QuickStart
环境
- Install Docker(微服务已经离不开容器化了)
- Install Dapr
- 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
能够看到暴露两个 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) =>