.NET Core微服务之路:基于Consul最少集群实现服务的注册与发现(二)

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: 重温Consul最少化集群的搭建     我们再复习一下上一篇的内容,先建立三台consul server节点,两个consul client节点,分别在每个节点上跑不同(名称不同而已)的实例。

重温Consul最少化集群的搭建

 
  我们再复习一下上一篇的内容,先建立三台consul server节点,两个consul client节点,分别在每个节点上跑不同(名称不同而已)的实例。我们先通过vmware启动这五个节点,并且能成功访问这个两个client节点的实例。(具体配置可以见上一篇)
 
  通过配置文件自动生成服务
{
  "services": [
    {
      "id": "CLIENT_SERVICE_01",
      "name": "CAS Client Service",
      "tags": [
        "urlprefix-/ClientService01"
      ],
      "address": "192.168.153.132",
      "port": 5000,
      "checks": [
        {
          "name": "clientservice_check01",
          "http": "http://192.168.53.132:5000/api/health",
          "interval": "10s",
          "timeout": "5s"
        }
      ]
    },
    {
      "id": "CLIENT_SERVICE_02",
      "name": "CAS Client Service",
      "tags": [
        "urlprefix-/ClientService02"
      ],
      "address": "192.168.153.132",
      "port": 5001,
      "checks": [
        {
          "name": "clientservice_check02",
          "http": "http://192.168.153.132:5001/api/health",
          "interval": "10s",
          "timeout": "5s"
        }
      ]
    }
  ]
}

 

添加KEY/VALUE  

curl -X PUT -d 'edisonchou' http://192.168.80.100:8500/v1/kv/web/vhallaccount
  我们通过 http://192.168.153.129:8500/v1/kv/?recurse来查看这个值是否添加到Consul中
   再验证是否已经同步到其他Consul服务端,先看129,注意加粗字体
[root@localhost ~]# ./consul kv get web/vhallaccount
stevelee
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:0c:29:a6:a1:1c brd ff:ff:ff:ff:ff:ff
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a6:a1:26 brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.129/24 brd 192.168.153.255 scope global noprefixroute ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::6d21:aa51:2262:b80f/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

   再看130的Consul服务端

[root@localhost ~]# ./consul kv get web/vhallaccount
stevelee
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:2c:c2:fc brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.130/24 brd 192.168.153.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::6114:ed9c:c49d:649b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:2c:c2:06 brd ff:ff:ff:ff:ff:ff
    inet 192.168.153.170/24 brd 192.168.153.255 scope global noprefixroute dynamic ens34
       valid_lft 1216sec preferred_lft 1216sec
    inet6 fe80::a67c:7966:8767:27fb/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

 

   我们也可以通过Consul WEB UI来编辑KV值。

 

 Consul服务的Watch机制

   熔断保护在Consul和Ocelot中都有实现,意思就是当一个服务不正常时(比如我们的一个服务实例挂了,Consul的健康检查机制检测到了),应该给系统维护人员给以告警。在Consul中,服务告警也是通过配置文件来实现的。

{
  "watches": [
    {
      "type": "checks",
      "handler_type": "http",
      "state": "critical",
      "http_handler_config": {
        "path": "http://192.168.153.132:9000/notice",
        "method": "POST",
        "timeout": "10s",
        "header": { "Authorization": [ "token" ] }
      }
    }
  ]
}

  我们再新建一个项目,建立一个控制器,用默认的HomController控制器也可以,键入如下代码:

[Produces("application/json")]
public class HomeController : Controller
{
    [HttpPost]
    [Route("/notice")]
    public IActionResult Notice()
    {
        var stream = HttpContext.Request.Body;
        if (HttpContext.Request.ContentLength != null)
        {
            var buffer = new byte[HttpContext.Request.ContentLength.Value];
            stream.Read(buffer, 0, buffer.Length);
            var content = Encoding.UTF8.GetString(buffer);

            var path = $"{AppDomain.CurrentDomain.BaseDirectory}{DateTime.Now:hh_mm_ss_ffff}.log";
            if (!System.IO.File.Exists(path))
            {
                System.IO.File.Create(path).Close();
            }

            using (var sw = new StreamWriter(path))
            {
                sw.Write(content);
                sw.Flush();
                sw.Close();
            }

            return Ok();
        }

        throw new Exception("post is null");
    }
}
  功能很简单,做一个接受消息的客户端,POST方法,代码不解释。
  当我们把5000端口上的服务停掉,会出现什么样的情况呢?132Consul的客户端服务器就会出现:5000/api/health无法访问的问题。
