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

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dapr牵手.NET学习笔记:用docker-compose部署服务相关的知识,希望对你有一定的参考价值。

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

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

首先要把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,目录结构如下:

其中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,如下:

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

paymentsystem1

paymentsystem1

以上是关于Dapr牵手.NET学习笔记:用docker-compose部署服务的主要内容,如果未能解决你的问题,请参考以下文章

Dapr牵手.NET学习笔记:绑定

Dapr牵手.NET学习笔记:开篇

Dapr牵手.NET学习笔记:跨物理机负载均衡服务调用

Dapr牵手.NET学习笔记:状态管理进阶

Dapr牵手.NET学习笔记:状态管理进阶

Dapr牵手.NET学习笔记:想入非非的服务调用