基于Spring cloud Ribbon和Eureka实现客户端负载均衡
前言
本案例将基于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实现客户端负载均衡的更多相关文章
- Spring Cloud官方文档中文版-客户端负载均衡:Ribbon
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...
- Spring Cloud入门教程(二):客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- Spring Cloud Ribbon---微服务调用和客户端负载均衡
前面分析了Eureka的使用,作为服务注册中心,Eureka 分为 Server 端和 Client 端,Client 端作为服务的提供者,将自己注册到 Server 端,Client端高可用的方式是 ...
- Spring Cloud Ribbon 中的 7 种负载均衡策略
负载均衡通器常有两种实现手段,一种是服务端负载均衡器,另一种是客户端负载均衡器,而我们今天的主角 Ribbon 就属于后者--客户端负载均衡器. 服务端负载均衡器的问题是,它提供了更强的流量控制权,但 ...
- 《Spring Cloud》学习(二) 负载均衡!
第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...
- Spring Cloud入门教程-Ribbon实现客户端负载均衡
简介 我们继续以之前博客的代码为基础,增加Ribbon组件来提供客户端负载均衡.负载均衡是实现高并发.高性能.可伸缩服务的重要组成部分,它可以把请求分散到一个集群中不同的服务器中,以减轻每个服务器的负 ...
- Spring Cloud Ribbon 客户端负载均衡 4.3
在分布式架构中,服务器端负载均衡通常是由Nginx实现分发请求的,而客户端的同一个实例部署在多个应用上时,也需要实现负载均衡.那么Spring Cloud中是否提供了这种负载均衡的功能呢?答案是肯 ...
- Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)
文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...
- 客户端负载均衡 - Ribbon
Ribbon是Netflix公司开源的一个负载均衡的项目(https://github.com/Netflix/ribbon),它是一个基于HTTP.TCP的客户端负载均衡器. 服务端负载均衡 负载均 ...
随机推荐
- 网络攻击技术:SQL Injection(sql注入)
网络攻击技术开篇——SQL Injection 1.1.1 摘要 日前,国内最大的程序员社区CSDN网站的用户数据库被黑客公开发布,600万用户的登录名及密码被公开泄露,随后又有多家网站的用户密码 ...
- 【转】window.onerror跨域问题
What the heck is "Script error"? Ben Vinegar/ May 17, 2016 If you’ve done any work with th ...
- DWM1000 自动应答代码实现与实例
这一节继续继承之前帧过滤部分,首先补充一下关于帧过滤部分,如果将目标地址设置为0xFFFF,则同一个网络(物理频道与PANID 都相同),所有节点都应该收到这条信息,这个信息为广播信息,0xFFFF为 ...
- ORM框架之SQLAchemy
SQLAchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,即:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果. 1.安装 ...
- SpringBoot报错:Table 'database_name.hibernate_sequence' doesn't exist
引起条件: SpringBoot+JPA插入包含自增字段的对象 @Id @GeneratedValue private Integer id; 解决方法: 给注解添加属性 @Id @Generated ...
- GeoHash(Java实现)
package com.koubei.collect_script.demo; import java.util.ArrayList; import java.util.Arrays; import ...
- postgresql从timestamp(6)复制到timestamp(0),时间会变
主要涉及临界点(跨天) 例子(时间:2016-08-05 23:59:59.863) timestamp(6):2016-08-05 23:59:59.863 timestamp(0):2016-08 ...
- ssm框架各自的作用
- A_B_Good Bye 2018_cf
A. New Year and the Christmas Ornament time limit per test 1 second memory limit per test 256 megaby ...
- Super Jumping! Jumping! Jumping! ---HDU - 1087
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. May ...