NetCore 使用 Consul注册服务

第一步:下载Consul.exe

  • windows 64位

第二步:配置环境变量

  • 配置环境变量
  • 查看版本

第三步:启动服务

  • 启动方式一:持久化服务
  • 启动方式二:开机启动服务
  • 启动方式三:手动启动服务

第四步:使用Consul+Ocelot配置微服务


 

第一步:下载Consul.exe

1)下载Consul.exe,官网地址:https://developer.hashicorp.com/consul/install#windows

 

2)下载并解压到d盘根目录,如下图:

 

第二步:配置环境变量

1)此电脑 “右键” =》属性 =》高级系统设置,如下图:

 2)配置环境变量,如下图:

 4)使用CMD查看版本:consul -v

 

第三步:启动Consul服务

方式一:持久化服务

1)编写脚本,以管理员身份运行

1
2
3
4
5
6
7
8
9
@echo off
@echo 服务启动...
@set "consulPath=D:\consul_1.17.0_windows_amd64\consul.exe"
@set "dataDir=D:\consul_1.17.0_windows_amd64\data"
@sc create Consul binpath= "%consulPath% agent -server -ui -bind=127.0.0.1 -node=127.0.0.1 -client=0.0.0.0 -bootstrap-expect 1 -data-dir %dataDir%"
@net start Consul
@sc config Consul start= AUTO
@echo Consul start is OK... success
@pause

  2)此时window服务出现,consul服务

 

 

3)检查Consul服务是否正常使用:在浏览器中输入http://127.0.0.1:8500,如下:

 

 

方式二:开机启动Consul服务

1)编写Bat脚本,consul.bat,代码如下:

1
2
3
@echo off
color F
consul agent -dev

 

2)进入 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 文件夹,同时复制consul.exe 和 consul.bat 到此文件夹,然后重启启动电动。

 

 

 3)检查Consul服务是否正常使用:在浏览器中输入http://127.0.0.1:8500,如下:


方式三:手动启动Consul服务

1)进入consul.exe所在的地址栏,输入cmd

 2)进入cmd后,粘贴脚本代码,效果如下:

1
consul agent -server -ui -bootstrap-expect=1 -data-dir=D:\consul_1.17.0_windows_amd64 -node=127.0.0.1 -client=0.0.0.0 -bind=127.0.0.1 -datacenter=dc1 -join 127.0.0.1

 

 3)检查Consul服务是否正常使用:在浏览器中输入http://127.0.0.1:8500,如下:

 

第四步:使用Consul+Ocelot配置微服务

1)通过NuGet,安装 Ocelot.Provider.Consul 包

 

2)配置Consul(appsettings.json)

1
2
3
4
5
6
"Consul": {
    "ServiceName": "cms", //当前服务名称
    "IP": "127.0.0.1", //当前服务IP
    "Weight": "1",
    "Port": "5017" //当前服务端口
  }


3)Consul扩展类(ConsulBuilderExtensions.cs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static class ConsulExtensions
{
    public static void ConsulExtend(this IConfiguration configuration, string serviceName)
    {
        ConsulClient client = new(m =>
        {
            m.Address = new Uri("http://127.0.0.1:8500/");//对应服务器的地址:consul的端口
            m.Datacenter = "dc1";
        });
 
        //启动的时候在consul中注册实例服务
        //在consul中注册的ip, port
        string ServiceName = configuration.GetSection("Consul")["ServiceName"];
        string ip = configuration.GetSection("Consul")["IP"];
        int port = int.Parse(configuration.GetSection("Consul")["Port"]);
        int weight = int.Parse(configuration.GetSection("Consul")["Weight"]);
        client.Agent.ServiceRegister(new AgentServiceRegistration()
        {
            ID = $"{configuration["ServiceName"]}-{Guid.NewGuid()}",//唯一的
            Name = serviceName,//组(服务)名称(动态)
            Address = ip,
            Port = port,//不同的端口=>不同的实例
            Tags = new string[] { weight.ToString() },//标签
            Check = new AgentServiceCheck()//服务健康检查
            {
                Interval = TimeSpan.FromSeconds(12),//间隔1s一次 检查
                HTTP = $"http://{ip}:{port}/api/health/check",
                Timeout = TimeSpan.FromSeconds(5),//检测等待时间
                DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(20)//失败后多久移除
            }
        });
        Console.WriteLine($"{ip}:{port}--weight:{weight}");
    }
}

 

4)Consul注册服务(Program.cs)

1
builder.Configuration.ConsulExtend(builder.Configuration.GetSection("Consul")["ServiceName"]);

 

5)创建心跳控制器(HealthController.cs)

1
2
3
4
5
6
7
8
9
10
[Route("api/[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
    [HttpGet("Check")]
    public ActionResult Check()
    {
        return Ok();
    }
}

注意:先运行注册的端口,如:http:127.0.0.1:5001/api/article/post,然后再通过公共端口5000调用,如:http:127.0.0.1:5001/cms/article/post;

posted @   microsoft-zhcn  阅读(207)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示