Hello World
Dapr Hello World
环境安装
# Linux
$ wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash
# Mac
$ curl -fsSL https://raw.githubusercontent.com/dapr/cli/master/install/install.sh | /bin/bash
然后运行
$ dapr init
⌛ Making the jump to hyperspace...
Downloading binaries and setting up components
✅ Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started
接下来可以指定运行时:
# Install v0.1.0 runtime
$ dapr init --runtime-version 0.1.0
# Check the versions of cli and runtime
$ dapr --version
cli version: v0.1.0
runtime version: v0.1.0
应用运行
接下来将演示如何使

稍后,您将部署一个

Node.js 代码
在
const daprPort = process.env.DAPR_HTTP_PORT || 3500;
const stateStoreName = `statestore`;
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;
app.post("/neworder", (req, res) => {
const data = req.body.data;
const orderId = data.orderId;
console.log("Got a new order! Order ID: " + orderId);
const state = [
{
key: "order",
value: data,
},
];
fetch(stateUrl, {
method: "POST",
body: JSON.stringify(state),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw "Failed to persist state.";
}
console.log("Successfully persisted state.");
res.status(200).send();
})
.catch((error) => {
console.log(error);
res.status(500).send({ message: error });
});
});
该应用程序在这里公开了将接收和处理新订单消息的终结点。它首先记录传入的消息,然后通过将状态数组发布到 /state/<state-store-name>
端点来将订单
res.json({
state: [
{
key: "order",
value: order,
},
],
});
然而,这种方法并不能让你验证消息是否成功持久化。该应用还暴露了一个
app.get("/order", (_req, res) => {
fetch(`${stateUrl}/order`)
.then((response) => {
if (!response.ok) {
throw "Could not get state.";
}
return response.text();
})
.then((orders) => {
res.send(orders);
})
.catch((error) => {
console.log(error);
res.status(500).send({ message: error });
});
});
这将调用
$ dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 node app.js
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
version: v1
你可以看到
const stateStoreName = `statestore`;
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;
然后通过以下调用可以触发该服务:
$ dapr invoke --app-id nodeapp --method neworder --data "{\"data\": { \"orderId\": \"42\" } }"
$ curl -XPOST -d @sample.json -H "Content-Type:application/json" http://localhost:3500/v1.0/invoke/nodeapp/method/neworder
在其他通过
dapr_port = os.getenv("DAPR_HTTP_PORT", 3500)
dapr_url = "http://localhost:{}/v1.0/invoke/nodeapp/method/neworder".format(dapr_port)
n = 0
while True:
n += 1
message = {"data": {"orderId": n}}
try:
response = requests.post(dapr_url, json=message)
except Exception as e:
print(e)
time.sleep(1)
非Docker 环境运行
$ dapr init --slim
在这种模式下,安装了两个不同的二进制文件
$ git clone https://github.com/dapr/samples.git
$ cd samples/hello-dapr-slim
在
app.post("/neworder", bodyParser.json(), (req, res) => {
const data = req.body.data;
const orderId = data.orderId;
console.log("Got a new order! Order ID: " + orderId);
res.status(200).send("Got a new order! Order ID: " + orderId);
});
这里的端点
$ dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 node app.js
Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 9165
You're up and running! Both Dapr and your app logs will appear here.
...
然后可以通过
$ dapr invoke --verb POST --app-id nodeapp --method neworder --data "{\"data\": { \"orderId\": \"41\" } }"
$ dapr invoke --verb POST --app-id nodeapp --method neworder --data '{"data": { "orderId": "41" } }'
$ curl -XPOST -d @sample.json -H "Content-Type:application/json" http://localhost:3500/v1.0/invoke/nodeapp/method/neworder