详解spring cloud中使用Ribbon实现客户端的软负载均衡

 更新时间:2018年01月15日 13:48:53   作者:牛奋lch  
这篇文章主要介绍了详解spring cloud中使用Ribbon实现客户端的软负载均衡,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

开篇

本例是在springboot整合H2内存数据库,实现单元测试与数据库无关性使用RestTemplate消费spring boot的Restful服务两个示例的基础上改造而来

在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一种,一旦ip地址发生了变化,都需要改动程序,并重新部署服务,使用Ribbon的时候,可以有效的避免这个问题。

前言:

软负载均衡的实现方式有两种,分别是服务端的负载均衡和客户端的负载均衡

服务端负载均衡:当浏览器向后台发出请求的时候,会首先向反向代理服务器发送请求,反向代理服务器会根据客户端部署的ip:port映射表以及负载均衡策略,来决定向哪台服务器发送请求,一般会使用到nginx反向代理技术。

客户端负载均衡:当浏览器向后台发出请求的时候,客户端会向服务注册器(例如:Eureka Server),拉取注册到服务器的可用服务信息,然后根据负载均衡策略,直接命中哪台服务器发送请求。这整个过程都是在客户端完成的,并不需要反向代理服务器的参与。

一、启动Eureka Server

请参考该例:spring cloud中启动Eureka Server

二、启动微服务,并注册到Eureka Server上

spring cloud-将spring boot服务注册到Eureka Server上

为了演示负载均衡的效果,再启动一个为服务,注意需要将端口号改成不一致

三、添加Ribbon支持

1、添加Ribbon的依赖

2、添加负载均衡支持

package com.chhliu.springboot.restful; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.web.client.RestTemplateBuilder; 
import org.springframework.cloud.client.loadbalancer.LoadBalanced; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.context.annotation.Bean; 
import org.springframework.web.client.RestTemplate; 
@SpringBootApplication 
@EnableEurekaClient 
public class SpringbootRestTemplateApplication { 
   
  @Autowired 
  private RestTemplateBuilder builder; 
 
  @Bean 
  @LoadBalanced // 添加负载均衡支持,很简单,只需要在RestTemplate上添加@LoadBalanced注解,那么RestTemplate即具有负载均衡的功能,如果不加@LoadBalanced注解的话,会报java.net.UnknownHostException:springboot-h2异常,此时无法通过注册到Eureka Server上的服务名来调用服务,因为RestTemplate是无法从服务名映射到ip:port的,映射的功能是由LoadBalancerClient来实现的。 
  public RestTemplate restTemplate() { 
    return builder.build(); 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(SpringbootRestTemplateApplication.class, args); 
  } 
} 

3、修改调用微服务的URL

package com.chhliu.springboot.restful.controller; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.client.RestTemplate;  
import com.chhliu.springboot.restful.vo.User; 
 
@RestController 
public class RestTemplateController { 
  @Autowired 
  private RestTemplate restTemplate; 
   
  @GetMapping("/template/{id}") 
  public User findById(@PathVariable Long id) {// 将原来的ip:port的形式,改成注册到Eureka Server上的应用名即可 
    User u = this.restTemplate.getForObject("http://springboot-h2/user/" + id, User.class); 
    System.out.println(u); 
    return u; 
  } 
} 

四、查看Eureka Server状态

五,在浏览器中,多次刷新http://localhost:7904/template/2地址

六、测试结果

7900端口服务:

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

7901端口服务:

Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 
Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 

7904端口服务:

User [id=2, username=user2, name=李四, age=20, balance=100.00] 
2017-01-23 09:58:05.682 INFO 7436 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: springboot-h2.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 
User [id=2, username=user2, name=李四, age=20, balance=100.00] 

从上面的测试结果可以看出,总共调了7904端口服务9次,其中7904端口服务调7900端口服务4次,调7901端口5次,刚好是9次

经过上面的几个步骤,就基本使用Ribbon实现了客户端负载均衡的功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java基于logback MessageConverter实现日志脱敏方案分析

    Java基于logback MessageConverter实现日志脱敏方案分析

    本文介绍了一种日志脱敏方案,即基于logbackMessageConverter和正则匹配的方法,该方法的优点是侵入性低,工作量少,只需修改xml配置文件,适用于老项目,感兴趣的朋友跟随小编一起看看吧
    2024-10-10
  • 搭建公司私有MAVEN仓库的方法

    搭建公司私有MAVEN仓库的方法

    这篇文章主要介绍了搭建公司私有MAVEN仓库的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • RabbitMQ实现消费端限流的步骤

    RabbitMQ实现消费端限流的步骤

    消费者端限流的主要目的是控制消费者每次从 RabbitMQ 中获取的消息数量,从而实现消息处理的流量控制,这篇文章主要介绍了RabbitMQ如何实现消费端限流,需要的朋友可以参考下
    2024-03-03
  • 解决引入Redisson可能会出现项目启动失败的问题

    解决引入Redisson可能会出现项目启动失败的问题

    这篇文章主要介绍了解决引入Redisson可能会出现项目启动失败的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • Java查找 List 中的最大最小值实例演示

    Java查找 List 中的最大最小值实例演示

    这篇文章主要介绍了JAVA得到数组中最大值和最小值的简单实例,需要的朋友可以参考下
    2017-04-04
  • 如何把本地jar包导入maven并pom添加依赖

    如何把本地jar包导入maven并pom添加依赖

    这篇文章主要介绍了如何把本地jar包导入maven并pom添加依赖,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • 在java中使用dom解析xml的示例分析

    在java中使用dom解析xml的示例分析

    本篇文章介绍了,在java中使用dom解析xml的示例分析。需要的朋友参考下
    2013-05-05
  • IDEA Eval Reset 使用方法汇总

    IDEA Eval Reset 使用方法汇总

    本文给大家介绍了IDEA Eval Reset 使用方法,安装插件包括离线安装方式和在线安装方式,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • 基于mybatis中数组传递注意事项

    基于mybatis中数组传递注意事项

    这篇文章主要介绍了mybatis中数组传递注意事项,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 三级联动省市ajax的代码

    三级联动省市ajax的代码

    这篇文章主要为大家详细介绍了ajax实现省市三级联动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助
    2021-07-07

最新评论