RPC load balancing strategy

Taking the time to write a simple version of a frame rpc, thought how do load balancing, the easiest way is to put forward a configuration file placement service address directly read the configuration file, thoughts turned to the configuration file can be put zk, zk with equivalent do the distribution center or service discovery. Excellent dubbo project can do so immediately refer the case Google's grpc, Google found a great article, I read the next (also borrowed from Google images of this article), it is good to write some of my own insights.

rpc communication itself is not complicated, as long as the agreement will be a good how to deal with a big problem, but load balancing strategy is worthy of scrutiny.

In general, load balancing strategy following two

1. Proxy Service

The client does not know the existence of the server side, it hit all requests proxy service, the proxy service to distribute to the server, and a fair load algorithm. Clients may not be trusted, this user-oriented services by the user, similar to our nginx distribute requests to the back-end machines.

Cons: The client does not know the existence of the back-end, and the client can not be trusted, the delay will be higher throughput and agency services will affect the service itself

Advantages: in the intermediate layer do interception monitoring special bar.

Figure: 

2. The client load balancing

The client knows there are multiple back-end services, from the client to choose the server and the client can be concluded from the back-end servers themselves out of a load of information, load balancing algorithm. The easiest way to achieve this is to say what I have put forward a configuration file directly, or random time polling service call to end.

Figure: 

advantage:

High-performance, due to the elimination of third-party interaction

Disadvantages:

The client will be very complicated, because the client wants to track the health of server load and client load balancing algorithm. And maintenance burden multi-language too much, and the client needs to be trusted, was a reliable client.

These are the introduction of two load balancing schemes, say the following is a detailed program uses several proxy mode load balancing

Load balancing proxy There are many ways

Load balancing can be a proxy  L3/L4(传输级别) or  L7(应用程序级别).

In  L3/L4 the server and terminate the TCP connection open to the rear end of another connection selected.

L7 Only the client to connect to engage in a service end to end connections between applications to do middleman.

L3/L4 级别的负载均衡按设计只做很少的处理,与L7级别的负载均衡相比的延迟会更少,而且更便宜,因为它消耗更少的资源。

在L7(应用程序级)负载平衡中,负载均衡服务终止并解析协议。负载均衡服务可以检查每个请求并根据请求内容分配后端。这就意味监控拦截等操作可以非常方便快捷的做在这里。

L3/L4 vs L7

正确的打开方式有一下几种

  1. 这些连接之间的RPC负载变化很大: 建议使用L7.
  2. 存储或计算相关性很重要 :建议使用L7,并使用cookie或类似的路由请求来纠正服务端.
  3. 设备资源少(缺钱): 建议使用 L3/L4.
  4. 对延迟要求很严格(就是要快): L3/L4.

