一、首先去https://www.docker.com/products/docker-desktop下載Windows版本的Docker Desktop并安裝(需要win10專業版以上作業系統,并啟用CPU虛擬化和安裝Hvper-V),
添加國內鏡像源:
{ "registry-mirrors": [ "https://reg-mirror.qiniu.com", "http://hub-mirror.c.163.com", "https://registry.docker-cn.com" ], "insecure-registries": [], "debug": true, "experimental": false }
有時候第一次安裝完用VS除錯的時候會下載一些資料包,會比較慢,類似于提示:
1>Info: Using vsdbg version '16.6.20415.1' 1>Info: Using Runtime ID 'linux-x64' 1>Info: C:\Users\UserName\vsdbg\vs2017u5 exists, deleting.
老半天不動,這時候去:https://download.csdn.net/download/5653325/12825714
下載離線包,解壓縮vs2017u5目錄中的內容到C:\Users\UserName\vsdbg\vs2017u5檔案夾中根目錄中,然后重新啟動VS
二、新建一個.NetCore3.1的API專案,在創建的時候選中啟用Docker支持,

三、寫一個簡單的回應輸出
public IActionResult Hello()
{
return Content(JsonConvert.SerializeObject(new ContentModal{
Status = 1,
Code = 200,
Message = $"請求成功=>{Request.HttpContext.Connection.LocalIpAddress}:{Request.HttpContext.Connection.LocalPort}",
Content = "ok",
}), "application/json", Encoding.UTF8);
}
四、專案中的Dockerfile檔案按以下內容完善(多余的可以刪掉)
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 8001 EXPOSE 443 COPY . . ENV ASPNETCORE_URLS http://+:8001 ENTRYPOINT ["dotnet", "weixinKey.dll"]
并且右鍵此DockerFile屬性,如果較新則復制,
使用Release模式生成專案,管理員運行cmd,定位到當前專案的Release目錄下的netcoreapp3.1目錄(我用的是.net core3.1版本,如果是其它版本,這個目錄名會不同)
然后運行docker build命令打包成docker包

第一次打包的時候會下載所依賴的環境,
五、打包完成后運行Power Shell(管理員模式),然后運行 docker images即可看到docker包已經推送到Docker Desktop中了

六、依次啟動多個此docker的實體,本次測驗啟用了四個實體,

七、查看Docker Desktop中的實體情況

八、下載Nginx的最新版本(http://nginx.org/download/nginx-1.18.0.zip),解壓后編輯conf目錄下的nginx.conf,添加以下內容啟用負載均衡,
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream wxapi{
server 10.2.18.244:8887 weight=1;
server 10.2.18.244:8888 weight=1;
server 10.2.18.244:8889 weight=1;
server 10.2.18.244:8890 weight=1;
}
# down 此標記的服務器不參與負載.
# weight 值越大,權重就越大,越能多次回應請求
# backup 其它服務器無法回應是會請求此種型別的服務器應急,
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://wxapi;
#proxy_redirect default;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
九、啟動Nginx(雙擊nginx.exe或者CMD下運行)
十、訪問測驗

Docker中會對每個實體虛擬一個IP地址,至此,我們的API成功運行在Docker中并通過Nginx實作了負載均衡,
十一、Docker中匯出、匯入鏡像檔案命令
匯出:docker image save centos > docker-centos.tar.gz
匯入:docker image load -i docker-centos.tar.gz
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/38399.html
標籤:.NET Core
上一篇:Web API 約定
