一,我们新建两个项目结构如下:ConsulTestDemo(AP项目)和TestClient(客户端访问调用)
二,我们先写Consul的接口注册服务配置,如下代码
using Consul; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ConsulTestDemo.Utils { /// <summary> /// 自己封装的注册类 /// </summary> public static class ConsulUtil { public static void ConsulRegist(this IConfiguration configuration) { ConsulClient client = new ConsulClient(c => { ///找到 Consul地址,默认地址 c.Address = new Uri("http://localhost:8500/"); c.Datacenter = "dc1"; }); string ip = configuration["ip"]; int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]); int port = int.Parse(configuration["port"]);//命令行参数必须传入 client.Agent.ServiceRegister(new AgentServiceRegistration() { ///微服务唯一标识,唯一的 ID = "service-" + port, Name = "TestConsulService",//组名称-Group,同意份代码集群的组 Address = ip,//其实应该写ip地址 Port = port,//不同实例 ///标签参数,可以在注册的时候根据拿到tags标签来当权重,可以是属于地址参数上的tag ///注册服务时指定权重,分配时获取权重并以此为依据分配实例 Tags = new string[] { weight.ToString() }, #region 配置心跳检查的 Check = new AgentServiceCheck() { ///心跳时间 Interval = TimeSpan.FromSeconds(12), ///心跳地址 HTTP = $"http://{ip}:{port}/Api/Health/Index", ///超时时间 Timeout = TimeSpan.FromSeconds(5), ///取消服务注册时间 DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5) } #endregion }); Console.WriteLine($"http://{ip}:{port}完成注册"); } } }
我们将注册服务的放到管道的最后,如下管道的代码
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using ConsulTestDemo.Utils; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace ConsulTestDemo { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); ///服务注册consul ///启动的时候已经完成注册,这里可能会有疑惑,当请求来的时候会不会再注册一次 ///这里管道逻辑是请求的时候已经返回了,不会执行这个注册了 this.Configuration.ConsulRegist(); } } }
三,现在服务和注册的代码已经写好了,我们启动consul
1,用命令打开下载好的consul文件所在目录,输入代码启动consul
如下代码
cd g:/project consul_1.6.2.exe agent –dev
四,我们在浏览器打开consul自带的页面,输入http://localhost:8500,如下界面
五,我们用命令行启动写好接口服务,注册两个服务,打开两个命令窗口,端口5001,5002,如下命令行,weight是权重参数
端口5001
cd G:projectCoreApiConsulTestDemoConsulTestDemoinDebug etcoreapp3.1 dotnet consultestdemo.dll --urls="http://*:5001" --ip="127.0.0.1" --port=5001 --weight=1
端口5002
cd G:projectCoreApiConsulTestDemoConsulTestDemoinDebug etcoreapp3.1 dotnet consultestdemo.dll --urls="http://*:5002" --ip="127.0.0.1" --port=5002 --weight=2
六,现在去看consul的界面,发现两个服务已经注册进去
七,我们做了心跳检测,如下Service Checks,现在我们在其中一个命令行窗口关闭一个程序(PS:快捷键----Ctrl+C 停止命令启动的程序 )
启动服务如下
我们停止了5001端口这个服务,心跳立马检查出来服务器报错 ,ctrl+c停止控制台
如下立马检测出来有问题,一个程序出错,停止了
八,如上总结,consul的服务注册和发现,以及心跳检测就这样完了