SpringCloud使用Ribbon做负载均衡-成都快上网建站

SpringCloud使用Ribbon做负载均衡

这篇文章主要讲解了“SpringCloud使用Ribbon做负载均衡”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringCloud使用Ribbon做负载均衡”吧!

在辽阳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计、外贸营销网站建设 网站设计制作定制开发,公司网站建设,企业网站建设,成都品牌网站建设,成都营销网站建设,外贸营销网站建设,辽阳网站建设费用合理。

Ribbon负载均衡

一、简介

1:什么是负载均衡

负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

负载均衡(Load Balance)其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。以上内容来自于百度百科

个人理解而言,负载均衡,是弥补了单体架构或者单个服务的负载能力不足而导致的整体性能瓶颈,因此,可以通过将一个服务部署多台服务器,然后将一台机器原来的压力分摊到多个执行单元上,这样就提升了原有架构的性能瓶颈。可以理解为 “将负载均衡到多个服务中”。常见的负载均衡有两种策略:

  • Nginx独立进程做负载均衡,通过负载均衡策略,将请求转发到不同的执行单元上

  • 客户端负载均衡策略,通过在客户端保存服务列表信息,然后自己调用负载均衡策略,分摊调用不同的执行单元。

2:什么是Ribbon

Ribbon是Netflix公司开源的一个负载均衡的组件,它属于上述负载均衡方式中的第二种。将负载均衡的逻辑封装在了客户端中,并且运行在客户端。Ribbon经过了非常严格的测试,可以更好的控制Http和Tcp客户端的负载均衡行为。

Ribbon的负载均衡有两种方式

  1. 和RestTemplate结合

  2. 和Feign结合

Ribbon的核心子模块

  1. ribbon-loadbalancer:可以独立使用或者和其他模块一起使用的负载均衡API

  2. ribbon-eureka:结合Eureka作客户端的API

  3. ribbon-core:Ribbon的核心API

3:什么是RestTemplate

RestTemplate是SpringResource中一个访问第三方RESTful API接口的网络通讯框架。其主要方法都与REST的HTTP协议的方法紧密关联。 RestTemplate支持常见的HTTP请求方式,例如GET、POST、PUT、DELETE等,所以RestTemplate很容易构建RESTful API 调用方式例如

restTemplate.getForObject("http://common-service/hello", String.class)

其核心API如下
https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html SpringCloud使用Ribbon做负载均衡

二、开始使用Ribbon

Ⅰ.代码编写
  1. ribbon-test父项目

  • pom.xml

com.calvin.ribbon
 ribbon-test
 pom
 1.0-SNAPSHOT

 
     common-service
     eureka-server
     ribbon-service
 

 
     org.springframework.boot
     spring-boot-starter-parent
     1.5.3.RELEASE
 

 
     UTF-8
     UTF-8
     1.8
 

 
     
         
             org.springframework.cloud
             spring-cloud-dependencies
            Dalston.SR4
            pom
            import
        
    
  1. eureka-server

  • pom.xml


    com.calvin.ribbon
    ribbon-test
    1.0-SNAPSHOT

4.0.0
eureka-server

    1.8



    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
    

    
        org.springframework.boot
        spring-boot-starter-test
        test
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    

application.yml

server:
  port: 8080
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone:
        http://${eureka.instance.hostname}:${server.port}/eureka
  • ServerApplication.java

@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class);
    }
}
  1. commons-service

  • pom.xml


    ribbon-test
    com.calvin.ribbon
    1.0-SNAPSHOT

4.0.0

common-service


    
        org.springframework.cloud
        spring-cloud-starter-eureka
    
    
        org.springframework.boot
        spring-boot-starter-web
    



    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    
  • application.yml

spring:
  application:
    name: common-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/

此处没有指定server.port,是因为我们在启动此服务的是时候做点手脚,需要以不同端口在本地启动, 即配置两个Name不同的启动类,给分别指定不同的Environment variables,内容为server.port=8083, server.port=8084 具体教程是在Idea下的EditConfiguration中做如下操作: SpringCloud使用Ribbon做负载均衡

  • CommonServiceApplication.java

