Пример #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseMvc();

            var consulOption = new ConsulOption()
            {
                ServiceName        = Configuration["ServiceName"],
                ServiceIP          = Configuration["ServiceIP"],
                ServicePort        = Convert.ToInt32(Configuration["ServicePort"]),
                ServiceHealthCheck = Configuration["ServiceHealthCheck"],
                Address            = Configuration["ConsulAddress"],
            };

            app.RegisterConsul(lifetime, consulOption);

            /*String ip = Configuration["ip"];//部署到不同服务器的时候不能写成127.0.0.1或者0.0.0.0,因为这是让服务消费者调用的地址
             * int port = int.Parse(Configuration["port"]);//获取服务端口
             * var client = new ConsulClient(ConfigurationOverview); //回调获取
             * var result = client.Agent.ServiceRegister(new AgentServiceRegistration()
             * {
             *  ID = "ServerNameFirst" + Guid.NewGuid(),//服务编号保证不重复
             *  Name = "ServerFirst",//服务的名称 集群使用consul
             *  Address = ip,//服务ip地址
             *  Port = port,//服务端口
             *  Check = new AgentServiceCheck //健康检查
             *  {
             *      DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//服务启动多久后反注册
             *      Interval = TimeSpan.FromSeconds(10),//健康检查时间间隔,或者称为心跳间隔(定时检查服务是否健康)
             *      HTTP = $"http://{ip}:{port}/api/Health",//健康检查地址
             *      Timeout = TimeSpan.FromSeconds(5)//服务的注册时间
             *  }
             * });*/
        }
Пример #2
0
        public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IApplicationLifetime lifetime, ConsulOption consulOption)
        {
            var consulClient = new ConsulClient(x =>
            {
                // consul 服务地址
                x.Address = new Uri(consulOption.Address);
            });

            var registration = new AgentServiceRegistration()
            {
                ID      = Guid.NewGuid().ToString(),
                Name    = consulOption.ServiceName, // 服务名
                Address = consulOption.ServiceIP,   // 服务绑定IP
                Port    = consulOption.ServicePort, // 服务绑定端口
                Check   = new AgentServiceCheck()
                {
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5), //服务启动多久后注册
                    Interval = TimeSpan.FromSeconds(10),                      //健康检查时间间隔
                    HTTP     = consulOption.ServiceHealthCheck,               //健康检查地址
                    Timeout  = TimeSpan.FromSeconds(5)
                },
                Tags = new string[] { "Test", ".net core" }
            };

            // 服务注册
            consulClient.Agent.ServiceRegister(registration).Wait();

            // 应用程序终止时,服务取消注册
            lifetime.ApplicationStopping.Register(() =>
            {
                consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            });
            return(app);
        }