Dapr + .NET Core实战(十一)单机Dapr集群

如何单机部署Dapr集群

第十篇讲过了K8S集群下如何使用Dapr运行程序,但是很多人一直在问如何单机下进行Dapr的负载,这节课我们来聊聊如何单机进行Dapr的负载。

首先要说的是单机下,通过 dapr run --app-id xxxxx这个命令是没办法直接运行dapr集群的,因为同一个app-id只能通过dapr run运行一次。这时候我们就需要利用docker来实现dapr的单机集群部署。下面我们就来实现dapr的单机集群部署。

说在中间,欢迎大家参加4小时Dapr+.NET 5的实战课程

课程链接     https://ke.qq.com/course/4000292?tuin=1271860f

Dapr + .NET Core实战(十一)单机Dapr集群

项目拓扑

Dapr + .NET Core实战(十一)单机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支持

Dapr + .NET Core实战(十一)单机Dapr集群

 

 修改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不一致,已成功实现负载均衡

Dapr + .NET Core实战(十一)单机Dapr集群

 

 Dapr + .NET Core实战(十一)单机Dapr集群

 

上一篇:通过源码告诉你阿里的数据库连接池Druid有多牛


下一篇:部署.war包到tomcat,启动tomcat报错