上一篇聊到用两个物理机(一个win,一个mac)来部署dapr和服务 ,实现order调用pay的负载均衡。本篇说一下在windows上的docker部署这三个服务,达到与上一篇的效果。

  三个服务的部署架构是这样的

Dapr牵手.NET学习笔记:用docker-compose部署服务_服务端

 首先要把OrderSystem(服务端口80)项目docker化,Dockerfile内容为:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

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

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

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

其实要把PaymentSystem(服务端口80)项目docker化,Dockerfile内容为:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

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

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

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

同时再添加一个docker-compose项目(右键 OrderSystem或PaymentSystem,Add,选择Container Orchestrator Support即可),命名为B2C,目录结构如下:

Dapr牵手.NET学习笔记:用docker-compose部署服务_docker_02

 其中docker-compose.yml内容如下

version: '3.4'

services:
ordersystem:
image: ${DOCKER_REGISTRY-}ordersystem
build:
context: ../
dockerfile: /OrderSystem/Dockerfile
ports:
- "3500:3500"
volumes:
- ../OrderSystem:/OrderSystem
ordersystem-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "order", "-app-port", "80" ]
depends_on:
- ordersystem
network_mode: "service:ordersystem"

paymentsystem1:
image: ${DOCKER_REGISTRY-}paymentsystem
build:
context: ../
dockerfile: /PaymentSystem/Dockerfile
ports:
- "3601:3500"
volumes:
- ../PaymentSystem:/PaymentSystem
paymentsystem1-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ]
depends_on:
- paymentsystem1
network_mode: "service:paymentsystem1"

paymentsystem2:
image: ${DOCKER_REGISTRY-}paymentsystem
build:
context: ../
dockerfile: /PaymentSystem/Dockerfile
volumes:
- ../PaymentSystem:/PaymentSystem
ports:
- "3602:3500"
paymentsystem2-dapr:
image: "daprio/daprd:latest"
command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ]
depends_on:
- paymentsystem2
network_mode: "service:paymentsystem2"

  为了使PaymentSystem部署多个副本,我在docker-compose.yml中配置了paymentsystem1和paymentsystem2,它们使用的都是PaymentSystem项目的信息。关于docker-compose就不多说了。

进入B2C目录,启动三个服务,命令如下:

docker-compose up -d

就会在docker中启动三个服务,一个order,两个pay,如下:

Dapr牵手.NET学习笔记:用docker-compose部署服务_microsoft_03


看一下结果吧,同样是postman,输入localhost:3500/v1.0/invoke/order/method/order,就会看到pay会轮询调用。

paymentsystem1

Dapr牵手.NET学习笔记:用docker-compose部署服务_docker_04

 paymentsystem2

Dapr牵手.NET学习笔记:用docker-compose部署服务_服务端_05

  想要更快更方便的了解相关知识,可以关注微信公众号 

Dapr牵手.NET学习笔记:用docker-compose部署服务_microsoft_06