zoukankan      html  css  js  c++  java
  • 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

    四、查看注册结果

  • 相关阅读:
    纳尼?不用码代码,就可回归主流程,一只海豚就可以做到
    教育产品-组件化视觉设计实践
    从整理看视觉设计(网易云课堂我的学习中心-微专业视觉优化)
    搜索意图识别浅析
    如何配置使用Dnsmasq
    如何实现最佳的跨平台游戏体验?Unity成亮解密实时渲染技术!
    PAT 1024. Palindromic Number
    PAT 1023. Have Fun with Numbers
    PAT 1022. Digital Library
    PAT 1021. Deepest Root
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/15122011.html
Copyright © 2011-2022 走看看