1.1 Dapr環境配置
1.1.1 在開發機安裝Docker Desktop並啟用Kubernetes
安裝過程略,安裝好後效果如下:(左下角兩個綠色指示Docker和K8s正在執行)
1.1.2 在開發機安裝Dapr Cli
安裝命令:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
將會安裝到如下目錄:
驗證安裝命令:
dapr
1.1.3 為開發機配置Dapr
配置命令:
dapr init
驗證命令:
dapr --version
命令:docker ps 可以看到多了Dapr自動加了三個容器:
1.1.4 為K8s配置Dapr
配置命令:
dapr init -k
驗證命令:
kubectl get pods --namespace dapr-system
可通過命令:
kubectl port-forward deploy/dapr-dashboard --namespace dapr-system 8080:8080
訪問Dapr Dashbord
1.2 建立專案
1.2.1 建立Blazor WebAssembly專案
注意選擇Asp.net Core hosted
1.2.2 向解決方案新增Asp.net Core WebApi專案
新增完如圖,我增加了兩個解決方案資料夾BlazorWeb和ServiceAPI以方便區分前端和後端。
啟動DaprTest1.Server Project執行:啟動埠配置為8000
1.3 直接呼叫微服務
當前解決方案中,DaprTest1.Client Blazor 專案的Fetch data 功能會呼叫DaprTest1.Server專案的WeatherForecastController.cs 中的Get介面以獲取天氣預報資訊。
我們需要修改為:在DaprTest1.Server專案的的Get介面不再直接返回天氣預報資訊,而是呼叫DaprTest1.ServiceApi1專案的WeatherForecastController中的Get 介面獲取天氣預報。
1.3.1 引用Refit包
在專案DaprTest1.Server中安裝Refit 包。
1.3.2 修改相關程式碼
在專案DaprTest1.ServiceApi1中引用DaprTest1.Shared專案,並刪除原來的Wheatherforcast.cs實體類。
在專案DaprTest1.Server 中新增檔案ICallServiceApi1.cs,並新增如下程式碼:
public interface ICallServiceApi1 {
[Get("/WeatherForecast")] Task<IEnumerable<WeatherForecast>> GetWeatherForecast(); }
在專案DaprTest1.Server 的Startup.cs 中新增如下程式碼,其中http://localhost:8001 是ServiceApi1介面訪問地址
// 注入httpClient services.AddHttpClient("HttpClient") .AddTypedClient(client => { client.BaseAddress = new Uri("http://localhost:8001"); return RestService.For<ICallServiceApi1>(client); });
修改專案DaprTest1.Server WeatherForecastController.cs 中的Get介面如下:
public class WeatherForecastController : ControllerBase { private readonly ILogger<WeatherForecastController> _logger; private readonly ICallServiceApi1 _callServiceApi1; public WeatherForecastController(ILogger<WeatherForecastController> logger, ICallServiceApi1 callServiceApi1) { _logger = logger; _callServiceApi1 = callServiceApi1; } [HttpGet] public async Task<IEnumerable<WeatherForecast>> Get() { return await _callServiceApi1.GetWeatherForecast(); } }
分別啟動DaprTest1.Server 和 DaprTest1.ServiceApi1專案,訪問http://localhost:8000/ 可看到效果, 效果圖略。
1.4 通過Dapr呼叫微服務
我們需要修改為:在DaprTest1.Server中不直接呼叫DaprTest1.ServiceApi1的介面地址獲取天氣預報資訊,而是通過Dapr呼叫DaprTest1.ServiceApi1 的服務ID來獲取天氣預報資訊.
1.4.1 引用Dapr.Client包
在專案DaprTest1.Server中安裝Dapr.Client包
1.4.2 修改相關程式碼
在專案DaprTest1.Server 的Startup.cs 中修改如下程式碼: 本程式碼參考了 張善友大神的
Dapr 客戶端 搭配 WebApiClientCore 玩耍服務呼叫
services.AddScoped<InvocationHandler>(); // 注入httpClient services.AddHttpClient("HttpClient").AddHttpMessageHandler<InvocationHandler>() .AddTypedClient(client => { client.BaseAddress = new Uri("http://serviceapi1"); return RestService.For<ICallServiceApi1>(client); });
1.4.3 在Dapr中執行專案
分別在DaprTest1.Server和DaprTest1.ServiceApi1專案中新增檔案dapr-selfhosted.ps1
檔案內容分別為:
dapr run ` --app-id blazorweb ` --app-port 8000 ` --dapr-http-port 3600 ` --dapr-grpc-port 60000 ` dotnet run
dapr run ` --app-id serviceapi1 ` --app-port 8001 ` --dapr-http-port 3601 ` --dapr-grpc-port 60001 ` dotnet run
PowerShell分別啟動DaprTest1.Server 和 DaprTest1.ServiceApi1專案下面的dapr-selfhosted.ps1檔案, 訪問 http://localhost:8000/ 即可看到效果
1.5 在K8s中執行專案
1.5.1 新增Docker檔案生成映象
分別在DaprTest1.Server和DaprTest1.ServiceApi1專案中新增Docker檔案dockerfile, 並修改檔案內容如下:
ARG NET_IMAGE=5.0-buster-slim FROM mcr.microsoft.com/dotnet/aspnet:${NET_IMAGE} AS base WORKDIR /app EXPOSE 5000 FROM mcr.microsoft.com/dotnet/sdk:${NET_IMAGE} AS build WORKDIR /src COPY ["Server/DaprTest1.Server.csproj", "Server/"] COPY ["Shared/DaprTest1.Shared.csproj", "Shared/"] COPY ["Client/DaprTest1.Client.csproj", "Client/"] RUN dotnet restore "Server/DaprTest1.Server.csproj" COPY . . WORKDIR "/src/Server" RUN dotnet build "DaprTest1.Server.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "DaprTest1.Server.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DaprTest1.Server.dll"]
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. ARG NET_IMAGE=5.0-buster-slim FROM mcr.microsoft.com/dotnet/aspnet:${NET_IMAGE} AS base WORKDIR /app EXPOSE 5000 FROM mcr.microsoft.com/dotnet/sdk:${NET_IMAGE} AS build WORKDIR /src COPY ["ServiceApi/DaprTest1.ServiceApi1/DaprTest1.ServiceApi1.csproj", "ServiceApi/DaprTest1.ServiceApi1/"] COPY ["Shared/DaprTest1.Shared.csproj", "Shared/"] RUN dotnet restore "ServiceApi/DaprTest1.ServiceApi1/DaprTest1.ServiceApi1.csproj" COPY . . WORKDIR "/src/ServiceApi/DaprTest1.ServiceApi1" RUN dotnet build "DaprTest1.ServiceApi1.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "DaprTest1.ServiceApi1.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DaprTest1.ServiceApi1.dll"]
在解決方案根目錄增加檔案build-docker-images.ps1 以生成Docker image,檔案內容如下:
docker build -t dapr-test1/blazorweb:1.0 -f Server/Dockerfile .
docker build -t dapr-test1/serviceapi1:1.0 -f ServiceApi/DaprTest1.ServiceApi1/Dockerfile .
在解決方案根目錄執行build-docker-images.ps1後, 可看到生成的兩個映象:
1.5.2 部署Docker映象到K8s
在解決方案根目錄新建資料夾 Deploy, 放置k8s部署檔案
其中deploy.ps1 為部署命令,內容如下:
kubectl apply ` -f namespace.yaml ` -f dapr-config.yaml ` -f blazorweb.yaml ` -f serviceapi1.yaml
部署完成後,可通過埠轉發給node看到效果.