Ribbon负载均衡(四)


一、Ribbon定义

spring cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡工具

 

简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法

,将Netflix的中间层服务层连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接

超时,重试等.简单的说,就是在配置文件中列出Load Balancer(简称LB)后面所有的机器,

Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器.我们很容易使用Ribbon实现自定义的负载均衡算法

 ribbon原理图:

 

二、LB负载均衡

集中式LB

即在服务的消费方和提供方之间使用独立的LB设施(可以是硬件,比如F5,也可以是软件,如Nginx)由该设施负责把访问请求通过某一种策略转发至服务的提供者;

进程式LB

将LB逻辑集成到消费方,消费方从服务中心获取知有哪些地址可用,然后自己再从这些地址中选择出一个合适的服务器.

Ribbon就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过他来获取到服务提供方的地址.

三、官方地址

https://github.com/Netflix/ribbon

四、Ribbon的初始化配置

1、修改microservicecloud-consumer-dept-80工程 

修改pom文件

      <!-- 将微服务provider侧注册进eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

修改yml文件 添加

eureka:
  client:
    service-url:
      defaultZone:  http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #向服务中心注册自己
    register-with-eureka: false 

对configBean进行新注解@LoadBalanced获得Rest时加入Ribbon的配置

@Configuration public class ConfigBean { @Bean @LoadBalanced //注解表明这个restRemplate开启负载均衡的功能。
    public RestTemplate getRestTemplate(){ return new RestTemplate(); } }

主启动类ConSumeAppStart_80添加@EnableEurekaClient

@SpringBootApplication @EnableEurekaClient public class ConSumeAppStart { public static void main(String[] args) { SpringApplication.run(ConSumeAppStart.class); } }

修改DeptController客户端访问类

@RestController
public class DeptController {

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
//完成真正的通过微服务名字从eureka上找到并访问
    /**
     * 使用 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap,
     * ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
     */
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping(value = "/consumer/dept/add")
    public boolean add(Dept dept)
    {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/addDept", dept, Boolean.class);
    }

    @RequestMapping("/findAll")
    public List<Dept> findAll() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/findAll",List.class);
    }
}

先启动3个eureka集群后,在启动microservicecloud-config-dept-client-8001并注册进eureka

启动ConSumeAppStart_80

测试 访问地址:http://localhost:80/findAll

效果

 

五、Ribbon负载均衡

1、架构说明图

 

Ribbon在工作分成2步

 第一步:先选择EurekaServer,它优先选择在同一个区域负载较少的server

第二步:再根据用户指定策略,在从server取到的服务注册列表中选择一个地址.

其中Ribbon提供了多种策略:比如轮询,随机和根据时间响应加权

 六、创建提供者多服务

参考microservicecloud-provider-dept-8001新建2份,分别命名为8002,8003  

新建8002/8003 数据库,各自微服务分别连各自的数据库clouddb02/clouddb03

修改8002/8003各自的yml

8002 yml

server: port: 8002 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.yehui.entity # 所有Entity别名类所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件 #应用名称 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/clouddb02 # 数据库名称 username: root password: root dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/ ###是否向注册中心注册自己 register-with-eureka: true ###是否需要从eureka上获取注册信息 fetch-registry: true instance: instance-id: providerdept_8001 #自定义服务名称 prefer-ip-address: true #访问路径可以显示IP地址 #信息的描述 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$

 8003yml

server: port: 8003 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.yehui.entity # 所有Entity别名类所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/clouddb03 # 数据库名称 username: root password: root dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/,http://localhost:7001/eureka/ instance: instance-id: providerdept_8003 #自定义服务名称 prefer-ip-address: true #访问路径可以显示IP地址 #信息的描述 info: app.name: atguigu-microservicecloud company.name: www.atguigu.com build.artifactId: $project.artifactId$ build.version: $project.version$

备注  

   端口

数据库链接  jdbc:mysql://localhost:3306/clouddb01

对外暴露的统一的服务实例名

 

启动3个eureka集群配置区

启动3个Dept微服务并各自测试通过

 

http://localhost:8003/dept/findAll

http://localhost:8001/dept/findAll

http://localhost:8002/dept/findAll

 

启动ConSumeAppStart_80

