go to my github

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

重温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个微服务了(不包括系统)!

 

 

posted @ 2018-10-17 16:45 另一个老李 阅读(...) 评论(...) 编辑 收藏