Dapr + .NET Core實戰(十一)單機Dapr叢集負載均衡

chester·chen發表於2021-10-10

如何單機部署Dapr叢集

第十篇講過了K8S叢集下如何使用Dapr執行程式,但是很多人一直在問如何單機下進行Dapr的負載,這節課我們來聊聊如何單機進行Dapr的負載。

首先要說的是單機下,通過 dapr run --app-id xxxxx這個命令是沒辦法直接執行dapr叢集的,因為同一個app-id只能通過dapr run執行一次。這時候我們就需要利用docker來實現dapr的單機叢集部署。下面我們就來實現dapr的單機叢集部署。

專案拓撲

 

 我們採用在docker內部為frontend部署一個例項,為backend部署兩個例項的方法,來實現backend的叢集化負載,另外需要為frontend和2個backend分別啟動一個sidecar容器。因此此例項一共需要部署6個容器。

專案實戰

首先在BackEnd中新增控制器IpController,並新增獲取backend例項ip的介面

   [Route("api/[controller]")]
    [ApiController]
    public class IpController : ControllerBase
    {
        public ActionResult Get()
        {
            var ip = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Select(p => p.GetIPProperties()).SelectMany(p => p.UnicastAddresses)
                      .Where(p => p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address))
                      .FirstOrDefault()?.Address.ToString();

            return Ok(new List<string> { ip });
        }

    }

 

然後需要在FrontEnd的DaprController中新增呼叫獲取backend ip的介面

        [HttpGet("ip")]
        public async Task<ActionResult> GetIpAsync()
        {
            using var _daprClient = new DaprClientBuilder().Build();
            var result = await _daprClient.InvokeMethodAsync<List<string>>(HttpMethod.Get, "backend", "api/Ip");
            return Ok(result);
        }

 

這樣我們就可以通過訪問frontend的Dapr/ip來驗證backend是不是可以實現負載均衡。

Docker執行

在前面的課程裡我們已經為backend和frontend建好了Dockerfile,這裡不再談Dockerfile的編寫

backend

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5000

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["BackEnd/BackEnd.csproj", "BackEnd/"]
RUN dotnet restore "BackEnd/BackEnd.csproj"
COPY . .
WORKDIR "/src/BackEnd"
RUN dotnet build "BackEnd.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "BackEnd.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BackEnd.dll"]

frontend

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5001

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["FrontEnd/FrontEnd.csproj", "FrontEnd/"]
RUN dotnet restore "FrontEnd/FrontEnd.csproj"
COPY . .
WORKDIR "/src/FrontEnd"
RUN dotnet build "FrontEnd.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "FrontEnd.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FrontEnd.dll"]

右鍵backend新增容器適配支援,新增docker-compose支援

 

 修改docker-compose.yaml的內容

version: '3.4'
services:
  backend:
    image: ${DOCKER_REGISTRY-}backend
    build:
      context: .
      dockerfile: BackEnd/Dockerfile
    ports:
      - "51000:50001"

  daprbackend-dapr:
    image: "daprio/daprd:latest"
    command: [ "./daprd", "-app-id", "backend", "-app-port", "5000" ]
    depends_on:
      - backend
    network_mode: "service:backend"

  backend2:
    image: ${DOCKER_REGISTRY-}backend2
    build:
      context: .
      dockerfile: BackEnd/Dockerfile
    ports:
      - "53000:50001"

  daprbackend-dapr2:
    image: "daprio/daprd:latest"
    command: [ "./daprd", "-app-id", "backend", "-app-port", "5000" ]
    depends_on:
      - backend2
    network_mode: "service:backend2"

  frontend:
    image: ${DOCKER_REGISTRY-}frontend
    build:
      context: .
      dockerfile: FrontEnd/Dockerfile
    ports:
      - "54000:50001"
      - "5001:5001"

  daprfrontend-dapr:
    image: "daprio/daprd:latest"
    command: [ "./daprd", "-app-id", "frontend", "-app-port", "5001" ]
    depends_on:
      - frontend
    network_mode: "service:frontend"

其中我們公開了frontend和backend的50001預設GRPC通訊埠,給frontend暴露了一個5001埠,另外給backend和frontend分別啟動了一個daprd容器,通過depends_onnetwork_mode實現繫結。

執行並驗證

我們通過docker-compose up -d 來執行

docker-compose up -d

看到以下內容則啟動成功

Creating dapr-aspnetcore-demo_backend2_1         ... done
Creating dapr-aspnetcore-demo_backend_1  ... done
Creating dapr-aspnetcore-demo_frontend_1          ... done
Creating dapr-aspnetcore-demo_daprbackend-dapr_1  ... done
Creating dapr-aspnetcore-demo_daprbackend-dapr2_1 ... done
Creating dapr-aspnetcore-demo_daprfrontend-dapr_1 ... done

通過postman呼叫frontend的dapr/ip,發現返回的backend的ip不一致,已成功實現負載均衡

 

 

 

相關文章