客户端通过Ribbon完成负载均衡并访问上一步的Dept微服务

 

上图有三个消费者

 

http://localhost/findAll

注意观察看到返回的数据的数据库的不同

 

从上面可以得到:Ribbon其实就是一个软负载均衡的客户端组件,他可以和其他所需求请求的客户端结合使用,eureka结合只是其中的一个实例

七、Ribbon核心组件IRule

1、IRule算法

IRule:根据特点算法中从服务列表中选取一个要访问的服务

RoundRobinRule:轮询算法

RandomRule随机算法

AvailabilityFilteringRule:会先过滤由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问

WeightedResponseTimeRule:根据平均响应的时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高,刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够会切换到WeightedResponseTimeRule

RetryRule:先按照RoundRobinRule的策略获取服务,如果获取失败则在制定时间内进行重试,获取可用的服务。

BestAviableRule:会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务

ZoneAvoidanceRule:默认规则,符合判断server所在区域的性能和server的可用性选择服务器

RetryRule:先根据RoundRonbinRule的策略获取服务,如果获取服务失败则在指定时间内会进行重试,获取可用的服务

访问地址:https://github.com/Netflix/ribbon

 

 config类

@Bean
public IRule getRule(){
    return new RandomRule(); //使用随机的算法替换默认的轮询算法
}

测试访问:http://localhost:80/findAll

 2、Ribbon自定义

修改microservicecloud-consumer-dept-80

 

主启动类添加@RibbonClient

在启动该微服务的时候就能去加载我们自定义Ribbon配置类,从而使配置生效,形如:

@RibbonClient(name = "MICROSERVICECLOUD-DEPT",configuration = MyRule.class)

注意细节

步骤:

  新建一个MyRule类

@Configuration public class MyRule { @Bean public IRule getRule(){ return new RoundRobinRule(); //使用使用轮询算法
 } }

  修改主启动类新增@RibbonClient注解

@SpringBootApplication @EnableEurekaClient //表明自己是一个eurekaclient.
@RibbonClient(name = "MICROSERVICECLOUD-DEPT",configuration = MyRule.class) public class ConSumeAppStart_80 { public static void main(String[] args) { SpringApplication.run(ConSumeAppStart_80.class); } }

 测试http://localhost:80/findAll

3、自定义规则深度解析

问题:依旧轮询策略,但是加上新需求,每个服务要求被调用5次.也即

以前是每台机器一次,现在是每台机器5次

解析源码:https://github.com/Netflix/ribbon/blob/master/ribbon-

loadbalancer/src/main/java/com/netflix/loadbalancer/RandomRule.java

参考源码修改为我们的需求需的MyRole_YH.java

 更新中

4、使用DiscoveryClient实现本地负载均衡

编写controller

@RestController public class DeptController { //获取服务注册信息
 @Autowired private DiscoveryClient discoveryClient; @Autowired private RestTemplate restTemplate; @RequestMapping("/find") public List<Dept> find(){ List<ServiceInstance> instances = discoveryClient.getInstances("microservicecloud-dept"); int instacesSize = instances.size(); int index = num%instacesSize; num++; String url = instances.get(index).getUri().toString(); return restTemplate.getForObject(url + "/dept/findAll",List.class); } } 

congig类

@Configuration public class ConfigBean { @Bean //@LoadBalanced 这个注解不需要了
    public RestTemplate getRestTemplate(){ return new RestTemplate(); } }

测试

5、Ribbon与Nginx区别

客户端负载均衡器

 在SpringCloud中Ribbon负载均衡客户端,会从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地。

让后在本地实现轮训负载均衡策略。

服务器端负载均衡Nginx

 nginx是客户端所有请求统一交给nginx,由nginx进行实现负载均衡请求转发,属于服务器端负载均衡。

 既请求有nginx服务器端进行转发。

客户端负载均衡Ribbon

 Ribbon是从eureka注册中心服务器端上获取服务注册信息列表,缓存到本地,让后在本地实现轮训负载均衡策略。

 既在客户端实现负载均衡。

 应用场景的区别:

Nginx适合于服务器端实现负载均衡 比如Tomcat ,Ribbon适合与在微服务中RPC远程调用实现本地服务负载均衡,比如Dubbo、SpringCloud中都是采用本地负载均衡。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM