❯ dapr init ⌛ Making the jump to hyperspace... ↑ Downloading binaries and setting up components... Dapr runtime installed to /Users/rick/.dapr/bin, you may run the following to add it to your path if you want to run daprd directly: export PATH=$PATH:/Users/rick/.dapr/bin ✅ Downloaded binaries and completed components set up. ℹ️ daprd binary has been installed to /Users/rick/.dapr/bin. ℹ️ dapr_placement container is running. ℹ️ dapr_redis container is running. ℹ️ dapr_zipkin container is running. ℹ️ Use `docker ps` to check running containers. ✅ Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started
完成初始化之後,可以在 docker 發現啟動了三個 container,如下:
1 2 3 4 5
~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bf0fc3456f59 daprio/dapr "./placement" 10 days ago Up 52 minutes 0.0.0.0:50005->50005/tcp dapr_placement a2837dfafc81 openzipkin/zipkin "start-zipkin" 10 days ago Up 52 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp dapr_zipkin 64a7fd503cc3 redis "docker-entrypoint.s…" 10 days ago Up 52 minutes 0.0.0.0:6379->6379/tcp dapr_redis
~$ dapr uninstall --all ℹ️ Removing Dapr from your machine... ℹ️ Removing directory: /Users/rick/.dapr/bin ℹ️ Removing container: dapr_placement ℹ️ Removing container: dapr_redis ℹ️ Removing container: dapr_zipkin ℹ️ Removing directory: /Users/rick/.dapr ✅ Dapr has been removed successfully
~$ dapr init --runtime-version=1.1.0 ⌛ Making the jump to hyperspace... ↓ Downloading binaries and setting up components... Dapr runtime installed to /Users/rick/.dapr/bin, you may run the following to add it to your path if you want to run daprd directly: export PATH=$PATH:/Users/rick/.dapr/bin ✅ Downloaded binaries and completed components set up. ℹ️ daprd binary has been installed to /Users/rick/.dapr/bin. ℹ️ dapr_placement container is running. ℹ️ dapr_redis container is running. ℹ️ dapr_zipkin container is running. ℹ️ Use `docker ps` to check running containers. ✅ Success! Dapr is up and running. To get started, go here: https://aka.ms/dapr-getting-started
## Bin 底下的東西大小不大 ~/.dapr ❯ ls -lah total 254304 drwxrwxrwx 5 rick staff 160B Feb 26 20:17 . drwxr-xr-x 5 rick staff 160B Feb 26 20:17 .. -rwxrwxrwx 1 rick staff 92M Feb 26 20:17 daprd -rwxrwxrwx 1 rick staff 32M Feb 26 20:17 dashboard drwxr-xr-x 3 rick staff 96B Feb 26 20:17 web
~$ dapr __ ____/ /___ _____ _____ / __ / __ '/ __ \/ ___/ / /_/ / /_/ / /_/ / / \__,_/\__,_/ .___/_/ /_/ =============================== Distributed Application Runtime Usage: dapr [command] Available Commands: completion Generates shell completion scripts components List all Dapr components. Supported platforms: Kubernetes configurations List all Dapr configurations. Supported platforms: Kubernetes dashboard Start Dapr dashboard. Supported platforms: Kubernetes and self-hosted help Help about any command init Install Dapr on supported hosting platforms. Supported platforms: Kubernetes and self-hosted invoke Invoke a method on a given Dapr application. Supported platforms: Self-hosted list List all Dapr instances. Supported platforms: Kubernetes and self-hosted logs Get Dapr sidecar logs for an application. Supported platforms: Kubernetes mtls Check if mTLS is enabled. Supported platforms: Kubernetes publish Publish a pub-sub event. Supported platforms: Self-hosted run Run Dapr and (optionally) your application side by side. Supported platforms: Self-hosted status Show the health status of Dapr services. Supported platforms: Kubernetes stop Stop Dapr instances and their associated apps. . Supported platforms: Self-hosted uninstall Uninstall Dapr runtime. Supported platforms: Kubernetes and self-hosted upgrade Upgrades a Dapr control plane installation in a cluster. Supported platforms: Kubernetes Flags: -h, --help help for dapr -v, --version version for dapr
跟很多開發工具都很像,這些後面應該都用得到。
Launch an Application
經過上述觀察,試跑一個 dapr application,啥扣都還沒寫,依照文件跑以下:
1 2 3 4 5 6 7 8 9 10
❯ dapr run --app-id myapp --dapr-http-port 3500 WARNING: no application command found. ℹ️ Starting Dapr with id myapp. HTTP Port: 3500. gRPC Port: 62638 ℹ️ Checking if Dapr sidecar is listening on HTTP port 3500
... 中間的 Log 後面整理 ...
ℹ️ Checking if Dapr sidecar is listening on GRPC port 62638 ℹ️ Dapr sidecar is up and running. ✅ You're up and running! Dapr logs will appear here.
❯ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bf0fc3456f59 daprio/dapr "./placement" 10 days ago Up 52 minutes 0.0.0.0:50005->50005/tcp dapr_placement a2837dfafc81 openzipkin/zipkin "start-zipkin" 10 days ago Up 52 minutes (healthy) 9410/tcp, 0.0.0.0:9411->9411/tcp dapr_zipkin 64a7fd503cc3 redis "docker-entrypoint.s…" 10 days ago Up 52 minutes 0.0.0.0:6379->6379/tcp dapr_redis
ℹ️ Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 63958 INFO[0000] starting Dapr Runtime -- version 1.0.0 -- commit 6314733 app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] log level set to: info app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] metrics server started on :63959/ app_id=nodeapp instance=iStar.local scope=dapr.metrics type=log ver=1.0.0 INFO[0000] standalone mode configured app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] app id: nodeapp app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0
... 略 ...
INFO[0000] internal gRPC server is running on port 63963 app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] application protocol: http. waiting on port 3000. This will block until the app is listening on that port. app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 == APP == Node App listening on port 3000!
INFO[0000] application discovered on port 3000 app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] application configuration loaded app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] actor runtime started. actor idle timeout: 1h0m0s. actor scan interval: 30s app_id=nodeapp instance=iStar.local scope=dapr.runtime.actor type=log ver=1.0.0 INFO[0000] dapr initialized. Status: Running. Init Elapsed 65.36200000000001ms app_id=nodeapp instance=iStar.local scope=dapr.runtime type=log ver=1.0.0 INFO[0000] placement tables updated, version: 0 app_id=nodeapp instance=iStar.local scope=dapr.runtime.actor.internal.placement type=log ver=1.0.0 ℹ️ Updating metadata for app command: node app.js ✅ You're up and running! Both Dapr and your app logs will appear here.
這次和上次不一樣,多了 == APP == Node App listening on port 3000! 這段訊息,其他差不多都一樣。
# Dapr CLI dapr invoke \ --app-id nodeapp \ --method order \ --verb GET
回傳結果:
1 2
{"orderId":"42"} ✅ App invoked successfully
PythonApp
官方的 Hello World 中,最後用 python 寫另一個 service,每秒自動建立新訂單的迴圈。
同樣的把這個 python 當作一個 Darp App 跑起來:
1 2 3 4 5 6
❯ dapr run --app-id pythonapp cmd /c "python3 app.py"
❯ dapr list APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID pythonapp 49312 49313 0 python3 app.py 5s 2021-03-09 23:36.26 3159 nodeapp 3500 63958 3000 node app.js 35m 2021-03-09 23:00.42 84911
Q and A
Q: 無法暫停 Dapr CLI 應用程式?
dapr list 檢查狀況
crtl + z 丟到 background
用 kill -9 <PID> 砍掉
1 2 3 4 5 6 7 8 9 10
# 正常啟動 ~$ dapr list APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID my-app 3500 50045 0 8s 2021-05-08 07:44.23 6942
# 啟動異常 ~$ dapr list APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID 0 0 0 28s 2021-05-08 07:39.41 6475