windows consul 配置

1. 直接启动,然后web服务主动通知consul

cmd consul agent -dev

webapi配置文件 appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "AppName": "ServiceA"
}

webapi ServiceA服务

 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();
        }

        //关闭的时候在consul中移除
        private void OnStopped()
        {
            var client = new ConsulClient();
            //根据ID在consul中移除当前服务
            client.Agent.ServiceDeregister("ServiceA");
        }
        private void OnStart()
        {
            var client = new ConsulClient();
            //健康检查
            var httpCheck = new AgentServiceCheck()
            {
                //服务出错一分钟后 会自动移除
                DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
                //每10秒发送一次请求到 下面的这个地址 这个地址就是当前API资源的地址
                Interval = TimeSpan.FromSeconds(10),
                HTTP = $"http://localhost:5011/health"
            };

            var agentReg = new AgentServiceRegistration()
            {
                //这台资源服务的唯一ID
                ID = "ServiceA",
                Check = httpCheck,
                Address = "localhost",
                Name = "ApiService",
                Port = 5011,
                Tags = new string[1] { "ServiceA" }
            };
            client.Agent.ServiceRegister(agentReg).ConfigureAwait(false);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime lifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //注册项目启动的方法
            lifetime.ApplicationStarted.Register(OnStart);
            //注册项目关闭的方法
            lifetime.ApplicationStarted.Register(OnStopped);

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

ServiceB 服务

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();
        }

        //关闭的时候在consul中移除
        private void OnStopped()
        {
            var client = new ConsulClient();
            //根据ID在consul中移除当前服务
            client.Agent.ServiceDeregister("ServiceB");
        }
        private void OnStart()
        {
            var client = new ConsulClient();
            //健康检查
            var httpCheck = new AgentServiceCheck()
            {
                //服务出错一分钟后 会自动移除
                DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
                //每10秒发送一次请求到 下面的这个地址 这个地址就是当前API资源的地址
                Interval = TimeSpan.FromSeconds(10),
                HTTP = $"http://localhost:5012/health"
            };

            var agentReg = new AgentServiceRegistration()
            {
                //这台资源服务的唯一ID
                ID = "ServiceB",
                Check = httpCheck,
                Address = "localhost",
                Name = "ApiService",
                Port = 5012
            };
            client.Agent.ServiceRegister(agentReg).ConfigureAwait(false);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime lifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //注册项目启动的方法
            lifetime.ApplicationStarted.Register(OnStart);
            //注册项目关闭的方法
            lifetime.ApplicationStarted.Register(OnStopped);

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

2 consul服务配置webapi服务

根目录新建service.json

{
  "services": [
    {
      "id": "ApiServiceA",
      "name": "ApiService",
      "tags": [ "ApiServiceA" ],
      "address": "localhost",
      "port": 5011,
      "checks": [
        {
          "id": "ApiServiceA_Check",
          "name": "ApiServiceA_Check",
          "http": "http://localhost:5011/health",
          "interval": "10s",
          "tls_skip_verify": false,
          "method": "GET",
          "timeout": "1s"
        }
      ]
    },
    {
      "id": "ApiServiceB",
      "name": "ApiService",
      "tags": [ "ApiServiceB" ],
      "address": "localhost",
      "port": 5012,
      "checks": [
        {
          "id": "ApiServiceB_Check",
          "name": "ApiServiceB_Check",
          "http": "http://localhost:5012/health",
          "interval": "10s",
          "tls_skip_verify": false,
          "method": "GET",
          "timeout": "1s"
        }
      ]
    }
  ]
}

cmd consul agent -dev -config-dir=D:MicroService【工具】consul_1.9.0_windows_amd64

原文地址:https://www.cnblogs.com/smartsensor/p/14666169.html