Tiven Wang
Wang Tiven August 30, 2017
425 favorite favorites
bookmark bookmark
share share

Load Balancing Concepts

在微服务架构中每个服务都可能有多个运行实例, 那么服务消费者在调用服务时怎么定位应该把请求发送给哪个实例呐? 这就是负载均衡 Load Balancing 要做的事情。Load Balancing 可以在服务端也可以是在消费端。

Client-side

在客户端做负载均衡是指在客户端配置一个服务地址的列表,服务地址可以是IP也可以是域名URI,然后客户端使用某种算法为请求选择服务的某个地址,这样就可以达到负载均衡的效果。服务的IP或者URI列表可以通过Hardcode的方式配置在客户端,也可以使用Service Discovery的方式获取。

例如 Netflix 提供的 Client Side Load Balancer: Ribbon.

Ribbon is a Inter Process Communication (remote procedure calls) library with built in software load balancers. The primary usage model involves REST calls with various serialization scheme support.

Server-side

服务端的负载均衡是在服务端进行请求分发的方式,常见的方式有Round-robin DNS,更多的其他方式是跟具体产品或者平台有关,如NGINX. 由 Load Balancing 发展成更高级一点的概念是 application delivery controller ADC,增加了一些高级属性。

例如,CloudFoundry 平台就支持把同一URI请求路由到不同的 App instances 上去,Cloud Foundry Components Router

Session Affinity

一个重要的问题是,当同一用户的不同请求被分发到不同的服务实例上时服务实例如何共享用户的session数据。

  • 第一种方式,如果用户session存储在单个服务实例本地local的话,把同一用户的请求转发到同一实例上去,需要Load Balancer支持,用户信息可以存储在浏览器cookie里。
  • 第二种方式,把用户session数据存储在共享的单独的缓存服务上,每个服务实例都从缓存服务上读取和存储用户session数据。

Client Side Load Balancer Ribbon

可以单独引入ribbon的依赖包

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

如果使用了 Eureka Service Discovery 的话,使用@FeignClient或者@LoadBalanced @Bean RestTemplate;

Feign already uses Ribbon, so if you are using @FeignClient then the ribbon client configurations also applies.

如果没有使用 Eureka Service Discovery 的话,就要配置@RibbonClient;

@RibbonClient(name = "some-service", configuration = SomeServiceConfig.class)
public class TestConfiguration {
}

显式配置 ribbon client:

some-service:
  ribbon:
    eureka:
      enabled: false
    listOfServers: http://localhost:5000, http://localhost:5001

Difference between @RibbonClient and @LoadBalanced

References

Similar Posts

  • Try Cloud Foundry 13 - Logs Loggregator is the next generation system for aggregating and streaming logs and metrics from all of the user apps and system components in an Elastic Runtime deployment. Loggregator uses Google's protocol buffers along with gRPC to deliver logs.
  • Try Cloud Foundry 12 - Config Server Config Server for Pivotal Cloud Foundry (PCF) is an externalized application configuration service, which gives you a central place to manage an application’s external properties across all environments.
  • Microservices - API Gateway Implement an API gateway that is the single entry point for all clients. The API gateway handles requests in one of two ways. Some requests are simply proxied/routed to the appropriate service. It handles other requests by fanning out to multiple services.
  • Microservices - Circuit Breaker 访问远程服务时, 比依赖超时时间更好一些的方式是一种叫断路器(Circuit Breaker)的模式. Circuit Breaker 就像一位交通警察, 在前方道路畅通的情况下, 他会放行; 当前方道路由于各种原因拥堵时, 他会告诉你前方道路不通请回; 如果他是个更智能的交警的话, 还会告诉你前方道路部分拥堵, 只允许部分车辆通过, 比如实行单双号.
  • Redis Redis
  • Microservices - Architecture Microservice architecture (MSA) is an approach to building software systems that decomposes business domain models into smaller, consistent, bounded-contexts implemented by services. These services are isolated and autonomous yet communicate to provide some piece of business functionality. Microservices are typically implemented and operated by small teams with enough autonomy that each team and service can change its internal implementation details (including replacing it outright!) with minimal impact across the rest of the system.

Comments

comments powered by Disqus
Back to Top