SpringCloud无废话入门02:Ribbon负载均衡
1.白话负载均衡
在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service。为什么一样的服务我们要部署两遍?其实理由很简单:
比如,在一个电商网站中,一开始的时候,用户并没有那么多,我们只需要把网站部署在一台服务器上就可以了。但是随着用户量越来越大,一台服务器就再也满足不了要求了,这样,我们就需要把这个一模一样的网站部署在多台服务器上,但是对外它们提供的服务却都是一样的,包括域名(或者说IP地址)也必须是唯一的。
所以,上一篇中的两个provider,在实际生产环境中,甚至是同一个项目的复制而已,仅仅只是改了配置文件。在我们的介绍中,我们只是为了介绍方便,或者说便于调试,才创建了两个项目。当然,里面的服务和代码都是一模一样的。
以上,如果用白话来讲就是:将相同的服务部署在多台服务器上,对外又只暴露一个唯一的地址,这种实现就叫做:负载均衡。
如果一定要画一个图来表示,那么它大致长这样,

2.Ribbon
那我们怎么把负载均衡引入到我们的软件架构中呢?一种当时是自己写代码实现。另一种当然是利用前人已经写好的框架了。Ribbon就是这样一个框架。
Ribbon是Netflix发布的开源项目,主要功能就是提供客户端的软件负载均衡算法,将其它服务提供者连接在一起。Ribbon通过在配置文件中列出Load Balancer(简称LB)后面所有的服务提供者,然后帮助我们基于某种规则(如简单轮询,随即连接等)去连接这些机器。
3.Ribbon负载均衡实现
要让ribbon实现负载均衡,首先就需要将ribbon本身当成一个服务引入到eureka中。
创建一个web的子模块,如下:

它跟其它的项目,从类型上来说没什么区别。
Pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>springcloud.parent</artifactId>
<groupId>com.zuikc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>ribbon</name>
<artifactId>ribbon</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
application.yml,
server:
port: 9291
spring:
application:
name: Ribbon-Consumer
eureka:
client:
service-url:
defaultZone: http://localhost:9091/eureka/
providers:
ribbon:
listOfServers: http://localhost:9191/eureka,http://localhost:9192/eureka
可以看到,在这个配置文件中,我们干了两件事情,
1:将自己注册都eureka中;
2:在listOfServers中,将要负载的两个服务地址配置出来;
现在,让我们创建ServiceRibbonApplication,
package com.zuikc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName ServiceRibbonApplication
* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”
* @Author 码农星球
**/
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
所有代码都跟其它的provider一样,唯独多了一个bean的配置。我们先不说这个RestTemplate是什么,我们先看我们的HelloService,
package com.zuikc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName HelloService
* @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”
* @Author 码农星球
**/
@RestController
public class HelloService {
@Autowired RestTemplate restTemplate;
@RequestMapping(value = "/hello")
public String getHelloContent() {
return restTemplate.getForObject("http://hello-service/hello",String.class);
}
}
在这个HelloService中,我们要完成一件事情,就是将http://localhost:9291/hello的请求,随机分发到两个provider。而这个分发,在代码层面就是通过RestTemplate去实现的。
注意这个getForObject的方法中的第一个参数是个url,这个url中的hello-service就是我们配置正在eureka中的服务名。
现在,让我们启动这个ribbon application,首先可以看到eureka中有个这个ribbon服务,

其次,让我们打开地址:http://localhost:9291/hello,就可以看到ribbon的这个服务,是在provider1和provider2中不停的切换了~~

感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。
SpringCloud无废话入门02:Ribbon负载均衡的更多相关文章
- SpringCloud无废话入门03:Feign声明式服务调用
1.Feign概述 在上一篇的HelloService这个类中,我们有这样一行代码: return restTemplate.getForObject("http://hello-servi ...
- springcloud(十四)、ribbon负载均衡策略应用案例
一.eureka-server服务中心项目不再创建 二.eureka-common-empdept公共组件项目不再掩饰 三.创建eureka-client-provider-empdept-one提供 ...
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- SpringCloud无废话入门04:Hystrix熔断器及监控
1.断路器(Circuit Breaker)模式 在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的.一旦发生这种情 ...
- SpringCloud无废话入门01:最简SpringCloud应用
1.创建Parent Parent很简单,创建一个空的maven项目,pom如下: <?xml version="1.0" encoding="UTF-8" ...
- SpringBoot无废话入门02:SpringBoot启动分析
1.核心注解 在上文中,我们讲到了@SpringBootApplication是SpringBoot的核心注解. 可以很方便的在idea中下载源码来查看该注解的源码,如下: 可以看到,该注解本身又被其 ...
- SpringCloud的入门学习之概念理解、Ribbon负载均衡入门
1.Ribbon负载均衡,Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端.负载均衡的工具. 答:简单的说,Ribbon是Netflix发布的开源项目,主要功能 ...
- 最适合新手入门的SpringCloud教程 6—Ribbon负载均衡「F版本」
SpringCloud版本:Finchley.SR2 SpringBoot版本:2.0.3.RELEASE 源码地址:https://gitee.com/bingqilinpeishenme/Java ...
- SpringCloud学习笔记(2):使用Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...
随机推荐
- JMeter实现登录初始化(类似LR的init函数功能实现)
1.项目背景 在做项目的性能测试过程中,发现系统的登录功能非常慢,所以,在涉及到登录才能操作的场景,尽量避开登录操作 解决方案: 首选设置“登录并生成签名值”线程组
- day8.python文件操作
打开和关闭文件 open函数 用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写. file = open(file_name [, access_ ...
- Qt错误 —— 无法启动此程序 因为计算机丢失QtCore5.dll 以及 无法定位程序输入点于动态链接库QtCore5.dll
首先,设置计算机的环境变量Path,计算机=>右键属性=>高级设置=>环境变量=>系统变量=>双击Path,在Path后面增加C:\Qt\Qt5.8.0\5.8\ming ...
- Selenium3详解(基本操作,定位方法)
如果想使用selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动. 基本操作: 刷新:refresh, 获取浏览器窗口大小:get_window_size 设置浏览器窗口大小:set_w ...
- JAVA程序的基本结构
java程序(类)其实是一个静态方法库,或者是定义了一个数据类型.所以会遵循7种语法: 1.原始数据类型: ---整型:byte.short.int.long ---浮 ...
- Constructing Roads-最小生成树(kruskal)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题目描述: #include<cstdio> #include<cstring ...
- 在VS2017(VC15)上配置opencv4.0.1环境
在VS2017(VC15)上配置opencv4.0.1环境 转 https://blog.csdn.net/GoldenBullet/article/details/86016921 作为萌新最初 ...
- 使用Chrome浏览器设置XX-net的方法
以下介绍使用Chrome浏览器设置XX-net的方法 1.下载并安装谷歌浏览器. 2.打开https://github.com/XX-net/XX-Net/blob/master/code/d ...
- Git 日常工作中使用的命令记录
前言 这篇文章主要是介绍我在使用Git中的有一些忘记了,但是很重要的命令. 20190424 Git 历史信息 username 和 email 更改 git config alias.chang ...
- Python 面向对象的补充
isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 class Foo(object) ...