Consul学习笔记

119 阅读1分钟

今天看别人的文章,看完了,好像都明白了。也跟着步骤来建项目。结果没有成功。

一下载Consul

官网地址:www.consul.io/;

image.png 下载下来了解压,然后直接点击,居然是一闪而过。以为下载错了,然后又删除了。我又回头去看文章,然后又下载下来,解压。 这次我用cmd命令打开

image.png

然后获得了与文章内容一致的效果。 接着学着配置config文件夹和data 文件夹

image.png 然后弄配置文件services.json

{
  "services": [
    {
      "id": "dockerApi",
      "name": "DockerApiName",
      "tags": [
        "primary"
      ],
      "address": "127.0.0.1",
      "port": 3662,
      "checks": [
        {
          "name":"DockerDemo",
          "http":"http://127.0.0.1:3662/api/health",
          "interval": "5s",
          "timeout": "20s"
        }
      ]
    }
    
  ]
}

我是用之前创建的dockerdemo项目。 依次学者配置

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Consul": {
    "ConsulAddr": "http://localhost:8500",
    "ServiceIP": "127.0.0.1",
    "ServicePort": "6688",
    "Tags": "test6688"
  },
  "AllowedHosts": "*"
}

在Startup文件里创建RegistService方法

   private void RegistService()
        {
            var configConsul = Configuration.GetSection("Consul");
            ConsulClient client = new ConsulClient(c =>
            {
                c.Address = new Uri(configConsul["ConsulAddr"]);
                c.Datacenter = "dc1";
            });

            string serviceIp = configConsul["ServiceIP"];
            string servicePort = configConsul["ServicePort"];
            string serviceTags = configConsul["Tags"];
            client.Agent.ServiceRegister(new AgentServiceRegistration
            {
                ID = "Code6688",
                Name = "Code6688Name",
                Address = serviceIp,
                Port = string.IsNullOrEmpty(servicePort) ? 0 : int.Parse(servicePort),
                Tags = serviceTags.Split(','),
                Check = new AgentServiceCheck
                {
                    HTTP = $"http://{serviceIp}:{servicePort}/api/Health",
                    Interval = TimeSpan.FromSeconds(5),
                    Timeout = TimeSpan.FromSeconds(10),
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20)
                }
            });
        }

然后在Configure 方法里调用

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            RegistService();
        }

原以为这样就可以了。结果打开这个链接http://localhost:8500/ui/dc1/services 死活没有内容,于是我又回头去检查代码。发现了这句

 HTTP = $"http://{serviceIp}:{servicePort}/api/Health",

就是链接和文章里的不一样,我的是https,而他的不是。我修改项目属性,去掉https,并修改了应用url

image.png 接着运行,项目可以运行了。

然后又去修改services.json 中的链接,然后再运行 consul.exe agent -dev -config-file ./config -data-dir ./data

image.png 这下和之前运行果然有些不一样了。然后有去打开链接http://localhost:8500/ui/dc1/services 这下页面有内容了。

image.png 算是成功运行起来了。 体会,还是要多动手实践,看懂并不代表会了。自己操作实践一次,会遇到问题,并解决问题,收获比看更多。 参考:www.cnblogs.com/zoe-zyq/p/1…