zoukankan      html  css  js  c++  java
  • gRPC Client的负载均衡器

    一、gRPC是什么?

    gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成 可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。

    二、为什么使用gRPC?

    有了 gRPC, 我们可以一次性的在一个 .proto 文件中定义服务并使用任何支持它的语言去实现客户端和服务器,反过来,它们可以在各种环境中,从Google的服务器到你自己的平板电脑- gRPC 帮你解决了不同语言间通信的复杂性以及环境的不同.使用 protocol buffers 还能获得其他好处,包括高效的序列号,简单的 IDL 以及容易进行接口更新。

    三、出现启动其中一个服务挂了、中间服务挂了、服务重新启动这三种情况,如何保证客户端的正常访问?

    1、haproxy

    缺点:每次连接都会创建一个channel,并发高的话资源消耗大,这样性能也会有问题

    2、客户端负载均衡器

    优点:单例,资源消耗小,性能比较好

    缺点:可能还存在点问题,代码有点乱

    3、zk、consul

    以后改进方案

    4、如果大家有更好的方案,欢迎大家拍砖,谢谢

    四、相关包的引用版本是多少?

    1、Grpc.Core.0.15.0

    2、Grpc.HealthCheck.0.15.0-beta

    3、Google.Protobuf.3.0.0-beta3

    五、核心代码如下:

    获取工作中的服务通道

    public static Channel GetWorkingChannel(string key)
        {
          Ensure.NotNullOrEmpty(key);
          CachedItem item = null;
          _cacheMap.TryGetValue(key, out item);
          Channel currentChosenChannel = null;
          if (_cacheMap.IsNotEmpty())
          {
            foreach (var channel in item.Channels.OrderBy(o => Guid.NewGuid()))
            {
              try
              {
                if (channel.State == ChannelState.Idle || channel.State == ChannelState.Ready)
                {
                  channel.ConnectAsync(DateTime.Now.AddMilliseconds(100)).Wait();
                  currentChosenChannel = channel;
                  break;
                }
              }
              catch
              {
                channel.ShutdownAsync().Wait();
              }
            };
          }
    
          if (currentChosenChannel == null) InitWorkingChannelsByKey(key);
          return currentChosenChannel;
        }
    

      

    进行健康检查

    private static bool CheckIfConnectionIsWorking(Channel serverChannel)
        {
          if (serverChannel != null)
          {
            try
            {
              var client = new Health.HealthClient(serverChannel);
              var response = client.Check(new HealthCheckRequest { Service = "HealthCheck" });
              return response.Status == HealthCheckResponse.Types.ServingStatus.Serving;
            }
            catch (Exception ex)
            {
              serverChannel.ShutdownAsync().Wait();
              return false;
            }
          }
          return false;
        }
    

      

    定时任务维护

     private static void ExecuteMaintenance(object state)
        {
          if (Interlocked.CompareExchange(ref _executing, 1, 0) != 0)
            return;
          try
          {
            if (_cacheMap.Count == 0)
            {
              StopMaintenance();
              if (_cacheMap.Count != 0)
                StartMaintenance();
            }
            else
            {
              DateTime oldThreshold = DateTime.UtcNow - _timeout;
              var expiredItems = _cacheMap.Where(i => i.Value.Updated < oldThreshold).Select(i => i.Key);
              for (int i = 0; i < expiredItems.Count(); i++)
              {
                var key = expiredItems.ElementAt(i);
                DisposeChannelsByKey(key);
                InitWorkingChannelsByKey(key);
              }
            }
          }
          finally
          {
            Interlocked.Exchange(ref _executing, 0);
          }
        }
    

      

    gRPC Client源代码已上传git,本人比较懒没有完整上传解决方案,该有的都有了。

  • 相关阅读:
    将不同服务器的一个表的数据复制到另一个表里面
    15丨基础篇:Linux内存是怎么工作的?
    13 | 答疑(一):无法模拟出 RES 中断的问题,怎么办?
    12 | 套路篇:CPU 性能优化的几个思路
    11 | 套路篇:如何迅速分析出系统CPU的瓶颈在哪里?
    10 | 案例篇:系统的软中断CPU使用率升高,我该怎么办?
    nginx error.log 提示 [error] 887#887: *58 FastCGI sent in stderr: "PHP message: PHP Warning: mysql_connect(): Headers and client library minor version mismatch. Headers:50556 Library:50637
    PHP查询数据库较慢,nginx 超时 返回 504:Sorry, the page you are looking for is currently unavailable.
    编码 ASCII, GBK, Unicode+utf-8
    python之re正则简单够用
  • 原文地址:https://www.cnblogs.com/hj4444/p/5793984.html
Copyright © 2011-2022 走看看