Consul服务注册与发现

ConsulHashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,比如 AirbnbSmartStack等相比,Consul的方案更“一站式”,内置了服务注册与发现框 架、分布一致性协议实现、健康检查、Key/Value存储、多数据中心方案,不再需要依赖其他工具(比如ZooKeeper等)。使用起来也较 为简单。Consul用Golang实现,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合

一、项目使用nuGet安装consul包

 

二、服务器安装Consul服务,本地使用windows版。

启动Consul服务

consul agent -dev     

服务地址

localhost:8500

三、编写注册服务代码

public static class ConsulSetting
    {
        public static void Register(this IConfiguration configuration)
        {
            ConsulClient client = new ConsulClient(c =>
            {
                c.Address = new Uri("http://localhost:8500");
                c.Datacenter = "dc1";
            });
            string ip = string.IsNullOrWhiteSpace(configuration["ip"]) ? "192.168.0.157" : configuration["ip"];
            int port = int.Parse(configuration["port"]);
            int weight = string.IsNullOrWhiteSpace(configuration["weight"]) ? 1 : int.Parse(configuration["weight"]);
            client.Agent.ServiceRegister(new AgentServiceRegistration()
            {
                ID="service"+Guid.NewGuid(), //唯一的名字
                Name="xiaoyaodijun",//服务分组,不同的服务分组不同
                Address=ip,
                Port=port,
                Tags=new string[] {weight.ToString()},//标签
                //Check=new AgentServiceCheck()
                //{
                //    Interval=TimeSpan.FromSeconds(12),//间隔12秒一次
                //    HTTP=$"http://{ip}:{port}/Api/Health/Index",
                //    Timeout=TimeSpan.FromSeconds(5),//检测等待时间
                //    DeregisterCriticalServiceAfter=TimeSpan.FromSeconds(20),//失败后多久移除
                //}
            });
            Console.WriteLine($"注册成功{ip}:{port}");

        }
    }
  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();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "TestWebApi", Version = "v1" });
            });
        }

        // 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.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebApi v1"));
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            Configuration.Register(); //注册Consul到项目中
        }
    }

dotnet运行命令

dotnet run  --urls=https://*:5000
 dotnet run --urls="http://*:5004" --ip=127.0.0.1  --port=5004 --weight=1

四、查看注册结果

 

posted @ 2021-08-10 08:37  逍遥帝君  阅读(362)  评论(0编辑  收藏  举报