2018/10/17 04:03:10 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused
2018/10/17 04:03:10 [INFO] agent: Synced check "service:CLIENT_SERVICE_01"
2018/10/17 04:03:20 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused
2018/10/17 04:03:30 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused
2018/10/17 04:03:40 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused
2018/10/17 04:03:50 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused
2018/10/17 04:04:00 [WARN] agent: Check "service:CLIENT_SERVICE_01" HTTP request failed: Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused

  Watch机制会每隔10s向http://192.168.153.132:9000/notice发送一组json格式的消息,内容如下

[{
    "Node": "LZZ.DEV.WebServer",
    "CheckID": "service:CLIENT_SERVICE_01",
    "Name": "clientservice_check01",
    "Status": "critical",
    "Notes": "",
    "Output": "Get http://192.168.153.132:5000/api/health: dial tcp 192.168.153.132:5000: connect: connection refused",
    "ServiceID": "CLIENT_SERVICE_01",
    "ServiceName": "CAS Client Service",
    "ServiceTags": ["urlprefix-/ClientService01"],
    "Definition": {
        "HTTP": "",
        "Header": null,
        "Method": "",
        "TLSSkipVerify": false,
        "TCP": "",
        "Interval": "0s",
        "Timeout": "0s",
        "DeregisterCriticalServiceAfter": "0s"
    }
}]
  你也可以将上面的notice方法替换为其他消息通知的方法,比如邮件,比如短信,我更推荐用短信模式,这样能马上告诉运维人员,有节点挂了,该去加班了。
  综合之前上一篇的介绍,目前看Consul的Web控制台,已经出现了10个微服务了(不包括系统)!

 

 

相关文章
|
2月前
|
安全 Java 网络安全
Android远程连接和登录FTPS服务代码(commons.net库)
Android远程连接和登录FTPS服务代码(commons.net库)
28 1
|
2月前
|
开发框架 .NET API
Windows Forms应用程序中集成一个ASP.NET API服务
Windows Forms应用程序中集成一个ASP.NET API服务
97 9
|
4月前
|
存储 NoSQL Redis
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
|
4月前
|
敏捷开发 设计模式 开发者
【揭秘终极利器】AgileEAS.NET:服务定位器模式的魔法,如何让企业级软件开发瞬间提速?揭秘背后的技术奥秘与实战指南!
【8月更文挑战第16天】AgileEAS.NET是基于DotNet的企业级敏捷开发平台,其服务定位器模式助力构建高度解耦系统。通过全局服务目录动态查找服务,避免硬编码依赖。在AgileEAS.NET中,服务定位器以静态类形式封装服务注册与检索功能。示例展示了如何注册与获取服务实例,如在`UserController`中通过服务定位器使用`IUserService`。此模式整合到框架生命周期管理,便于各处获取服务实例,提升开发效率。然而,应适度使用并考虑依赖注入容器以增强代码可维护性和可测试性。
76 4
|
4月前
|
安全 Java 网络安全
Android远程连接和登录FTPS服务代码(commons.net库)
很多文章都介绍了FTPClient如何连接ftp服务器,但却很少有人说如何连接一台开了SSL认证的ftp服务器,现在代码来了。
105 2
|
4月前
|
开发框架 .NET 编译器
【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
|
4月前
|
开发框架 .NET API
.Net Core Console 项目如何使用 HttpClient 与 Web 服务通信
.Net Core Console 项目如何使用 HttpClient 与 Web 服务通信
|
存储 安全 Go
Golang 语言微服务的服务注册与发现组件 Consul
Golang 语言微服务的服务注册与发现组件 Consul
124 0
|
7月前
|
存储 网络协议 数据中心
服务注册中心Consul
服务注册中心Consul
81 1
|
7月前
Consul安装教程和注册
Consul安装教程和注册
79 0