@SpringBootApplication
@EnableEurekaClient
public class CommonServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(CommonServiceApplication.class);
    }
}
  • HelloController.java

@RestController
public class HelloController {

    @Value("${server.port}")
    private String port;

    /**
     * 接口测试
     * @return
     */
    @GetMapping("/hello")
    public String sayHello(){
        return "port : " + port ;
    }
}
  1. ribbon-service

  • pom.xml


    ribbon-test
    com.calvin.ribbon
    1.0-SNAPSHOT

4.0.0
ribbon-service

    
        org.springframework.cloud
        spring-cloud-starter-eureka
    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.cloud
        spring-cloud-starter-ribbon
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    


    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    
  • application.yml

server:
  port: 8082
spring:
  application:
    name: ribbon-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
  • RibbonClientApplication.java

@EnableEurekaClient
@SpringBootApplication
public class RibbonServerApplication {

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

    /**
     * 配置LoadBalance和RestTemplate
     * @return RestTemplate
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

接下来就是调用的关键代码

  • RemoteCommonService.java

/**
 * 

   * CommonService服务远程调用类  * 

 * @author Calvin  * @date 2019/10/09  * @since 1.0  */ @Service public class RemoteCommonService {     /**      * 注入RestTemplate      */     @Autowired     private RestTemplate restTemplate;     /**      * 远程调用common-service/hello      * @return      */     public String sayHi(){         return restTemplate.getForObject("http://common-service/hello", String.class);     } }
  • SayHiController.java

@RestController
public class SayHiController {


    @Autowired
    private RemoteCommonService remoteCommonService;

    @GetMapping("hi")
    public String sayHi(){
        return remoteCommonService.sayHi() + ", this is ribbon service";
    }

}
Ⅱ.配置启动

最终配置四个启动类,如图所示:
SpringCloud使用Ribbon做负载均衡
启动顺序:

  • step1. EurekaSeverApplicaton

  • step2. CommonServiceApplication

  • step3. CommonServiceApplication2

  • step4. RibbonServerApplication

Ⅲ.结果验证

Eureka管理界面 http://localhost:8080/
SpringCloud使用Ribbon做负载均衡

调用 http://localhost:8082/hi

SpringCloud使用Ribbon做负载均衡

刷新页面

SpringCloud使用Ribbon做负载均衡

三、核心剖析——LoadBalancerClient

负载均衡的核心类是LoadBalanceClient,此类可以获取负载均衡的服务提供者的实例信息,当然这些实例信息是通过EurekaClient获取的,并且缓存了一份服务实例信息。

@Autowired
    private LoadBalancerClient loanBalanceClient;

    /**
     * 远程调用common-service/hello
     * @return
     */
    public String sayHi(){
        ServiceInstance serviceInstance = loanBalanceClient.choose("common-service");
        logger.info("current service info is {} : {}", serviceInstance.getHost(), serviceInstance.getPort());
        return restTemplate.getForObject("http://common-service/hello", String.class);
    }

此时不断刷接口,同样可以看到LoadBalancerClient在不断的改变请求的实例信息。

四、小结

  1. 认识了关于Ribbon以及常用负载均衡的概念

  2. 动手实践了一下Ribbon进行负载均衡的调用

  3. 认识Ribbon的一个核心类

五、总结

  1. 本文首次实践Ribbon做负载均衡,缺乏原理剖析

  2. 文章中代码相对简单,但是基本可以表达Ribbon所做的事情

  3. 需要在以后,对整个Ribbon的源码进行解析(算是插眼)

  4. 关于RestTemplate还有很多用法,后续可以专门出文章进行深入理解

感谢各位的阅读,以上就是“SpringCloud使用Ribbon做负载均衡”的内容了,经过本文的学习后,相信大家对SpringCloud使用Ribbon做负载均衡这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


本文标题:SpringCloud使用Ribbon做负载均衡
文章分享:http://kswjz.com/article/iiddjp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流