Kong.Net组件的使用
经过上面的操作后,使用是没有多大问题的了,但是应用基于Docker启动后容器IP也是不固定的,那么手动添加的场景肯定不方便,不灵活。国人开源了一款Kong.Net-https://github.com/lianggx/Kong.Net,让微服务应用在启动后把他本身的信息注册到Kong,这样Kong也不需要与Consul做整合,可以理解成微服务应用通过Kong.Net把IP+Port注册到了kong里。
/ This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, KongClient kongClient) { UseKong(app, kongClient); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } public void UseKong(IApplicationBuilder app, KongClient kongClient) { var upStream = Configuration.GetSection("kong:upstream").Get<UpStream>(); var target = Configuration.GetSection("kong:target").Get<TargetInfo>(); var uri = new Uri(Configuration["server.urls"]); // This target is your host:port target.Target = uri.Authority; app.UseKong(kongClient, upStream, target); }
Consul的部署
在Server C执行以下指令
#Server模式 docker run -d --ip=10.0.1.101 --net=overlay -p 8500:8500 -p 8600:8600/udp --name=consul-server-c consul agent -server -ui -node=consul-server-c -bootstrap-expect=1 -advertise=10.0.1.101 -bind=10.0.1.101 -client=0.0.0.0 #Client模式 docker run -d --ip=10.0.1.102 --net=overlay --name=consul-client-c consul agent -node=consul-client-c -advertise=10.0.1.102 -bind=10.0.1.102 -client=0.0.0.0 -join=10.0.1.101 在Server A执行下面指令 #Server模式 docker run -d --ip=10.0.1.103 --net=overlay -p 8500:8500 -p 8600:8600/udp --name=consul-server-a consul agent -server -ui -node=consul-server-a -bootstrap-expect=1 -advertise=10.0.1.103 -bind=10.0.1.103 -client=0.0.0.0 -join=10.0.1.101 #Client模式 docker run -d --ip=10.0.1.104 --net=overlay --name=consul-client-a consul agent -node=consul-client-a -advertise=10.0.1.104 -bind=10.0.1.104 -client=0.0.0.0 -join=10.0.1.101 在Server B执行以下指令 #Server模式 docker run -d --ip=10.0.1.105 --net=overlay -p 8500:8500 -p 8600:8600/udp --name=consul-server-b consul agent -server -ui -node=consul-server-b -bootstrap-expect=1 -advertise=10.0.1.105 -bind=10.0.1.105 -client=0.0.0.0 -join=10.0.1.101 #Client模式 docker run -d --ip=10.0.1.106 --net=overlay --name=consul-client-b consul agent -node=consul-client-b -advertise=10.0.1.106 -bind=10.0.1.106 -client=0.0.0.0 -join=10.0.1.101
服务注册
.Net Core应用注册到Consul,需要注意的是得在应用启动后把服务注册到Consul(lifetime.ApplicationStarted),不然是无法拿到微服务应用在Overlay2的地址,微服务默认是用HTTP因为是内网应用,所以不需要HTTPS,端口也是默认80,因为Docker会给每个容器分配一个独立的IP。此外Tags是Fabio约定的格式,主要让Fabio路由用的。
/// <summary> /// Consul服务注册 /// </summary> /// <param name="app"></param> /// <param name="lifetime"></param> /// <param name="configuration"></param> /// <returns></returns> public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime, IConfiguration configuration) { var option = configuration.GetSection("Consul").Get<ConsulOption>(); option.ThrowIfNull(); //创建Consul客户端 var consulClient = new ConsulClient(x => x.Address = new Uri(option.ConsulHost));//请求注册的 Consul 地址 AgentServiceRegistration registration = null; lifetime.ApplicationStarted.Register(() => { var selfHost = new Uri("http://" + LocalIpAddress + ":" + option.SelfPort); //注册服务 registration = new AgentServiceRegistration { Checks = new[] { new AgentServiceCheck { Interval = TimeSpan.FromSeconds(option.HealthCheckInterval), HTTP = $"{selfHost.OriginalString}/health",//健康检查地址 Timeout = TimeSpan.FromSeconds(3) } }, ID = selfHost.OriginalString.EncodeMd5String(), Name = option.ServiceName, Address = selfHost.Host, Port = selfHost.Port, Tags = new[] { $"urlprefix-/{option.ServiceName} strip=/{option.ServiceName}" }//添加 urlprefix-/servicename 格式的 tag 标签,以便 Fabio 识别 }; consulClient.Agent.ServiceRegister(registration).Wait(); }); //反注册服务 lifetime.ApplicationStopping.Register(() => { if (registration != null) consulClient.Agent.ServiceDeregister(registration.ID).Wait(); }); return app; }
.Net微服务配置
{ "Logging": { "LogLevel": { "Default": "Warning", "Microsoft": "Information" } }, "AllowedHosts": "*", "redisUrl": "", "MongoDbUrl": "", "Consul": { "ServiceName": "Msg", "ConsulHost": "http://10.0.1.101:8500", "SelfPort": 80 }, "IsDebug": false }
Docker构建.Net Core微服务
Docker File
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base WORKDIR /app EXPOSE 80 FROM base AS final WORKDIR /app COPY ./ /app ENV TZ=Asia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone Docker构建指令 docker build -t msgserver . docker run -d -p 8801:80 --network=overlay --name msgserver msgserver
Fabio的部署
registry_consul_addr为Consul的Overlay2的IP,可以通过docker inspect指令进行查看。
docker run -d --net=overlay -p 443:443 -p 9998:9998 -p 9999:9999 --name=fabio -e 'registry_consul_addr=10.0.1.101:8500' magiconair/fabio
部署成功后,可以通过fabio_ip+9998端口查看服务注册的情况。
然后可以fabio_ip+9999进行请求转发,下面GIF效果图