# Spring Cloud构建微服务架构（二）服务消费者

Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides client-side load balancing algorithms.

Apart from the client-side load balancing algorithms, Ribbon provides also other features:

Service Discovery Integration – Ribbon load balancers provide service discovery in dynamic environments like a cloud. Integration with Eureka and Netflix service discovery component is included in the ribbon library
Fault Tolerance – the Ribbon API can dynamically determine whether the servers are up and running in a live environment and can detect those servers that are down
Configurable load-balancing rules – Ribbon supports RoundRobinRule, AvailabilityFilteringRule, WeightedResponseTimeRule out of the box and also supports defining custom rules
Ribbon API works based on the concept called “Named Client”. While configuring Ribbon in our application configuration file we provide a name for the list of servers included for the load balancing.

Let’s take it for a spin.

Ribbon Configuration

Ribbon API enables us to configure the following components of the load balancer:

Rule – Logic component which specifies the load balancing rule we are using in our application
Ping – A Component which specifies the mechanism we use to determine the server’s availability in real-time
ServerList – can be dynamic or static. In our case, we are using a static list of servers and hence we are defining them in the application configuration file directly

public class RibbonConfiguration {

@Autowired
IClientConfig ribbonClientConfig;

@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}

@Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}

Notice how we used the WeightedResponseTimeRule rule to determine the server and PingUrl mechanism to determine the server’s availability in real-time.

According to this rule, each server is given a weight according to its average response time, lesser the response time gives lesser the weight. This rule randomly selects a server where the possibility is determined by server’s weight.

And the PingUrl will ping every URL to determine the server’s availability.

http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon

## Ribbon

Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon，后续会介绍Feign的使用。

Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

#### 准备工作

• 启动Chapter-9-1-1中的服务注册中心：eureka-server
• 启动Chapter-9-1-1中的服务提供方：compute-service
• 修改compute-service中的server-port为2223，再启动一个服务提供方：compute-service

http://blog.didispace.com/springcloud2/

+ 订阅