前言

本案例将基于Spring cloud Ribbon和Eureka实现客户端负载均衡,其中Ribbon用于实现客户端负载均衡,Eureka主要是用于服务注册及发现;

传统的服务端负载均衡

常见的服务端负载均衡有基于nginx实现的,Nginx收到请求后,通过轮询,IP哈希等算法来决定转发该请求到哪个服务来处理,这种方式缺点还是比较多的;

客户端负载均衡

在微服务架构中,会有很多服务,每个服务有可能会有多个实例,为了治理这些服务,我们可以通过eureka、consoul、 zookeeper来实现,解决完服务管理后,但是还有问题,如果当某个服务希望调用其它服务,通常会通过eureka去查询服务列表,然后eureka返回该服务的所有实例,and 然后调用方应该用哪个服务呢?

这时候,就需要客户端负载均衡来处理这个问题了,客户端负载均衡是和应用程序绑定在一起的,我们不需要单独部署一个额外的服务,本文将使用Spring cloud Ribbon,可以帮助客户端去决定选择哪个服务实例来调用,并可以设定负载均衡算法;

Netflix ribbon介绍

一个客户端的负载均衡实现,Netflix ribbon提供了以下功能:

1、负载均衡

2、故障容错

3、支持多种协议(HTTP, TCP, UDP)

4、缓存

在maven项目中,可以使用以下方式引入:

<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
<version>2.2.2</version>
</dependency>

Netflix ribbon例子

一种快速创建工程的方法是去https://start.spring.io/网站,添加对应的依赖,然后直接下载引入到IDE即可;

1、创建Eureka服务管理工程

很简单,主要是要在Spring boot工程里加上@EnableEurekaServer注解,并添加以下application.properties中的配置;

RibbonEurekaServerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class RibbonEurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(RibbonEurekaServerApplication.class, args);
}
}

application.properties

spring.application.name= ${springboot.app.name:eureka-serviceregistry}
server.port = ${server-port:8761}
eureka.instance.hostname= ${springboot.app.name:eureka-serviceregistry}
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry= false
eureka.client.serviceUrl.defaultZone: http://${registry.host:localhost}:${server.port}/eureka/

然后启动,界面如下:

2、创建服务端工程

首先,建一个controller

@RestController
public class MyRestController { @Autowired
Environment environment; @GetMapping("/")
public String health() {
return "I am Ok";
} @GetMapping("/backend")
public String backend() {
System.out.println("Inside MyRestController::backend..."); String serverPort = environment.getProperty("local.server.port"); System.out.println("Port : " + serverPort); return "Hello form Backend!!! " + " Host : localhost " + " :: Port : " + serverPort;
}
}

然后新建启动类,并加上@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonServerApplication { public static void main(String[] args) {
SpringApplication.run(RibbonServerApplication.class, args);
}
}

最后,加上配置文件application.properties

spring.application.name=server
server.port = 9090 eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2

3、创建客户端工程

注意客户端工程需要添加spring-cloud-starter-netflix-ribbon依赖;

首先,我们新建启动类,并加上@RibbonClient@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
@RibbonClient(name = "server", configuration = RibbonConfiguration.class)
public class RibbonClientApplication { public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}

注意这里的RibbonConfiguration,是我们的自定义配置类,这里面的方法可以自己根据情况重写,本例子只是一个简单的测试,用的是Ribbon提供的默认方式,代码如下:

public class RibbonConfiguration {

    @Autowired
IClientConfig config; @Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
} @Bean
public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule();
}
}

最后,添加application.properties配置文件,如下:

spring.application.name=client
server.port=8888 eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2 server.ribbon.eureka.enabled=true
#server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092
server.ribbon.ServerListRefreshInterval=1000
#logging.level.root=TRACE

例子测试验证

首先,打包,然后启动所有的以上服务,使用java -jar -Dserver.port=XXXX YYYYY.jar命令启动即可;

启动后,可以在eureka中看到我们启动的服务:

由于我们要演示客户端负载均衡功能,所以再启动几个服务端服务,端口为9091 and 9092,启动后如下:

OK,准备工作搞好了,可以开测了,点击http://localhost:8888/client/frontend几次,结果如下,,

