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 来构建微服务非常重要。
负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
负载均衡在系统架构中是一个非常重要,并且是不得不去实施的内容。 因为负载均衡是对系统的高可用、 网络压力的缓解和处理能力扩容的重要手段之一。 我们通常所说的负载均衡都指的是服务端负载均衡, 其中分为硬件负载均衡和软件负载均衡。 硬件负载均衡主要通过在服务器节点之间安装专门用于负载均衡的设备,比如 F5 等;而软件负载均衡则是通过在服务器上安装一些具有均衡负载功能或模块的软件来完成请求分发工作, 比如Nginx 等。 不论采用硬件负载均衡还是软件负载均衡,只要是服务端负载均衡都能以类似下图的架构方式构建起来:
通过Spring Cloud Ribbon的封装, 我们在微服务架构中使用客户端负载均衡调用非常简单, 只需要如下两步:
在上一篇中,我们启动eurekaserver,端口5555,和helloserver端口:5556后,现在停止helloserver,并将helloserver的端口改为5557,再次启动。会发现在eurekaserver下有二个实例,这就相当于一个小集群了。访问:http://127.0.0.1:5555/
@EnableEurekaServer
@SpringBootApplication
@RestController
public class HelloserverApplication {
private final Logger log = (Logger) LoggerFactory.getLogger(HelloserverApplication.class);
@Autowired
private DiscoveryClient client;
@Value("${server.port}")
String port;
@RequestMapping(name = "/hello", method = RequestMethod.GET)
public String index() {
ServiceInstance instance = client.getLocalServiceInstance();
log.info("/hello, host:" + instance.getHost() + ",service_id:" + instance.getServiceId());
return "Hello SpringCloud~HOST:" + instance.getHost() + ",PORT:" + port + ",service_id:" + instance.getServiceId();
}
public static void main(String[] args) {
SpringApplication.run(HelloserverApplication.class, args);
}
}
(1)pom.xml(依赖spring-boot-starter-web、spring-cloud-starter-eureka、spring-cloud-starter-ribbon)
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-web
(2)application.yml
server:
port: 5560
eureka:
client:
serviceUrl:
defaultZone: http://localhost:5555/eureka/
spring:
application:
name: service-ribbon
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudribbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudribbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
(4)HelloService
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String helloService() {
return restTemplate.getForObject("http://HELLOSERVER/hello", String.class);
}
}
(5)HelloController
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String hello() {
return helloService.helloService();
}
}
(6)测试
访问:http://127.0.0.1:5560/hello。
刷新:
(7)增加helloserver的服务实例
命令:java -jar helloserver-0.0.1-SNAPSHOT.jar --server.port=5558
(8)再次刷新测试,访问:http://127.0.0.1:5560/hello。
(9)访问服务中心eurekaserver:http://127.0.0.1:5555/