序言
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。Spring Cloud Ribbon虽然只是一个工具类框架,它不像服务注册中心、配置中心、API网关那样需要独立部署,但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。因为微服务间的调用,API网关的请求转发等内容,实际上都是通过Ribbon来实现的,包括后续我们将要介绍的Feign,它也是基于Ribbon实现的工具。所以,对Spring Cloud Ribbon的理解和使用,对于我们使用Spring Cloud来构建微服务非常重要。
此文章仅限入门 SpringCloud版本为 Greenwich
由于 Netflix 的一贯跳票 Spring 正在开发自己的软负载Spring Cloud Loadbalancer
不过目前在孵化中,使用起来和Ribbon区别并不是很大。
使用
如果你使用的是 Eureka 那么在 spring-cloud-starter-netflix-eureka-client
之中其实已经包含了 spring-cloud-starter-netflix-ribbon
如果是其他注册中心,则需要依赖spring-cloud-starter-netflix-ribbon
。
这里就拿Eureka 的一个Client 举例
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
然后设置一下 yml
server:
port: 7001
spring:
application:
name: spring-cloud-action-server-order
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
然后是主函数
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Configuration
static class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
}
然后在Controller中这样操作 注意啊 在 Ribbon调用的时候服务名一定要大写
@RestController
public class ATestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping
public String getOrder() {
List<String> services = discoveryClient.getServices();
services.forEach(this::accept);
return restTemplate.getForObject("http://SPRING-CLOUD-ACTION-SERVER-PRODUCT/getProducts", String.class);
}
private void accept(String service) {
List<ServiceInstance> list = discoveryClient.getInstances(service);
for (ServiceInstance instance : list) {
System.out.println(instance.getUri() + "/" + service + " - " + instance.getServiceId());
}
}
}
然后在启动这个client 在调用其他注册中心的服务的时候就能达到一个负载均衡的效果。
在这里呢我们注入了一个 DiscoveryClient
的类, 这个类呢能用 注册中心中拉取信息,这里就叫做注册表吧。我们请求一下这个接口可以看到 控制台上有如下打印。
http://192.168.56.1:7002/spring-cloud-action-server-product - SPRING-CLOUD-ACTION-SERVER-PRODUCT
http://192.168.56.1:7003/spring-cloud-action-server-product - SPRING-CLOUD-ACTION-SERVER-PRODUCT
http://192.168.56.1:7001/spring-cloud-action-server-order - SPRING-CLOUD-ACTION-SERVER-ORDER
对其实就是他真是的ip和端口,其实理解起来不难,所以呢在restTemplate
调用的时候 通过 serviceId
找到对应的客户端 然后根据客户端配置的负载算法从对应集合中找出符合当前算法条件的结果,最后拼接出相应的 http 地址即可。
负载方式
Ribbon默认的负载方式是轮训,在Ribbon默认已经有多重负载的方式
package com.netflix.loadbalancer;
public interface IRule {
Server choose(Object var1);
void setLoadBalancer(ILoadBalancer var1);
ILoadBalancer getLoadBalancer();
}
只需要在Ribbon中IRule
接口的子类并把其注册为子类注册为bean
启动类上添加注解@RibbonClient(name="微服务名", configuration="装载自定义负载均衡算法的配置类")
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "spring-cloud-action-server-product", configuration = ATestApplication.MySelfRuleConfig.class)
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
@Configuration
static class MySelfRuleConfig {
@Bean
IRule rule() {
return new RandomRule();
}
}
}
如果自带的这些负载规则不满足你的口味,那么道理相同,自己实现IRule
接口定义自己的规则即可。
此文章仅限入门,切记啊不会用找不到多去官网看文档!