zoukankan      html  css  js  c++  java
  • WIndows下Consul的简单使用

    Consul最常用的就是服务注册与发现,健康检查,接下来演示一下在Windows上如何使用

    Step1:在官网下载consul.exe(下载较慢,耐心等待)

    Step2:cmd到这个路径下,输入consul.exe agent -dev开启后,打开http://localhost:8500/

    Step3:在代码中,进行服务注册

     public static class ConsulHelper
        {
            public static void Init( this IConfiguration _configuration)
            {
                ConsulClient clinet = new ConsulClient(c =>
                {
                    c.Address = new Uri("http://localhost:8500/");
                    c.Datacenter = "dcl";
                });
    
                string ip = _configuration["ip"];
                int port = int.Parse(_configuration["port"]);
                clinet.Agent.ServiceRegister(new AgentServiceRegistration() { 
                    ID="service"+Guid.NewGuid(),
                    Name="shenqing",
                    Address = ip,
                    Port = port,
                    Check = new AgentServiceCheck() { 
                        Interval = TimeSpan.FromSeconds(12),//间隔12秒一次
                        HTTP = $"http://{ip}:{port}/API/Health/Index",
                        Timeout = TimeSpan.FromSeconds(52),//检测等待时间
                        DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)//失败多久后移除,最小值60秒
                    }
                }).Wait();
            }
        }
    View Code

    Step4:示例 同一个服务,用三个不同的端口开启

     Step5:此时查看Consuly页面,可以看到已经有3个service

     暂停一个服务后,页面可看到一个X,并在你配置的一段时间后,会在列表里移除,这就是健康检查。

  • 相关阅读:
    python常见对象的结构
    python不可变对象
    python常用对象使用方法
    python对象分类
    Binary Tree Serialization
    Two nodes of a BST are swapped, correct the BST
    Redis数据库
    CGI和FastCGI的区别
    mysql索引
    Python和Golang的应用
  • 原文地址:https://www.cnblogs.com/xingzhu-nan/p/12592834.html
Copyright © 2011-2022 走看看