Consul的使用

Consul可以用来进行服务发现,我们来测试下Consul的使用

一.Consul基本使用

先需要下载Consul的程序https://www.consul.io/downloads.html

我们这里下载Windows 64版本。

下载后直接解压就行了,里面会有一个consul.exe文件。通过命令可以启动consul.exe agent -dev

此时我们就可以在浏览器中通过http://localhost:8500/进行访问consul了

这就说明我们的consul启动成功了。

二.Consul和.net程序的结合使用

通过Consul来对.net程序进行服务发现可以有两种方法

通过代码的方式和通过配置文件的方式,下面我们就来讲下这两种方式怎么实现的吧

我们新建一个webapi项目ConsulDemo

A.代码的方式

通过nuget安装consul

创建一个webapi控制器HealthController只需简单的实现一个api返回ok就行了,这个是用来做健康监测的,监测这个webapi程序是否正常运行,能访问到就说明正常运行。

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HealthController : ControllerBase
    {
        public IActionResult Index() {
            return new OkResult();
        }
    }

 

然后创建一个ConsulHelper类

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 = "127.0.0.1";
            int port = 5000;
            clinet.Agent.ServiceRegister(new AgentServiceRegistration() {
                ID = "service" + Guid.NewGuid(),
                Name = "test",
                Address = ip,
                Port = port,
                Check = new AgentServiceCheck() {//此处就是健康监测需要配置的,配置刚才创建的Health监测的api控制器
                    Interval = TimeSpan.FromSeconds(12),//间隔12秒一次
                    HTTP = $"http://{ip}:{port}/API/Health/Index",
                    Timeout = TimeSpan.FromSeconds(52),//检测等待时间
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(60)//失败多久后移除,最小值60秒
                }
            }).Wait();
        }
    }

在Startup.cs的Configure中加上            ConsulHelper.Init(Configuration);这句话就行了

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }

            ConsulHelper.Init(Configuration);
            app.UseMvc();
        }

此时我们将程序运行起来,可以看到有个test的服务了,这就说明Consul发现了我们刚启动的服务了,因为我们刚才配置中写了Name="test"

B.配置文件的方式

通过上面代码的方式,其实配置文件的方式应该更为简单方便,毕竟不用写代码了,只需修改下配置文件就行了。

在Consul目录下创建配置文件consul.json

{
  "service": {
    "id": "consul-service-test",
    "name": "test",
    "tags": [ "test" ],
    "address": "127.0.0.1",
    "port": 5000,
    "checks": [
      {
        "http": "http://127.0.0.1:5000/api/Health/Index",
        "interval": "10s"
      }
    ]
  }
}

不过启动的命令得修改了,后面需加上配置文件的目录地址

consul agent -dev -config-dir=D:\Soft\consul_1.8.5_windows_amd64

这样webapi项目里其实上面都不用修改,只需要有健康监测的api就行了。Consul启动后就会自动取发现此webapi的服务了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Consul是一个开源工具,用于实现分布式系统的服务发现和配置管理。它由HashiCorp公司开发,使用Go语言编写,具有绿色、轻量级的特点。\[3\] 在使用Consul时,可以使用Consul-template来实现配置模板和Nginx配置的更新。首先,需要安装并运行Consul。可以从官网下载最新版本的Consul服务,并解压到指定目录。然后,将解压后的Consul可执行文件移动到/usr/local/bin目录下。可以使用consul --version命令来验证安装是否成功。\[1\] 接下来,可以启动Consul服务。使用consul agent命令来启动服务,并指定相关参数,如-server、-ui、-bootstrap-expect、-data-dir、-node、-client、-bind、-datacenter和-config-dir等。这些参数可以根据实际需求进行配置。\[2\] 一旦Consul服务启动成功,就可以开始使用Consul进行服务发现和配置管理了。Consul提供了一套API和命令行工具,可以用于注册和发现服务、配置管理等操作。可以通过编写Consul配置文件来定义服务和相关配置信息,并使用Consul-template来生成实际的Nginx配置文件。Consul-template使用HTTP长轮询来实现变更触发和配置更改,可以通过Consul的watch命令来实现。\[1\] 总结来说,Consul是一个分布式、高可用、可横向扩展的工具,用于实现分布式系统的服务发现和配置管理。通过安装和运行Consul,并结合Consul-template来实现Nginx配置的更新和重启功能。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Consul的介绍、安装与使用](https://blog.csdn.net/hudeyong926/article/details/121287659)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [consul配置和使用](https://blog.csdn.net/weixin_44105468/article/details/121450170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值