Blazor+Dapr+K8s微服务之服务调用
?
1.1??Dapr環(huán)境配置
1.1.1??????? 在開發(fā)機(jī)安裝Docker Desktop并啟用Kubernetes
安裝過程略,安裝好后效果如下:(左下角兩個綠色指示Docker和K8s正在運(yùn)行)
?
1.1.2??????? 在開發(fā)機(jī)安裝Dapr Cli
安裝命令:
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"將會安裝到如下目錄:
?
驗(yàn)證安裝命令:
dapr?
1.1.3??????? 為開發(fā)機(jī)配置Dapr
配置命令:
dapr init驗(yàn)證命令:
dapr --version命令:docker ps 可以看到多了Dapr自動加了三個容器:
?
1.1.4??????? 為K8s配置Dapr
配置命令:
dapr init -k驗(yàn)證命令:
kubectl get pods --namespace dapr-system?
可通過命令:?
kubectl port-forward deploy/dapr-dashboard --namespace dapr-system 8080:8080訪問Dapr Dashbord
?
1.2???????? 創(chuàng)建項(xiàng)目
1.2.1??????? 創(chuàng)建Blazor WebAssembly項(xiàng)目
注意選擇Asp.net Core hosted
?
1.2.2??????? 向解決方案添加Asp.net Core WebApi項(xiàng)目
添加完如圖,我增加了兩個解決方案文件夾BlazorWeb和ServiceAPI以方便區(qū)分前端和后端。
?
啟動DaprTest1.Server Project運(yùn)行:啟動端口配置為8000
?
1.3???????? 直接調(diào)用微服務(wù)
當(dāng)前解決方案中,DaprTest1.Client Blazor 項(xiàng)目的Fetch data 功能會調(diào)用DaprTest1.Server項(xiàng)目的WeatherForecastController.cs ?中的Get接口以獲取天氣預(yù)報信息。
我們需要修改為:在DaprTest1.Server項(xiàng)目的的Get接口不再直接返回天氣預(yù)報信息,而是調(diào)用DaprTest1.ServiceApi1項(xiàng)目的WeatherForecastController中的Get 接口獲取天氣預(yù)報。
1.3.1??????? ?引用Refit包
在項(xiàng)目DaprTest1.Server中安裝Refit 包。
?
1.3.2??????? 修改相關(guān)代碼
在項(xiàng)目DaprTest1.ServiceApi1中引用DaprTest1.Shared項(xiàng)目,并刪除原來的Wheatherforcast.cs實(shí)體類。
?
在項(xiàng)目DaprTest1.Server 中新增文件ICallServiceApi1.cs,并添加如下代碼:
public interface ICallServiceApi1{[Get("/WeatherForecast")]Task<IEnumerable<WeatherForecast>> GetWeatherForecast();}?
在項(xiàng)目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);});?
修改項(xiàng)目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項(xiàng)目,訪問http://localhost:8000/ 可看到效果, 效果圖略。
1.4???????? 通過Dapr調(diào)用微服務(wù)
我們需要修改為:在DaprTest1.Server中不直接調(diào)用DaprTest1.ServiceApi1的接口地址獲取天氣預(yù)報信息,而是通過Dapr調(diào)用DaprTest1.ServiceApi1 的服務(wù)ID來獲取天氣預(yù)報信息.
1.4.1??????? 引用Dapr.Client包
在項(xiàng)目DaprTest1.Server中安裝Dapr.Client包
?
1.4.2??????? 修改相關(guān)代碼
在項(xiàng)目DaprTest1.Server 的Startup.cs 中修改如下代碼: 本代碼參考了 張善友大神的
Dapr 客戶端 搭配 WebApiClientCore 玩耍服務(wù)調(diào)用
services.AddScoped<InvocationHandler>();// 注入httpClientservices.AddHttpClient("HttpClient").AddHttpMessageHandler<InvocationHandler>().AddTypedClient(client =>{client.BaseAddress = new Uri("http://serviceapi1");return RestService.For<ICallServiceApi1>(client);});1.4.3??????? 在Dapr中運(yùn)行項(xiàng)目
分別在DaprTest1.Server和DaprTest1.ServiceApi1項(xiàng)目中添加文件dapr-selfhosted.ps1
文件內(nèi)容分別為:
dapr run `--app-id blazorweb `--app-port 8000 `--dapr-http-port 3600 `--dapr-grpc-port 60000 `dotnet rundapr run `--app-id serviceapi1 `--app-port 8001 `--dapr-http-port 3601 `--dapr-grpc-port 60001 `dotnet run?
PowerShell分別啟動DaprTest1.Server 和 DaprTest1.ServiceApi1項(xiàng)目下面的dapr-selfhosted.ps1文件, 訪問?http://localhost:8000/?即可看到效果
?
?
1.5???????? 在K8s中運(yùn)行項(xiàng)目
1.5.1??????? 添加Docker文件生成鏡像
分別在DaprTest1.Server和DaprTest1.ServiceApi1項(xiàng)目中添加Docker文件dockerfile, 并修改文件內(nèi)容如下:
ARG NET_IMAGE=5.0-buster-slimFROM mcr.microsoft.com/dotnet/aspnet:${NET_IMAGE} AS baseWORKDIR /appEXPOSE 5000FROM mcr.microsoft.com/dotnet/sdk:${NET_IMAGE} AS buildWORKDIR /srcCOPY ["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/buildFROM build AS publishRUN dotnet publish "DaprTest1.Server.csproj" -c Release -o /app/publishFROM base AS finalWORKDIR /appCOPY --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 5000FROM 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/buildFROM build AS publish RUN dotnet publish "DaprTest1.ServiceApi1.csproj" -c Release -o /app/publishFROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DaprTest1.ServiceApi1.dll"]?
在解決方案根目錄增加文件build-docker-images.ps1 以生成Docker image,文件內(nèi)容如下:
docker build -t dapr-test1/blazorweb:1.0 -f Server/Dockerfile .
docker build -t dapr-test1/serviceapi1:1.0 -f ServiceApi/DaprTest1.ServiceApi1/Dockerfile .
?
在解決方案根目錄執(zhí)行build-docker-images.ps1后, 可看到生成的兩個鏡像:
?
1.5.2??????? 部署Docker鏡像到K8s
在解決方案根目錄新建文件夾 Deploy, 放置k8s部署文件
?
其中deploy.ps1 為部署命令,內(nèi)容如下:
kubectl apply `-f namespace.yaml `-f dapr-config.yaml `-f blazorweb.yaml `-f serviceapi1.yaml?
部署完成后,可通過端口轉(zhuǎn)發(fā)給node看到效果.
?
?
代碼地址:iamxiaozhuang/dapr-test (github.com)
總結(jié)
以上是生活随笔為你收集整理的Blazor+Dapr+K8s微服务之服务调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET 6 新特性 WaitAsync
- 下一篇: Istio 首次安全评估结果公布