下面要说的就是客户端实现负载均衡方式的详细方案:

  public class Test
  
  {
  
  public class InnerClass
  
  {
  
  public int getSum(int x,int y)
  
  {
  
  return x+y;
  
  }
  
  }
  
  public static void main(String[] args)
  
  {
  
  Test.InnerClass testInner =new Test().new InnerClass();
  
  int i = testInner.getSum(2/3);
  
  System.out.println(i); //输出5
  
  }
  
  }
  
  实例内部类
  
  实例内部类是指没有用 static 修饰的内部类
  
  public class Outer
  
  {
  
  class Inner
  
  {
  
  //实例内部类
  
  }
  
  }
  
  在外部类的静态方法和外部类以外的其他类中,必须通过外部类的实例创建内部类的实例,如果有多层嵌套,则内部类可以访问所有外部类的成员
  
  public class Outer
  
  {
  
  class Inner{}
  
  Inner i=new Inner(); //类内部不需要创建外部类实例
  
  public void method0()
  
  {
  
  Inner j=new Inner(); //类内部不需要创建外部类实例
  
  }
  
  public static void method1()
  
  {
  
  Inner r=new Outer().new inner(); //静态方法需要创建外部类实例
  
  }
  
  class Inner1
  
  {
  
  Inner k=new Inner(); //不需要创建外部类实例
  
  }
  
  }
  
  class OtherClass
  
  {
  
  Outer.Inner i=new Outer(yongshi123.cn).new Inner(); //其他类使用时需要创建外部类实例
  
  }
  
  静态内部类
  
  静态内部类是指使用 static 修饰的内部类
  
  public class Outer
  
  {
  
  static class Inner
  
  {
  
  //静态内部类
  
  }
  
  }
  
  在创建静态内部类的实例时,不需要创建外部类的实例
  
  public class Outer
  
  {
  
  static class Inner{}
  
  }
  
  class OtherClass
  
  {
  
  Outer.Inner oi=new Outer.Inner();
  
  }
  
  局部内部类
  
  局部内部类是指在一个方法中定义的内部类
  
  public class Test
  
  {
  
  public void method()
  
  {
  
  class Inner
  
  {
  
  //局部内部类
  
  }
  
  }
  
  }
  
  局部内部类与局部变量一样,不能使用访问控制修饰符(public、private 和 protected)和 static 修饰符修饰,局部内部类只在当前方法中有效,局部内部类中可以访问外部类的所有成员
  
  public class Test
  
  {
  
  Inner i=new Inner(www.hnxinhe.cn); //编译出错
  
  Test.Inner ti=new Test.Inner(); //编译出错
  
  Test.Inner ti2=new Test().new Inner(); //编译出错
  
  public void method()
  
  {
  
  class Inner{}
  
  Inner i=new Inner();
  
  }
  
  }
  
  匿名类
  
  匿名类是指没有类名的内部类,必须在创建时使用 new 语句来声明类
  
  new<类或接口>()
  
  {
  
  //类的主体
  
  };
  
  这种形式的 new 语句声明一个新的匿名类,它对一个给定的类进行扩展,或者实现一个给定的接口。使用匿名类可使代码更加简洁、紧凑,模块化程度更高
  
  匿名类有两种实现方式:
  
  继承一个类,重写其方法。
  
  实现一个接口(可以是多个),实现其方法。
  
  public class Out
  
  {
  
  void show()
  
  {
  
  System.out.println("调用 Out 类的 show() 方法");
  
  }
  
  }
  
  public class TestAnonymousInterClass
  
  {
  
  //在这个方法中构造一个匿名内部类
  
  private void show(www.xgjrfwsc.cn)
  
  {
  
  Out anonyInter=new Out()
  
  {
  
  //获取匿名内部类的实例
  
  void show()
  
  {
  
  System.out.println("调用匿名类中的 show() 方法");
  
  }
  
  };
  
  anonyInter.show();
  
  }
  
  public static void main(String[] args)
  
  {
  
  TestAnonymousInterClass test=new TestAnonymousInterClass();
  
  test.show();
  
  }

1. 笨重的客户端

这就意味着客户端中实现负载平衡策略,客户端负责跟踪可用的服务器以及用于选择服务器的算法。 客户端通常集成与其他基础设施(如服务发现、名称解析、配额管理等)通信的库,这就很复杂庞大了。

2. Lookaside 负载均衡 (旁观?)

旁观式负载平衡也称为外部负载平衡,使用后备负载平衡,负载平衡的各种功能智能的在一个单独的特殊的负载均衡服务中实现。客户端只需要查询这个旁观式的负载均衡服务, 这个服务就能给你最佳服务器的信息,然后你拿这个数据去请求那个服务端。 就像我一开说的比如把服务端的信息注册到zk,由zk去做负载均衡的事情,客户端只需要去zk取服务端数据,拿到了服务端地址后,直接向服务端请求。

如图: 

以上说了这么多,到底服务间的负载均衡应该用哪个,总结以下几点:

  1. 客户端和服务器之间非常高的流量,且客户端是可信的,建议使用‘笨重’的客户端 或者 Lookaside 负载均衡

  2. 传统设置——许多客户端连接到代理背后的大量服务,需要服务端和客户端之间有一个桥梁,建议使用代理式的负载均衡

  3. 微服务- N个客户端,数据中心有M个服务端,非常高的性能要求(低延迟,高流量),客户端可以不受信任,建议使用 Lookaside 负载均衡

 

Guess you like

Origin www.cnblogs.com/qwangxiao/p/10995948.html