Spring Cloud06: Ribbon 负载均衡
一、使用背景
前面的学习中,我们已经使用RestTemplate来实现了服务消费者对服务提供者的调用,如果在某个具体的业务场景下,对某个服务的调用量突然大幅提升,这个时候就需要对该服务实现负载均衡以满足对高并发的访问情况。在大型的分布式项目中,负载均衡是必备的,那么就可以采用Ribbon来实现。
二、什么是Ribbon
Ribbon是Sping Cloud的一个组件,Spring Cloud Ribbon是一个负载均衡的解决方案,Ribbon是Netflix发布的负载均衡器,Spring对其进行了集成,Spring Cloud Ribbon是基于Netflix Ribbon实现的,是一个用于对HTTP请求进行控制的负载均衡客户端。Spring Cloud Ribbon也是要结合Eureka Server来使用的,因为也要在注册中心进行注册。在注册中心对Ribbon进行注册之后,Ribbon就可以基于某种负载均衡算法,如轮询、随机、加权轮询、加权随机等自动帮助服务消费者调用接口,开发者也可以根据具体需求自定义Ribbon负载均衡算法。实际开发中,Spring Cloud Eureka来使用,Eureka Server提供所有可以调用的服务提供者列表,Ribbon基于特定的负载均衡算法从这些服务提供者中选择要调用的具体实例。示例图如下:
三、实战,快速搭建Ribbon实例
1.创建Module,pom.xml添加Eureka client依赖,将其注册到注册中心,添加代码如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
2.创建配置文件application.yml,配置信息如下:
server:
port: 8040
spring:
application:
name: ribbon
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
prefer-ip-address: true
属性说明
* spring.application.name : 当前服务注册在 Eureka Server 上的名称。
* eureka.client.service-url.defaultZone : 注册中⼼的访问地址。
* eureka.instance.prefer-ip-address : 是否将当前服务的 IP 注册到 Eureka Server。 3.创建启动类
3.创建启动类,代码如下:
package com.zing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
注解说明:
* @Bean:通过@Bean注解将实例注入到IOC容器中。实际上@SpringBootApplication 相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan的默认属性,而@Bean是一个方法级别的注解,主要用在@Configuration注解的类里,@Configuration相当于xml文件中的beans标签,@Bean相当于bean标签。
* @LoadBalanced:声明一个基于Ribbon的负载均衡。标注此注解后,RestTemplate就具有了客户端负载均衡的能力。
4.Controller层代码实现如下:
package com.zing.controller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.zing.entity.Student;
@RestController
@RequestMapping("/ribbon")
public class RibbonHandler {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/findAll")
public Collection<Student> findAll(){
return restTemplate.getForObject("http://provider/student/findAll", Collection.class);
}
@GetMapping("/index")
public String index() {
return restTemplate.getForObject("http://provider/student/index", String.class);
}
}
5.测试接口
(1) 启动注册中心,然后分别启动两个provider实例。打开注册中心可看到如下信息:
(2) 启动Ribbon,可看到注册中心有RIBBON的一个实例,如下:
(3) 测试index接口,可看到端口交替出现,结果如下:
四、总结
此次代码采用Ribbon+RestTemplate的方式来实现服务调用的负载均衡,客户端默认采用轮询的方式调用服务提供者,是两个provider实例交替进行响应。但是这种开发方式是可以被简化的,在实际的开发中有更加便捷的方式,可以同样实现这样一个功能,即Feign
Spring Cloud06: Ribbon 负载均衡的更多相关文章
- Spring Cloud Ribbon负载均衡
目录 一.简介 二.客户端负载均衡 三.RestTemplate详解 GET请求 POST请求 PUT请求 DELETE请求 一.简介 Spring Cloud Ribbon是一个基于HTTP 和 ...
- Spring Cloud Ribbon负载均衡(快速搭建)
Spring Cloud Ribbon 是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过 Spring Cloud 的封装, 可以让我们轻松地将面向服务的 ...
- SpringCloud微服务实战二:Spring Cloud Ribbon 负载均衡 + Spring Cloud Feign 声明式调用
1.Spring Cloud Ribbon的作用 Ribbon是Netflix开发的一个负载均衡组件,它在服务体系中起着重要作用,Pivotal将其整合成为Spring Cloud Ribbon,与其 ...
- Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b
环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 目录结构: 可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了E ...
- Spring Cloud微服务Ribbon负载均衡/Zuul网关使用
客户端负载均衡,当服务节点出现问题时进行调节或是在正常情况下进行 服务调度.所谓的负载均衡,就是当服务提供的数量和调用方对服务进行 取舍的调节问题,在spring cloud中是通过Ribbon来解决 ...
- spring cloud: 关闭ribbon负载均衡
spring cloud: 关闭ribbon负载均衡 1.eureka服务 2.2个user服务:7900/7901 3,movie服务 movie服务去请求 user的用户信息,而此时只想请求790 ...
- spring cloud学习笔记二 ribbon负载均衡
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...
- SpringCloud系列——Ribbon 负载均衡
前言 Ribbon是一个客户端负载均衡器,它提供了对HTTP和TCP客户端的行为的大量控制.我们在上篇(猛戳:SpringCloud系列——Feign 服务调用)已经实现了多个服务之间的Feign调用 ...
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...
随机推荐
- Windows核心编程 第八章 用户方式中线程的同步(下)
8.4 关键代码段 关键代码段是指一个小代码段,在代码能够执行前,它必须独占对某些共享资源的访问权.这是让若干行代码能够"以原子操作方式"来使用资源的一种方法.所谓原子操作方式,是 ...
- CentOS7 搭建 Redis 集群
一.手动搭建 1. 准备节点 节点数量至少为 6 个才能保证组成完整高可用的集群 (1) 目录结构 cluster ├── 9001 │ ├── data │ │ ├── appendon ...
- 剖析XAML语言
这节剖析一下XAML(读作:zaml)--这一WPF中的UI设计语言. XAML 在wpf中,UI部分使用xaml语言来编写,xaml语言是由xml语言派生而来的语言,所以在xaml中我们可以看到很多 ...
- C++逆向分析----多重继承和菱形继承
多重继承 多重继承是指C++类同时继承两个类或两个以上的类. class Test { public: int num1; Test() { num1 = 1; } virtual void Proc ...
- RTTI之dynamic_cast运算符
#include <iostream> #include <cstdlib> #include <ctime> using std::cout; class Gra ...
- oo第四单元作业总结
一.本单元两次作业的架构: 本单元两次作业的架构基本是一致的,所以两次作业的架构就一起说了. 为了避免查询时出现同一个结果反复计算的情况(连续两次查询一个类的顶级父类,如果我们在查询的指令中来计算其父 ...
- [bug] Python AttributeError: module 'web' has no attribute 'application'
原因 文件名是web.py,与包名web冲突 解决 重命名文件,再运行
- [刷题] 167 Two Sum II
要求 升序数组 找到两个数使得它们相加之和等于目标数 函数返回两个下标值(下标从1开始) 示例 输入:numbers = [2, 7, 11, 15], target = 9 输出:[1,2] 思路 ...
- linux最大文件打开数和swap限制
linux最大文件打开数和swap限制 逑熙 关注 2017.07.24 15:39* 字数 388 阅读 314评论 0喜欢 0 linux 2.6+的核心会使用硬盘的一部分做为SWAP分区,用 ...
- unrar命令解压rar unrar e XXX.rar (验证通过20200511)
unrar命令解压rar 一个从入门到放弃再到改行的工程师 2018-05-02 17:53:04 3916 收藏展开压缩tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成 ...