服务注册概要

本贴最后更新于 1863 天前,其中的信息可能已经斗转星移

服务如何集成到以 Ocelot 为中心的微服体系


[TOC]

Consul 简介:

Consul,为你的基础设施提供服务发现和服务配置的工具

  • 服务发现 Consul 的客户端可用提供一个服务,比如 api 或者 mysql ,另外一些客户端可用使用 Consul 去发现一个指定服务的提供者.通过 DNS 或者 HTTP 应用程序可用很容易的找到他所依赖的服务
  • 健康检查 Consul 客户端可用提供任意数量的健康检查,指定一个服务(比如:webserver 是否返回了 200 OK 状态码)或者使用本地节点(比如:内存使用是否大于 90%)

基本命令(Consul 启动命令):

  • 生成环境命令

consul agent -server -bootstrap-expect 1 -data-dir ./dataCenter -node=s1 -bind=192.168.1.50 -ui -rejoin -client 0.0.0.0

  • 开发时命令

consul agent --dev

服务分布式注册:

  • 在 Startup 类的 Configure 方法加入以下代码片段

  • 服务启动时注册:

    string ip = Configuration.GetSection("AppSettings:ip").Value;
            string port = Configuration.GetSection("AppSettings:port").Value;
            string serviceName = "ProjectService";
            string serviceId = serviceName + $":{port}";//端口号为在服务集群中区别服务ID

            using (var consulClient = new ConsulClient(ConsulConfig))
            {
                AgentServiceRegistration asr = new AgentServiceRegistration
                {
                    Address = ip,
                    Port = Convert.ToInt32(port),
                    ID = serviceId,
                    Name = serviceName,
                    Check = new AgentServiceCheck
                    {
                        DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
                        HTTP = $"http://{ip}:{port}/v1/api/Health/Get",
                        Interval = TimeSpan.FromSeconds(10),
                        Timeout = TimeSpan.FromSeconds(5),
                    },
                };
                consulClient.Agent.ServiceRegister(asr).Wait();
            }

细节说明

  • DeregisterCriticalServiceAfter

表示 Consul 做健康服务检查失败 xx 时间后 Consul 会撤销该服务

  • Interval

表示每 xx 时间做一次检查

  • Timeout

服务健康检查超时时间

  • HTTP

健康检查接口

服务停止时撤销:

    //注销Consul
            appLifeTime.ApplicationStopped.Register(() => {
                using (var consulClient = new ConsulClient(ConsulConfig)) {
                    Console.WriteLine("应用退出,开始从consul注销");
                    consulClient.Agent.ServiceDeregister(serviceId).Wait();
                }
            });

Consul 注册成功时,管理界面呈现该服务为绿标:

20181017104925

Ocelot 如何发现服务:

由于服务注册到 consul,Ocelot 通过集成 Consul 的服务发现,获取该服务的所有节点,如以下配置:

// chy.project --> service part
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "ServiceName": "ProjectService",
      //"DownstreamHostAndPorts": [
      //  {
      //    "Host": "localhost",
      //    "Port": 44338
      //  }
      //],
      "UseServiceDiscovery": true,

      "UpstreamPathTemplate": "/chy.project/{url}",
      "UpstreamHttpMethod": [ "Get", "Post", "Delete", "Put" ],

      "ReRouteIsCaseSensitive": false,

      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10,
        "TimeoutValue": 10000
      }
    }

细节说明

  • UseServiceDiscovery

开启服务发现,前提是 GlobalConfiguration 设置服务发现,如下:

    "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
  • ServiceName

指定注册到 Consul 的服务名称

Ocelot 如何热更新配置:

Ocelot 提供以下接口用于更新配置:

  • 获取配置: http://localhost:6800/admin/configuration 获取到的配置就是这样滴

20181017103608

  • 更新配置: http://localhost:6800/admin/configuration 使用 Post 提交更新的配置
    20181017104446

-- 到这里服务已集成到微服体系当中

  • 服务

    提供一个服务绝不仅仅是简单的把硬件和软件累加在一起,它包括了服务的可靠性、服务的标准化、以及对服务的监控、维护、技术支持等。

    41 引用 • 24 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...