生活随笔
收集整理的這篇文章主要介紹了
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 80FROM 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/buildFROM build AS publish
RUN dotnet publish "OrderSystem.csproj" -c Release -o /app/publishFROM 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 80FROM 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/buildFROM build AS publish
RUN dotnet publish "PaymentSystem.csproj" -c Release -o /app/publishFROM 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-}ordersystembuild:context: ../dockerfile: /OrderSystem/Dockerfileports:- "3500:3500"volumes: - ../OrderSystem:/OrderSystem ordersystem-dapr:image: "daprio/daprd:latest"command: [ "./daprd", "-app-id", "order", "-app-port", "80" ]depends_on:- ordersystemnetwork_mode: "service:ordersystem"paymentsystem1:image: ${DOCKER_REGISTRY-}paymentsystembuild:context: ../dockerfile: /PaymentSystem/Dockerfileports:- "3601:3500"volumes: - ../PaymentSystem:/PaymentSystem paymentsystem1-dapr:image: "daprio/daprd:latest"command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ]depends_on:- paymentsystem1network_mode: "service:paymentsystem1"paymentsystem2:image: ${DOCKER_REGISTRY-}paymentsystembuild:context: ../dockerfile: /PaymentSystem/Dockerfilevolumes: - ../PaymentSystem:/PaymentSystem ports:- "3602:3500"paymentsystem2-dapr:image: "daprio/daprd:latest"command: [ "./daprd", "-app-id", "pay", "-app-port", "80" ]depends_on:- paymentsystem2network_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部署服务的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。