• Win10下Consul的搭建和.Net5的注册和获取(简单版)


    Consul的下载就不说了,解压包后吧安装路劲放到系统环境变量Path里面去

    consul的启动命令

    consul agent -dev

    验证启动成功网址:localhost:8500

    启动后就要给consul注册了

    全局仅一次的注册放在StartUp里,怎么封装先不说,具体实现是这样的

     //找到Consul
                ConsulClient client = new ConsulClient(c =>
                {
                    c.Address = new Uri("http://localhost:8500/");
                    c.Datacenter = "dcl";
                });
                string ip = Configuration["ip"];
                int port = Convert.ToInt32(Configuration["port"]);
                string tag = Configuration["tag"];
    
    
                client.Agent.ServiceRegister(new AgentServiceRegistration
                {
                    ID = $"Steven:{Guid.NewGuid()}",
                    Name = "StevenGroup",
                    Address = ip,
                    Port = port,
                    Tags = new string[] { tag },
                    Check = new AgentServiceCheck
                    {
                        Interval = TimeSpan.FromSeconds(10),//间隔固定的时间访问一次,https://localhost:44308/api/Health
                        HTTP = $"http://{ip}:{port}/Heart",//健康检查地址
                        Timeout = TimeSpan.FromSeconds(5)
                    }
                });
                Console.WriteLine("注册成功");

    我这里是通过控制台传参数进去给IP和Port的

    多个实例注册后

    值得一提的是在注册中的Name并不是标识,而是分组名称。。。。。

    这样就注册完事儿了,接下来说怎么去获取地址

           ConsulClient client = new ConsulClient(c =>
                {
                    c.Address = new Uri("http://localhost:8500/");
                    c.Datacenter = "dcl";
                });
                var response = client.Agent.Services().Result.Response;
    
    
                Uri uri = new Uri("http://StevenGroup/weatherforecasta");
                string groupName = uri.Host;
                AgentService agentService = null;
                var dic = response.Where(s => s.Value.Service.Equals(groupName, StringComparison.OrdinalIgnoreCase)).Reverse().ToArray();
    
                agentService = dic[0].Value;

    应该很好懂,主要是取出来,至于去取哪一个作为你的转发项,那就看你怎么分配了

    失败重试的逻辑我没写。就这样吧,这篇的目的就是让你的Consul先能跑起来

  • 相关阅读:
    面向对象--选课系统作业
    面向对象--本章总结
    面向对象--本章总结---练习题
    16 python 异常处理
    5.15 python 面向对象的软件开发&领域模型
    14 元类
    intellijidea课程 intellijidea神器使用技巧 6-2 数据库关联
    intellijidea课程 intellijidea神器使用技巧 6-1 Spring的关联
    intellijidea课程 intellijidea神器使用技巧 5-2 localhistory
    intellijidea课程 intellijidea神器使用技巧 5-1 svn相关
  • 原文地址:https://www.cnblogs.com/SevenWang/p/15788592.html
Copyright © 2020-2023  润新知