这个架构图很清晰,通过这个helloworld可以对dapr有个感性的认识,整个过程也就半小时吧,建议实操一把。
- 安装dapr
参考官网:Install the Dapr CLI | Dapr Docspowershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
-
初始化
dapr init --slim
-
创建Node.js后端微服务
git clone https://github.com/dapr/samples.git cd samples/hello-dapr-slim
实例使用了Express,代码很简单
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运行Node.js微服务
先安装依赖npm install
用Dapr运行,感觉像托管给Dapr,增加了一层端口
dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 node app.js
-
测试微服务
测试方法很多,就是Post一个Http请求,例如用cmd命令行:
dapr invoke --verb POST --app-id nodeapp --method neworder --data "{\"data\": { \"orderId\": \"41\" } }"
也可以用PostMan, VsCode的Rest Client插件
-
查看微服务
dapr list
可以看到Dapr还支持gRPC
-
清理微服务
dapr stop --app-id nodeapp
参考:How-To: Run Dapr in self-hosted mode without Docker | Dapr Docs
小结:Slim方式初始化功能有限,还需要自己配置状态管理,适合没有Docker环境的小伙伴