/// <summary> /// 服务注册 /// </summary> /// <param name="service"></param> /// <returns></returns> public Task <bool> RegistrationService(MangoService service) { if (service == null) { throw new ArgumentNullException(nameof(service)); } if (string.IsNullOrEmpty(HealthCheckUrl)) { throw new NullReferenceException("请设置服务健康检查Url"); } var httpCheck = new AgentServiceCheck() { DeregisterCriticalServiceAfter = DeregisterCriticalServiceAfter, Interval = Interval, HTTP = HealthCheckUrl, Timeout = Timeout }; // Register service with consul var registration = new AgentServiceRegistration() { Checks = new[] { httpCheck }, ID = service.Id, Name = service.ServiceName, Address = service.IP, Port = Convert.ToInt32(service.Port), //Tags = new[] { $"urlprefix-/{service.ServiceName}" } }; _consulClient.Agent.ServiceRegister(registration).Wait(); return(Task.FromResult(true)); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime) { app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("all"); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); #region ·þÎñ×¢²á if (Env.IsProduction()) { var token = Configuration["Consul:Token"]; var consulIp = Configuration["Consul:Ip"]; var consulPort = Configuration["Consul:Port"]; var serviceName = Configuration["Service:Name"]; var servicePort = Configuration["Service:Port"]; var healthCheck = Configuration["Service:HealthCheck"]; var currentIp = Configuration["Service:Ip"]; var rc = new ConsulRegistration($"http://{consulIp}:{consulPort}", token) { HealthCheckUrl = $"http://{currentIp}:{servicePort}/{healthCheck}" }; var se = new MangoService { Id = Guid.NewGuid().ToString(), IP = currentIp, Port = servicePort, ServiceName = serviceName }; app.RegisterConsulService(rc, se, lifetime); } #endregion }
/// <summary> /// 向consul注册服务 /// </summary> /// <param name="app"></param> /// <param name="serviceRegistration"></param> /// <param name="serviceEntity"></param> /// <param name="lifetime"></param> /// <returns></returns> public static IApplicationBuilder RegisterConsulService(this IApplicationBuilder app, IServiceRegistration serviceRegistration, MangoService serviceEntity, IHostApplicationLifetime lifetime) { //服务注册 serviceRegistration.RegistrationService(serviceEntity); //结束时取消注册 lifetime.ApplicationStopping.Register(() => { serviceRegistration.DeregisterService(serviceEntity); }); return(app); }
/// <summary> /// 取消注册 /// </summary> /// <param name="service"></param> /// <returns></returns> public Task <bool> DeregisterService(MangoService service) { _consulClient.Agent.ServiceDeregister(service.Id).Wait(); return(Task.FromResult(true)); }