1、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9090

2、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9092

3、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9091

然后可以再启动或删除几个后端服务,如启动一个9093端口,在测试一下,9093端口对应的服务也可以访问到,效果同样OK。。。

那么,如果只想写死,从几个服务中选择,而不是自动变动呢,可以在客户端用以下配置,那么就会一直从这三个服务中选取了。

server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092

基于Spring cloud Ribbon和Eureka实现客户端负载均衡的更多相关文章

  1. Spring Cloud官方文档中文版-客户端负载均衡:Ribbon

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

  2. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  3. Spring Cloud Ribbon---微服务调用和客户端负载均衡

    前面分析了Eureka的使用,作为服务注册中心,Eureka 分为 Server 端和 Client 端,Client 端作为服务的提供者,将自己注册到 Server 端,Client端高可用的方式是 ...

  4. Spring Cloud Ribbon 中的 7 种负载均衡策略

    负载均衡通器常有两种实现手段,一种是服务端负载均衡器,另一种是客户端负载均衡器,而我们今天的主角 Ribbon 就属于后者--客户端负载均衡器. 服务端负载均衡器的问题是,它提供了更强的流量控制权,但 ...

  5. 《Spring Cloud》学习(二) 负载均衡!

    第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...

  6. Spring Cloud入门教程-Ribbon实现客户端负载均衡

    简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...

  7. Spring Cloud Ribbon 客户端负载均衡 4.3

      在分布式架构中,服务器端负载均衡通常是由Nginx实现分发请求的,而客户端的同一个实例部署在多个应用上时,也需要实现负载均衡.那么Spring Cloud中是否提供了这种负载均衡的功能呢?答案是肯 ...

  8. Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)

    文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...

  9. 客户端负载均衡 - Ribbon

    Ribbon是Netflix公司开源的一个负载均衡的项目(https://github.com/Netflix/ribbon),它是一个基于HTTP.TCP的客户端负载均衡器. 服务端负载均衡 负载均 ...

随机推荐

  1. 三、自动化测试平台搭建-django-如何用mysql数据库做web项目

    从这节开始到后面说的大概内容如下: 这里说的是Django做一个web项目的大概框架,从下篇具体说Django中的模型(查询..),视图(请求,响应,cookie,session..),模板(验证码, ...

  2. Array库

    /** * 查找元素在数组中出现的所有位置 * @param {要查找的数组} array * @param {要查找的元素} ele * @param {回调函数} callback */ func ...

  3. SpringBoot使用prometheus监控

    本文介绍SpringBoot如何使用Prometheus配合Grafana监控. 1.关于Prometheus Prometheus是一个根据应用的metrics来进行监控的开源工具.相信很多工程都在 ...

  4. <算法图解>读书笔记:第1章 算法简介

    阅读书籍:[美]Aditya Bhargava◎著 袁国忠◎译.人民邮电出版社.<算法图解> 第1章 算法简介 1.2 二分查找 一般而言,对于包含n个元素的列表,用二分查找最多需要\(l ...

  5. 土制Excel导入导出及相关问题探讨

    转载请注明出处https://www.cnblogs.com/funnyzpc/p/10392085.html 新的一年,又一个开始,不见收获,却见年龄,好一个猪年,待我先来一首里尔克的诗: < ...

  6. login shell 和 non-login shell 的区别

              login shell:去的bash时需要完整的登录流程.就是说通过输入账号和密码登录系统,此时取得的shell称为login shell non-login shell:取得sb ...

  7. jquery获取一组文本框的值

    $("#btn5").click(function()  {    var str="";$("[name='checkbox'][checked]& ...

  8. 记录一种下载https网址中的mp4文件的方法

    需要下载一个网页中的视频, 页面中的视频播放器为 JW player, 通过搜索发现可以下载对应的视频. 1. 使用chrome浏览器分析 网页中的视频地址: F12或者右键-->检查, 在打开 ...

  9. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  10. Linux 管理进程

    探查进程 参数 描述 -A 显示所有进程 -N 显示与指定参数不符的所有进程 -a 显示除控制进程(session leader1)和无终端进程外的所有进程 -d 显示除控制进程外的所有进程 -e 显 ...