Docker ASP.NET Core 2.0 微服务跨平台实践
本篇博文的目的:在 Mac OS 中使用 VS Code 開發(fā) ASP.NET Core 2.0 應(yīng)用程序,然后在 Ubuntu 服務(wù)器配置 Docker 環(huán)境,并使用 Docker 運(yùn)行 Consul 和 Fabio 環(huán)境,最后使用 Docker 運(yùn)行 ASP.NET Core 2.0 應(yīng)用程序。
你要的項(xiàng)目源碼:https://github.com/yuezhongxin/HelloDocker.Sample
上面配置看起來還蠻簡單,但實(shí)際去操作的時(shí)候,還是遇到了蠻多的問題,并且花了很多的時(shí)間去解決,比如 Docker 運(yùn)行 Consul 和 Fabio,下面詳細(xì)說下過程。
1. Docker 運(yùn)行 Consul 環(huán)境
關(guān)于?Consul?的概念:
Consul 是 HashiCorp 公司推出的開源工具,用于實(shí)現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置。與其他分布式服務(wù)注冊與發(fā)現(xiàn)的方案,比如 Airbnb 的 SmartStack 等相比,Consul 的方案更“一站式”,內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框 架、分布一致性協(xié)議實(shí)現(xiàn)、健康檢查、Key/Value 存儲、多數(shù)據(jù)中心方案,不再需要依賴其他工具(比如 ZooKeeper 等)。使用起來也較 為簡單。Consul 用 Golang 實(shí)現(xiàn),因此具有天然可移植性(支持 Linux、windows 和 Mac OS X);安裝包僅包含一個(gè)可執(zhí)行文件,方便部署,與 Docker 等輕量級容器可無縫配合。
Consul Docker 鏡像地址:https://hub.docker.com/_/consul/
配置 Consul 的微服務(wù)集群環(huán)境,需要先配置下 Server 服務(wù)端(需要獨(dú)立服務(wù)器環(huán)境),配置命令(沒有使用 Docker):
$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -ui-dir=./dist ?-config-dir /etc/consul.d -bind=10.9.10.110一開始,我想在 Mac OS 環(huán)境中使用 Docker 配置 Consul Client 客戶端,但遇到了一些問題,配置命令:
$ docker run -d --net=host --name=consul-client consul agent -bind=10.9.10.190 -client=0.0.0.0 -node=mac-xishuai -retry-join=10.9.10.236先解析下命令的意思:
-
--net=host:host網(wǎng)絡(luò)模式,容器的網(wǎng)絡(luò)接口和主機(jī)一樣,也就是共享一個(gè) IP 地址,如果沒有此命令,默認(rèn)是bridge網(wǎng)絡(luò)模式,也就是我們常用的橋接模式,Docker 會分配給容器一個(gè)獨(dú)立的 IP 地址(端口也是獨(dú)立的),并且容器和主機(jī)之間可以相互訪問。
-
-bind=:Consul Client 綁定的 IP 地址,一般是內(nèi)網(wǎng)的私有 IP 地址,需要內(nèi)網(wǎng)服務(wù)器之前可以相互訪問到,注意并不是127.0.0.1。
-
-retry-join=:加入 Consul 集群中,地址是 Consul Server 的 IP 地址,也可以是-join=,加上retry會不斷進(jìn)行重試。
一臺服務(wù)器一般會配置一個(gè) Consul Client,所以我們可以直接讓 Consul 容器和主機(jī)的 IP 地址一樣(我使用的),但使用了 Docker 之后,一臺服務(wù)器就可以配置多個(gè) Consul Client,我們就可以使用bridge網(wǎng)絡(luò)模式,一臺服務(wù)器可以完成配置整個(gè) Consul 集群環(huán)境。
這里需要再重點(diǎn)說下-client=,一開始我沒有理解,先看下官方說明:
If you want to expose the Consul interfaces to other containers via a different network, such as the bridge network, use the-clientoption for Consul.
With this configuration, Consul's client interfaces will be bound to the bridge IP and available to other containers on that network, but not on the host network. Note that we still keep the cluster address out on the host network for performance. Consul will also accept the-client=0.0.0.0option to bind to all interfaces.
啥意思呢?Consul 服務(wù)注冊的時(shí)候,一般是通過?HTTP API?接口進(jìn)行注冊,比如:http://10.9.10.190:8500/v1/agent/service/register,就是往 Consul 集群中注冊服務(wù),需要注意的是,10.9.10.190一般是 Consul Client 的 IP 地址(也可以是 Consul Server),-client配置的就是此地址,簡單來說,就是用來服務(wù)注冊并能訪問到的地址,換句話說,服務(wù)注冊可以跨服務(wù)器(服務(wù)和 Consul Client 并不需要在同一臺服務(wù)器上),0.0.0.0表示任何本機(jī)的相關(guān) IP 地址都可以訪問,推薦此配置。
這里需要再說明下,Docker 部署 ASP.NET Core 2.0、Consul 和 Fabio 有兩種方式:
-
使用一個(gè) Docker 容器:很簡單,在一個(gè)容器中完成服務(wù)部署,并且配置 Consul 和 Fabio 環(huán)境,這樣容器就會很臃腫,并且每次發(fā)布的時(shí)候都得重新配置 Consul 和 Fabio 環(huán)境,如果服務(wù)很多的話,想想就覺得恐怖。
-
分別獨(dú)立 Docker 容器:服務(wù)部署、配置 Consul 和 Fabio 環(huán)境,都是獨(dú)立容器實(shí)現(xiàn),互不影響,也可以跨服務(wù)實(shí)現(xiàn),簡單靈活。
顯而易見,推薦第二種方式。
回到正題,上面配置命令,在 Mac OS 報(bào)如下錯誤:
$ docker logs consul-client ==> Starting Consul agent... ==> Error starting agent: Failed to start Consul client: Failed to start lan serf: Failed to create memberlist: Could not set up network transport: failed to obtain an address: Failed to start TCP listener on "10.9.10.190" port 8301: listen tcp 10.9.10.190:8301: bind: cannot assign requested address這個(gè)問題花了很多時(shí)間也沒有解決,奇怪的是不使用 Docker,直接運(yùn)行 Consul Client 配置命令,卻是可以的,后來沒辦法,我就在 Mac OS 中使用 Ubuntu 虛擬機(jī)了(版本 14.04),使用的?Vagrant?管理工具。
再重新運(yùn)行配置命令:
$ docker run -d --net=host --name=consul-client consul agent -bind=10.9.10.89 -client=0.0.0.0 -node=vagrant-ubuntu-xishuai -retry-join=10.9.2.236$ docker psCONTAINER ID ? ? ? ?IMAGE ? ? ? ? ? ? ? COMMAND ? ? ? ? ? ? ? ? ?CREATED ? ? ? ? ? ? STATUS ? ? ? ? ? ? ?PORTS ? ? ? ? ? ? ? NAMES9c4988cf475f ? ? ? ?consul ? ? ? ? ? ? ?"docker-entrypoint..." ? 2 seconds ago ? ? ? Up 2 seconds ? ? ? ? ? ? ? ? ? ? ? ? ? ?consul-client$ docker logs consul-client ==> Starting Consul agent... ==> Consul agent running! ? ? ? ? ? Version: 'v1.0.0'Node ID: '34e63f0a-d361-f152-3803-b9fda0642e4d'Node name: 'vagrant-ubuntu-xishuai'Datacenter: 'dc1' (Segment: '') ? ? ? ? ? ?Server: false (Bootstrap: false) ? ? ? Client Addr: [0.0.0.0] (HTTP: 8500, HTTPS: -1, DNS: 8600) ? ? ?Cluster Addr: 10.9.10.89 (LAN: 8301, WAN: 8302) ? ? ? ? ? Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false==> Log data will now stream in as it occurs: ? ?2017/11/14 06:40:52 [INFO] serf: EventMemberJoin: vagrant-ubuntu-xishuai 10.9.10.89 ? ?2017/11/14 06:40:52 [INFO] agent: Started DNS server 0.0.0.0:8600 (udp) ? ?2017/11/14 06:40:52 [INFO] agent: Started DNS server 0.0.0.0:8600 (tcp) ? ?2017/11/14 06:40:52 [INFO] agent: Started HTTP server on [::]:8500 (tcp) ? ?2017/11/14 06:40:52 [INFO] agent: Retry join LAN is supported for: aws azure gce softlayer ? ?2017/11/14 06:40:52 [INFO] agent: Joining LAN cluster... ? ?2017/11/14 06:40:52 [INFO] agent: (LAN) joining: [10.9.2.236] ? ?2017/11/14 06:40:52 [WARN] manager: No servers available ? ?2017/11/14 06:40:52 [ERR] agent: failed to sync remote state: No known Consul servers ? ?2017/11/14 06:40:52 [INFO] serf: EventMemberJoin: agent_1 10.9.2.236 ? ?2017/11/14 06:40:52 [INFO] agent: (LAN) joined: 1 Err: <nil>2017/11/14 06:40:52 [INFO] agent: Join LAN completed. Synced with 1 initial agents ? ?2017/11/14 06:40:52 [INFO] consul: adding server agent_1 (Addr: tcp/10.9.2.236:8300) (DC: dc1)打開 Consul UI 界面,就可以看到我們配置的 Consul Client 了:
2. Docker 運(yùn)行 Fabio 環(huán)境
Fabio?是一個(gè)快速、現(xiàn)代、zero-conf 負(fù)載均衡 HTTP(S) 路由器,用于部署 Consul 管理的微服務(wù)。
Fabio Docker 鏡像地址:https://hub.docker.com/r/magiconair/fabio/
配置命令:
$ docker run -d --net=host --name=fabio -e 'registry_consul_addr=10.9.10.89:8500' magiconair/fabio 需要注意的兩個(gè)屬性值:-
Proxy.LocalIP:10.0.2.15:綁定本機(jī)的 IP 地址,服務(wù)器的 IP 地址是10.9.10.89,所以配置的10.0.2.15是錯誤的,這個(gè) IP 地址內(nèi)網(wǎng)是訪問不了的。
-
Registry.Consul.Addr:10.9.10.89:8500:綁定 Consul 地址,我們上面已經(jīng)完成的 Consul Client 地址就是10.9.10.89:8500,所以是正確的。
這個(gè)配置命令研究了好久,也沒有解決綁定本機(jī) IP 地址的問題,后來又找到了另外一種方式。
首先,在/etc/fabio/目錄下創(chuàng)建一個(gè)fabio.properties文件(示例配置),然后vim fabio.properties增加下面配置:
registry.consul.register.addr = 10.9.10.89:9998registry.consul.addr = 10.9.10.89:8500registry.consul.register.addr綁定 Fabio 地址(本機(jī) IP 地址),registry.consul.addr綁定 Consul 地址。
然后切換到/etc/fabio/目錄,執(zhí)行配置命令:
$ docker run -d -p 9999:9999 -p 9998:9998 --net=host --name=fabio -v $PWD/fabio.properties:/etc/fabio/fabio.properties magiconair/fabio$ docker psCONTAINER ID ? ? ? ?IMAGE ? ? ? ? ? ? ? COMMAND ? ? ? ? ? ? ? ? ?CREATED ? ? ? ? ? ? ?STATUS ? ? ? ? ? ? ?PORTS ? ? ? ? ? ? ? NAMES301fe4a5b40b ? ? ? ?magiconair/fabio ? ?"/fabio -cfg /etc/..." ? About a minute ago ? Up About a minute ? ? ? ? ? ? ? ? ? ? ? fabio9c4988cf475f ? ? ? ?consul ? ? ? ? ? ? ?"docker-entrypoint..." ? 4 hours ago ? ? ? ? ?Up 4 hours ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?consul-client查看執(zhí)行日志:
$ docker logs fabio2017/11/14 10:10:58 [INFO] Version 1.5.3 starting2017/11/14 10:10:58 [INFO] Go runtime is go1.9.22017/11/14 10:10:58 [INFO] Metrics disabled2017/11/14 10:10:58 [INFO] Setting GOGC=8002017/11/14 10:10:58 [INFO] Setting GOMAXPROCS=12017/11/14 10:10:58 [INFO] consul: Connecting to "10.9.10.89:8500" in datacenter "dc1"2017/11/14 10:10:58 [INFO] Admin server access mode "rw"2017/11/14 10:10:58 [INFO] Admin server listening on ":9998"2017/11/14 10:10:58 [INFO] Waiting for first routing table2017/11/14 10:10:58 [INFO] consul: Using dynamic routes2017/11/14 10:10:58 [INFO] consul: Using tag prefix "urlprefix-"2017/11/14 10:10:58 [INFO] consul: Watching KV path "/fabio/config"2017/11/14 10:10:58 [INFO] consul: Manual config changed to #37239382017/11/14 10:10:58 [INFO] HTTP proxy listening on :99992017/11/14 10:10:58 [INFO] Access logging disabled2017/11/14 10:10:58 [INFO] Using routing strategy "rnd"2017/11/14 10:10:58 [INFO] Using route matching "prefix"2017/11/14 10:10:58 [INFO] consul: Health changed to #37273392017/11/14 10:10:59 [INFO] consul: Registered fabio with id "fabio-vagrant-ubuntu-trusty-9998"2017/11/14 10:10:59 [INFO] consul: Registered fabio with address "10.9.10.89"2017/11/14 10:10:59 [INFO] consul: Registered fabio with tags ""2017/11/14 10:10:59 [INFO] consul: Registered fabio with health check to "http://[10.9.10.89]:9998/health"2017/11/14 10:11:00 [INFO] Config updates可以通過 Consul UI,進(jìn)行查看 Fabio 是否正常:
也可以直接瀏覽?http://10.9.10.89:9998/routes?filter=,查看已經(jīng)注冊的服務(wù):
其實(shí),如果不清楚配置命令的話,我們也可以查看 Consul 源碼,有可能會幫助我們熟悉命令,比如(https://github.com/fabiolb/fabio/blob/master/registry/consul/register.go):
3. 使用 Consul 注冊 ASP.NET Core 2.0 服務(wù)
在 Mac OS 中使用 VS Code 開發(fā) ASP.NET Core 2.0 應(yīng)用程序,就像寫 Markdown 一樣方便。
Consul 注冊 ASP.NET Core 2.0 服務(wù),使用的是 Consul 組件,地址:https://github.com/PlayFab/consuldotnet
安裝程序包(VS Code 需要使用NuGet Pakcage Manager命令安裝):
> install-package Conusl然后添加一個(gè)RegisterWithConsul擴(kuò)展服務(wù):
using System;using System.Collections.Generic;
using System.Linq;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
public static class ApplicationBuilderExtensions{ ? ?
public static IApplicationBuilder RegisterWithConsul(this IApplicationBuilder app, IApplicationLifetime lifetime) ? ?{ ? ? ? ?//var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{Program.IP}:8500"));//如果服務(wù)和 Consul 在同一臺服務(wù)器上,使用此代碼var consulClient = new ConsulClient(x => x.Address = new Uri($"http://10.9.10.89:8500"));//請求注冊的 Consul 地址var httpCheck = new AgentServiceCheck(){DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服務(wù)啟動多久后注冊Interval = TimeSpan.FromSeconds(10),//健康檢查時(shí)間間隔,或者稱為心跳間隔HTTP = $"http://{Program.IP}:{Program.Port}/health",//健康檢查地址Timeout = TimeSpan.FromSeconds(5)}; ? ? ? ?// Register service with consulvar registration = new AgentServiceRegistration(){Checks = new[] { httpCheck },ID = Guid.NewGuid().ToString(),Name = Program.ServiceName,Address = Program.IP,Port = Program.Port,Tags = new[] { $"urlprefix-/{Program.ServiceName}" }//添加 urlprefix-/servicename 格式的 tag 標(biāo)簽,以便 Fabio 識別};consulClient.Agent.ServiceRegister(registration).Wait();//服務(wù)啟動時(shí)注冊,內(nèi)部實(shí)現(xiàn)其實(shí)就是使用 Consul API 進(jìn)行注冊(HttpClient發(fā)起)lifetime.ApplicationStopping.Register(() =>{consulClient.Agent.ServiceDeregister(registration.ID).Wait();//服務(wù)停止時(shí)取消注冊}); ? ? ? ?return app;} }
Start.cs配置代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime){ ? ?if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseMvc();app.RegisterWithConsul(lifetime);//here}Program.cs配置代碼:
public class Program{ ? ?public static string IP = ""; ?
?public const int Port = 54917; ?
?public const string ServiceName = "hello-docker"; ? ?public const string Version = "v1"; ?
?
??public static void Main(string[] args) ? ?{ ? ? ? ?//Program.IP = LocalIPAddress;//使用 Docker 的時(shí)候,獲取的是 IP 地址不正確,需要進(jìn)行完善Program.IP = "10.9.10.190";//Docker 容器中的 IP 地址,如果使用 host 網(wǎng)絡(luò)模式,也是主機(jī)的 IP 地址BuildWebHost(args).Run();} ? ?public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseUrls($"http://*:{Program.Port}").UseStartup<Startup>().Build(); ?
??
???public static string LocalIPAddress{ ? ? ?
???get{UnicastIPAddressInformation mostSuitableIp = null; ? ? ? ? ? ?
???var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); ? ? ? ? ? ?foreach (var network in networkInterfaces){ ? ? ? ? ? ? ?
??? ?if (network.OperationalStatus != OperationalStatus.Up) ? ? ? ? ? ? ? ? ?
??? ??continue; ? ? ? ? ? ? ?
??? ??var properties = network.GetIPProperties(); ? ? ? ? ? ? ? ?if (properties.GatewayAddresses.Count == 0) ? ? ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ?
??? ?? ?foreach (var address in properties.UnicastAddresses){ ? ? ? ? ? ? ? ? ?
??? ?? ? ?if (address.Address.AddressFamily != AddressFamily.InterNetwork) ? ? ? ? ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ? ? ? ?
??? ?? ? ??if (IPAddress.IsLoopback(address.Address)) ? ? ? ? ? ? ? ? ? ? ? ?continue; ? ? ? ? ? ? ?
??? ?? ? ?? ? ? ?return address.Address.ToString();}} ? ? ? ? ?
??? ?? ? ?? ? ? ??return mostSuitableIp != null? mostSuitableIp.Address.ToString(): "";}} }
健康檢查配置代碼:
[Route("[controller]")]public class HealthController : Controller{ ?
?private static readonly HttpClient _httpClient; ? ?static HealthController() ? ?{_httpClient = new HttpClient();}[HttpGet] ?
??public IActionResult Get() => Ok("ok"); ? ?//或者使用fabio進(jìn)行健康檢查//[Route("")]//[HttpGet]//public async Task<HttpResponseMessage> GetWithFabio() => await _httpClient.GetAsync("http://127.0.0.1:9998/health");}
4. 使用 Docker 發(fā)布部署 ASP.NET Core 2.0 服務(wù)
我們需要在 ASP.NET Core 2.0 應(yīng)用程序目錄下,添加一個(gè)Dockerfile文件,用來構(gòu)建自定義鏡像(命令參考:使用 Dockerfile 定制鏡像),示例:
FROM microsoft/aspnetcore-build AS build-envWORKDIR /app# copy csproj and restore as distinct layersCOPY *.csproj ./RUN dotnet restore# copy everything else and buildCOPY . ./RUN dotnet publish -c Release -o out# build runtime imageFROM microsoft/aspnetcoreWORKDIR /appCOPY --from=build-env /app/out .ENTRYPOINT ["dotnet", "HelloDocker.Web.dll"]簡單來說,就是先使用microsoft/aspnetcore-build鏡像,還原程序包并發(fā)布,然后再使用microsoft/aspnetcore鏡像,運(yùn)行 ASP.NET Core 2.0 應(yīng)用程序。
構(gòu)建鏡像命令:
$ docker build -t hello-docker:v1 .Sending build context to Docker daemon ?41.98kBStep 1/10 : FROM microsoft/aspnetcore-build AS build-env ---> d6273f7c44d4Step 2/10 : WORKDIR /app ---> e37f90cd9aafRemoving intermediate container a4e4db93ea06Step 3/10 : COPY *.csproj ./ ---> 56278755f94cStep 4/10 : RUN dotnet restore ---> Running in 3b7e8c5d01f4 ?Restoring packages for /app/HelloDocker.Web.csproj... ?Restore completed in 411.99 ms for /app/HelloDocker.Web.csproj. ?Installing System.Security.Principal.Windows 4.4.0-preview1-25305-02. ?Installing Microsoft.NETCore.Platforms 2.0.0-preview1-25305-02. ?Installing System.Security.AccessControl 4.4.0-preview1-25305-02. ?Installing Microsoft.Win32.Registry 4.3.0. ?Installing System.Security.Permissions 4.4.0-preview1-25305-02. ?Installing System.Diagnostics.Process 4.3.0. ?Installing Newtonsoft.Json 10.0.2. ?Installing System.Net.Http.WinHttpHandler 4.0.0. ?Installing CoreCompat.System.Drawing.v2 5.2.0-preview1-r131. ?Installing System.Data.Common 4.3.0. ?Installing System.Security.Cryptography.Pkcs 4.3.0. ?Installing System.Xml.XPath.XmlDocument 4.3.0. ?Installing Pomelo.EntityFrameworkCore.MySql 2.0.0. ?Installing MySqlConnector 0.26.4. ?Installing Pomelo.JsonObject 1.1.1. ?Installing Consul 0.7.2.3. ?Installing EPPlus.Core 1.5.2. ?Generating MSBuild file /app/obj/HelloDocker.Web.csproj.nuget.g.props. ?Generating MSBuild file /app/obj/HelloDocker.Web.csproj.nuget.g.targets. ?Restore completed in 5.71 sec for /app/HelloDocker.Web.csproj. ---> cdf6ca65acf6Removing intermediate container 3b7e8c5d01f4Step 5/10 : COPY . ./ ---> fffa81d15ddcStep 6/10 : RUN dotnet publish -c Release -o out ---> Running in 291c8eea750fMicrosoft (R) Build Engine version 15.4.8.50001 for .NET CoreCopyright (C) Microsoft Corporation. All rights reserved. ?HelloDocker.Web -> /app/bin/Release/netcoreapp2.0/HelloDocker.Web.dll ?HelloDocker.Web -> /app/out/ ---> 078311772175Removing intermediate container 291c8eea750fStep 7/10 : FROM microsoft/aspnetcore ---> b97d3cf55223Step 8/10 : WORKDIR /app ---> b0637e3d706bRemoving intermediate container 7095565fbbcaStep 9/10 : COPY --from=build-env /app/out . ---> c3cb8a708c4bStep 10/10 : ENTRYPOINT dotnet HelloDocker.Web.dll ---> Running in d4111dc055f8 ---> 29121f0eb2b0Removing intermediate container d4111dc055f8Successfully built 29121f0eb2b0Successfully tagged hello-docker:v1上面構(gòu)建鏡像的過程,非常詳細(xì),我們可以得到很多的信息,這邊就不敘述了,構(gòu)建完鏡像之后,我們可以查看下是否成功:
$ docker imagesREPOSITORY ? ? ? ? ? ? ? ? ? TAG ? ? ? ? ? ? ? ? IMAGE ID ? ? ? ? ? ?CREATED ? ? ? ? ? ? SIZEhello-docker ? ? ? ? ? ? ? ? v1 ? ? ? ? ? ? ? ? ?29121f0eb2b0 ? ? ? ?Less than a second ago ? 284MB<none> ? ? ? ? ? ? ? ? ? ? ? <none> ? ? ? ? ? ? ?078311772175 ? ? ? ?Less than a second ago ? 1.9GBmicrosoft/aspnetcore-build ? latest ? ? ? ? ? ? ?d6273f7c44d4 ? ? ? ?4 days ago ? ? ? ? ?1.85GBmicrosoft/aspnetcore ? ? ? ? latest ? ? ? ? ? ? ?b97d3cf55223 ? ? ? ?4 days ago ? ? ? ? ?280MBconsul ? ? ? ? ? ? ? ? ? ? ? latest ? ? ? ? ? ? ?dff07cab6abd ? ? ? ?9 days ago ? ? ? ? ?51.8MBmagiconair/fabio ? ? ? ? ? ? latest ? ? ? ? ? ? ?b0d96559369f ? ? ? ?10 days ago ? ? ? ? 11.8MBhello-docker下面沒命名的鏡像,是臨時(shí)生成的,作用是使用microsoft/aspnetcore-build鏡像,還原程序包的時(shí)候,不需要重新安裝了。
另外,我們可以在 ASP.NET Core 2.0 應(yīng)用程序目錄下,添加.dockerignore文件,來減少我們構(gòu)建的鏡像文件大小,示例:
bin/* obj/*需要說明下,Consul 和 Fabio 我都是部署在虛擬機(jī)的 Docker 容器中,ASP.NET Core 2.0 應(yīng)用程序,我打算運(yùn)行在 Mac OS 系統(tǒng)中,也就是說服務(wù)和 Consul 是跨服務(wù)器的。
運(yùn)行命令:
$ docker run -d -p 54917:54917 --name hello-docker-web hello-docker:v1查看下是否運(yùn)行成功:
$ docker psCONTAINER ID ? ? ? ?IMAGE ? ? ? ? ? ? ? COMMAND ? ? ? ? ? ? ? ? ?CREATED ? ? ? ? ? ? ? ? ?STATUS ? ? ? ? ? ? ?PORTS ? ? ? ? ? ? ? ? ? ? ?NAMES396d42f37185 ? ? ? ?hello-docker:v1 ? ? ? ?"dotnet Hello-Dock..." ? Less than a second ago ? Up 2 seconds ? ? ? ?0.0.0.0:54917->54917/tcp ? hello-docker-web$ docker logs hello-docker-webHosting environment: ProductionContent root path: /appNow listening on: http://[::]:54917Application started. Press Ctrl+C to shut down.可以看到,運(yùn)行是成功的。
我們可以請求驗(yàn)證下:
$ curl http://10.9.10.190:54917/api/values ["value1","value2"]或者使用 Fabio 的網(wǎng)關(guān)請求(Fabio 可以用作負(fù)載均衡):
$ curl http://10.9.10.89:9999/hello-docker/api/values ["value1","value2"]我們可以查看 Consul UI 中的服務(wù)是否運(yùn)行正常:
圖片
或者查看 Fabio UI 中的服務(wù)是否存在(通過健康檢查后會出現(xiàn)):
圖片
查看資料:
-
Dockerize a .NET Core application
-
consul - Hub.Docker
-
magiconair/fabio - Hub.Docker
-
todo-backend-aspnetcore
-
Docker 結(jié)合 Consul 實(shí)現(xiàn)的服務(wù)發(fā)現(xiàn)(一)
-
使用 Consul 進(jìn)行服務(wù)發(fā)現(xiàn)
-
服務(wù)發(fā)現(xiàn) - consul 的介紹、部署和使用
-
fabio Installation
-
Consul + fabio 實(shí)現(xiàn)自動服務(wù)發(fā)現(xiàn)、負(fù)載均衡
-
.NET API for Consul
-
Winton.Extensions.Configuration.Consul
-
Configuring .NET Core Applications using Consul
-
Using Consul for Service Discovery with ASP.NET Core
-
Service Discovery And Health Checks In ASP.NET Core With Consul
-
How to self register a service with Consul
-
Nomad and Consul Configuration Overview
原文地址:http://www.cnblogs.com/xishuai/p/ubuntu-docker-consul-fabio-aspnet-core.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的Docker ASP.NET Core 2.0 微服务跨平台实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net core WebAPI实
- 下一篇: 讨论.NET Core 配置对GC 工作