天天看點

SpringCloud微服務(02):Ribbon和Feign元件,實作服務調用的負載均衡

本文源碼:GitHub·點這裡 || GitEE·點這裡

一、Ribbon簡介

1、基本概念

Ribbon是一個用戶端的負載均衡(Load Balancer,簡稱LB)器,它提供對大量的HTTP和TCP用戶端的通路控制。

2、負載均衡簡介

目前主流的負載均衡方案可分成兩類:

1)集中式

即在服務的消費方和提供方之間使用獨立的LB設施,可以是硬體,如F5,也可以是軟體,如nginx,由該設施負責把通路請求通過某種政策轉發至服務的提供方;

2)程序内

将LB邏輯內建到消費方,消費方從服務注冊中心擷取可用服務清單,然後根據指定負載均衡政策選擇合适的伺服器。Ribbon就屬于該方式。

3、Ribbon負載政策

SpringCloud微服務(02):Ribbon和Feign元件,實作服務調用的負載均衡
1) RoundRobinRule 輪詢
輪詢服務清單List<Server>的index,選擇index對應位置的服務。
2) RandomRule 随機
随機服務清單List<Server>的index,選擇index對應位置的服務。
3) RetryRule 重試
指定時間内,重試(請求)某個服務不成功達到指定次數,則不再請求該服務。
           

二、Feign簡介

Feign 是一個聲明式的 Web Service 用戶端。它的出現使開發 Web Service 用戶端變得很簡單。使用 Feign 隻需要建立一個接口加上對應的注解,比如:@FeignClient 接口類注解。

2、執行流程

  1. 主程式入口添加 @EnableFeignClients 注解開啟對 FeignClient 接口掃描加載。接口使用@FeignClient注解。
  1. 調用Feign 接口中的方法被時,通過JDK的代理的方式,生成具體的 RequestTemplate。
  1. RequestTemplate 生成 Request請求,結合Ribbon實作服務調用負載均衡政策。

三、綜合使用案例

1、項目結構圖

SpringCloud微服務(02):Ribbon和Feign元件,實作服務調用的負載均衡

1)、子產品描述

Eureka注冊中心
node02-eureka-7001
兩個服務提供方
node02-provider-6001
node02-provider-6002
Ribbon服務調用
node02-consume-8001
Feign服務調用
node02-consume-8002
           

2)、依賴Eureka知識

上篇文章Eureka使用:

2、Ribbon服務調用

代碼所屬子產品:node02-consume-8001

1)、核心依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
           

2)、配置檔案

@Configuration
public class LoadConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate (){
        return new RestTemplate() ;
    }
    @Bean
    public IRule getIRule (){
        // 預設輪詢算法
        // return new RoundRobinRule() ;
        // 重試算法:預設情況,通路某個服務連續三次失敗,就不會再通路
        // return new RetryRule() ;
        // 随機算法
        return new RandomRule() ;
    }
}

           

3)、調用方式

@RestController
public class ConsumeController {

    @Autowired
    private RestTemplate restTemplate ;

    String server_name = "http://NODE02-PROVIDER" ;
    // http://localhost:8001/showInfo
    @RequestMapping("/showInfo")
    public String showInfo (){
        return restTemplate.getForObject(server_name+"/getInfo",String.class) ;
    }

}
           

這裡的NODE02-PROVIDER就是服務提供方的配置檔案。兩個服務提供方的這塊配置相同,Ribbon正基于此,實作多個服務調用的負載均衡。

spring:
  application:
    name: node02-provider
           

4)、提供方接口

@RequestMapping("/getInfo")
public String getInfo (){
    LOG.info("provider-6002");
    return "success" ;
}
           

3、Feign服務調用

代碼所屬子產品:node02-consume-8002

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
           
@FeignClient(value = "NODE02-PROVIDER")
public interface GetAuthorService {
    @RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET)
    String getAuthorInfo (@PathVariable("authorId") String authorId) ;
}
           
@RestController
public class ConsumeController {
    @Resource
    private GetAuthorService getAuthorService ;
    @RequestMapping(value = "/getAuthorInfo")
    public String getAuthorInfo () {
        return getAuthorService.getAuthorInfo("1") ;
    }
}
           

4)、啟動類注解

// 因為包名路徑不同,需要加basePackages屬性
@EnableFeignClients(basePackages={"cloud.block.code.service"})
           

5)、提供方接口

@RequestMapping(value = "/getAuthorInfo/{authorId}",method = RequestMethod.GET)
public String getAuthorInfo (@PathVariable("authorId") String authorId) {
    LOG.info("provider-6002");
    return "知了一笑"+authorId ;
}
           

四、源代碼位址

GitHub·位址
https://github.com/cicadasmile/spring-cloud-base
GitEE·位址
https://gitee.com/cicadasmile/spring-cloud-base
           
SpringCloud微服務(02):Ribbon和Feign元件,實作服務調用的負載均衡

繼續閱讀