Ribbon:负载均衡(基于客户端)
负载均衡以及Ribbon
Ribbon是什么?
Spring Cloud Ribbon 是基于Netflix Ribbon 实现的一套客户端负载均衡的工具。
简单的说,Ribbon 是 Netflix 发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间层服务连接在一起。Ribbon 的客户端组件提供一系列完整的配置项,如:连接超时、重试等。简单的说,就是在配置文件中列出 LoadBalancer (简称LB:负载均衡) 后面所有的及其,Ribbon 会自动的帮助你基于某种规则 (如简单轮询,随机连接等等) 去连接这些机器。我们也容易使用 Ribbon 实现自定义的负载均衡算法!
Ribbon能干嘛?
- LB,即负载均衡 (LoadBalancer) ,在微服务或分布式集群中经常用的一种应用。
- 负载均衡简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA (高用)。
- 常见的负载均衡软件有 Nginx、Lvs 等等。 Dubbo、SpringCloud 中均给我们提供了负载均衡,SpringCloud
的负载均衡算法可以自定义。 - 负载均衡简单分类:
集中式LB
即在服务的提供方和消费方之间使用独立的LB设施,如Nginx,由该设施负责把访问请求通过某种策略转发至服务的提供方!
进程式LB
将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选出一个合适的服务器。
Ribbon 就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址!
集成Ribbon
导入依赖,注意:在消费者80端口导入
负载均衡一般是在客户端实现的
有些版本的eureka依赖里面已经集成Ribbon依赖,具体可以自己看下,有的话可以不另外导入依赖, 没有的话需要导入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
配置yml:
server:
port: 8005
#eureka配置
eureka:
client:
fetch-registry: false #不向eureka中注册自己
service-url:
defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/, http://localhost:7001/eureka/
主启动类:
package com.xuyuan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class deptConsumer {
public static void main(String[] args) {
SpringApplication.run(deptConsumer.class,args);
}
}
RestTemplateConfig中开启负载均衡
package com.xuyuan.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {//@Configuration--spring applicationContext.xml
@Bean
@LoadBalanced //变为负载均衡Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//配置负载均衡实现RestTemplate
}
@LoadBalanced
基于微服务的名字访问的 不用关心ip地址和端口号 我的是8005
负载均衡的实现方式主要有以下几种:
随机 (Random)
轮询 (RoundRobin)
一致性哈希 (ConsistentHash)
哈希 (Hash)
加权(Weighted)
加三个数据库dbo1,dbo2,dbo3
copy三个启动类 8001,8002,8003
启动两个7001,7002
演示不了内存不够
经过测试,负载均衡算法默认是通过轮询的方式实现的
Ribbon自定义算法:
configBean中用别人的随机算法
@Configuration
public class ConfigBean {//@Configuration--spring applicationContext.xml
@Bean
@LoadBalanced //变为负载均衡Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//配置负载均衡实现RestTemplate
//RiRule
//AvailabilityFilteringRule: 会先过滤掉,跳闸,访问服务~ 对剩下的进行轮循
//RoundRobinpule 轮询
//RandomRule 随机
//RetryRule:会先按照轮询获取服务~如果服务获取失败,则会在指定时间内进行,重试
@Bean
public IRule myirule(){
return new RandomRule();
}
}
这样可以实现随机但是由于怕根目录扫描所以自己键一个config
```java
package com.xuyuan.springcloud;
import com.xuyuan.myiure.XuRiue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@SpringBootApplication
@EnableEurekaClient
//在微服务中启动时候就能去加载我们自定义的Ribbon类
@RibbonClient(name="SPRINGCLOUD-02PROVIDER-DEPT",configuration = XuRiue.class)
public class deptConsumer {
public static void main(String[] args) {
SpringApplication.run(deptConsumer.class,args);
}
}
现在我们自定义一个
XuRandomRule
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.xuyuan.myiure;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class XuRandomRule extends AbstractLoadBalancerRule {
//每个服务访问五次 换下一个服务(3个)
//total=0。默认=0,如果等于5 ,我们指向下一个服务点
//index=0 默认0。如果total=5 index+1,
private int total=0;//被调用的次数
private int currentIndex = 0;//当前是谁在提供服务
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
} else {
Server server = null;
while(server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();//获取活着的服务
List<Server> allList = lb.getAllServers();//获取全部的服务
int serverCount = allList.size();
if (serverCount == 0) {
return null;
}
// int index = this.chooseRandomInt(serverCount);//生成区间随机数
// server = (Server)upList.get(index);//从活着的服务中,随机获取一个
//===========================//
if(total<5){
server = upList.get(currentIndex);
total++;
}else {
total=0;
currentIndex++;
if (currentIndex>upList.size()){
currentIndex=0;
}
server = upList.get(currentIndex);//从活着服务中,读取指定的服务来进行操作
}
//===========================//
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
}
server = null;
Thread.yield();
}
}
return server;
}
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
public Server choose(Object key) {
return this.choose(this.getLoadBalancer(), key);
}
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}
自己的
package com.xuyuan.myiure;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class XuRiue {
@Bean
public IRule myirule(){
return new XuRandomRule();//XuRandomRule调用自己的
}
}