微服務Dapr:window環境下的快速入門


一、安裝Dapr CLI腳手架

開始下載並安裝 Dapr CLI,使用powershell,輸入以下的語句安裝:

powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"

但是一般會報錯如下,意思就是被牆了,要FQ才行:

iwr : 未能解析此遠程名稱: 'raw.githubusercontent.com'
所在位置 行:1 字符: 1 + iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/i ...    
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest],WebExce
ption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

FQ后再次執行,出現以下提示:

PS D:\> powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"

PowerShell requires an execution policy of 'RemoteSigned'.
To make this change please run:
'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'

按照提示我們需要執行策略更改,如下,輸入Y確認即可:

PS D:\> Set-ExecutionPolicy RemoteSigned -scope CurrentUser

執行策略更改
執行策略可幫助你防止執行不信任的腳本。更改執行策略可能會產生安全風險,如 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies 幫助主題所述。是否要更改執行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暫停(S)  [?] 幫助 (默認值為“N”): Y

再次執行安裝就安裝成功了,默認安裝在C:\dapr目錄下,我們將此目錄添加至用戶PATH環境變量中。

 

 此處看到已經默認添加了,我們就不用添加了,打開新的powershell后輸入dapr命令驗證,成功顯示如下

 二、初始化Dapr

 用管理員權限運行powershell,輸入dapr --version命令查詢dapr版本如下:

PS C:\> dapr --version
CLI version: 1.2.0
Runtime version: n/a

 可以看到Runtime version沒有顯示,說明dapr運行時沒有安裝,我們輸入dapr init命令,顯示如下提示:

PS C:\> dapr init
Making the jump to hyperspace...
could not connect to Docker. Docker may not be installed or running

說明鏈接不到docker,我們運行docker后再次輸入dapr init命令,但是報了403權限問題無法安裝,多試幾次就成功了(這里打個問號?),也可以查到版本號了:

PS C:\> dapr init
Making the jump to hyperspace...
Downloading binaries and setting up components...
cannot get the latest release version: https://api.github.com/repos/dapr/dapr/releases - 403 Forbidden
PS C:\> dapr init
Making the jump to hyperspace...
Downloading binaries and setting up components...
Downloaded binaries and completed components set up.
daprd binary has been installed to C:\Users\Xu\.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
PS C:\> dapr --version
CLI version: 1.2.0
Runtime version: 1.2.2

 之后通過docker ps命令查看安裝的容器,需要確保鏡像為daprio/dapr、openzipkin/zipkin和redis的容器都運行

PS C:\> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                   PORTS                              NAMES
26af89c8762c        daprio/dapr         "./placement"            5 minutes ago       Up 5 minutes             0.0.0.0:6050->50005/tcp            dapr_placement
df646eb3b33a        openzipkin/zipkin   "start-zipkin"           5 minutes ago       Up 5 minutes (healthy)   9410/tcp, 0.0.0.0:9411->9411/tcp   dapr_zipkin
4a28c3982d6b        redis               "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes             0.0.0.0:6379->6379/tcp             dapr_redis

 在dapr inti時,CLI 還創建了一個默認組件文件夾,其中包括幾個 YAML 文件,其中包含state store、elevated 和 zipkin。 Dapr sidecar, 將讀取這些文件。 告訴它使用Redis容器進行狀態管理和消息傳遞,以及Zipkin容器來收集跟蹤。

 可以通過運行cmd,在文件管理器中打開如下:

explorer "%USERPROFILE%\.dapr\"

將會看到Dapr 配置、 Dapr 二進制目錄和 Dapr 的默認組件目錄

 三、使用Dapr的內置api

運行以下命令以啟動 Dapr sidecar,它將在端口 3500 上監聽名為 myapp 的空白應用程序

dapr run --app-id myapp --dapr-http-port 3500

因為使用此命令,沒有定義自定義組件文件夾。因此Dapr 使用在 init 流中創建的默認組件定義(在 Windows中啟用cmd用語句explorer "%USERPROFILE%\.dapr\"打開),告訴 Dapr 使用本地的 Redis Docker 容器作為狀態存儲和消息代理。

接下來我們保存一個鍵值對到redis docker中,如下

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '[{ "key": "name", "value": "Bruce Wayne"}]' -Uri 'http://localhost:3500/v1.0/state/statestore'

然后我們可以獲取到我們保存的鍵值

Invoke-RestMethod -Uri 'http://localhost:3500/v1.0/state/statestore/name'

我們可以在redis中看到我們保存的鍵值對

四、定義一個組件

像是上述的執行的dapr內置api,是啟用了我們的默認組件,這里我們自己構造組件進行運行,創建一個名為my-components的文件夾,在此目錄內創建一個新文件localSecretStore.yaml和一個名為mysecrets.json的密鑰文件,內容分別如下:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: my-secret-store
  namespace: default
spec:
  type: secretstores.local.file
  version: v1
  metadata:
  - name: secretsFile
    value: my-components/mysecrets.json
  - name: nestedSeparator
    value: ":"
{
   "my-secret" : "I'm Batman"
}

您可以看到上述的文件定義有一個type: secretstores.local.file字段值,其告訴Dapr使用本地文件組件作為密鑰存儲。 元數據字段提供了使用該組件所需的組件特定信息(在本例中,是密鑰存儲JSON的路徑)。

我們將當前目錄跳轉到my-components文件夾的上一個目錄,運行dapr sidecar,並以我們定義的組件為啟動項:

dapr run --app-id myapp --dapr-http-port 3500 --components-path ./my-components

之后我們獲取密鑰可以看到如下情況獲取成功:

PS D:\> Invoke-RestMethod -Uri 'http://localhost:3500/v1.0/secrets/my-secret-store/my-secret'

my-secret
---------
I'm Batman

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM