心澄欲遣

不践迹,亦不入于室

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1. 直接启动Consul服务,然后再启动web服务,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

 

访问地址 http://localhost:8500

 

posted on 2021-04-16 11:24  心澄欲遣  阅读(452)  评论(0编辑  收藏  举报
欢迎第myspace graphics个访客