微服務Spring Cloud (二)遠程調用 Ribbon

轉載請表明出處 https://blog.csdn.net/Amor_Leo/article/details/87875269 謝謝

Spring Cloud Ribbon 概述

Spring Cloud Ribbon 是一個基於Http和TCP的客服端負載均衡工具,它是基於Netflix Ribbon實現的。它提供了很多負載均衡算法,例如輪詢、隨機等,在配置服務提供者地址後,可以將服務消費者請求按照負載均衡算法進行分發。
它主要包括六個組件:

  • ServerList:負載均衡使用的服務器列表.這個列表會緩存在負載均衡器中,並定期更新.當Ribbon與Eureka結合使用時,ServerList的實現類就是DiscoveryEnabledNIWSServerList,它會保存Eureka Server中註冊的服務實例表.
  • ServerListFilter:服務器列表過濾器.這是一個接口,主要用於對Service Consumer獲取到的服務器列表進行預過濾,過濾的結果也是ServerList.Ribbon提供了多種過濾器的實現.
  • IPing:探測服務實例是否存活的策略.
  • IRule:負載均衡策略,其實現類表述的策略包括:輪詢、隨機、根據響應時間加權等.我們也可以自己定義負載均衡策略,比如我們就利用自己實現的策略,實現了服務的版本控制和直連配置.實現好之後,將實現類重新注入到Ribbon中即可.
  • ILoadBalancer:負載均衡器.這也是一個接口,Ribbon爲其提供了多個實現,比如ZoneAwareLoadBalancer,而上層代碼通過調用其API進行服務調用的負載均衡選擇.一般ILoadBalancer的實現類中會引用一個IRule.
  • RestClient:服務調用器.顧名思義,這就是負載均衡後,Ribbon向Service Provider發起REST請求的工具.

Ribbon提供的主要負載均衡策略介紹:

  1. 簡單輪詢負載均衡(RoundRobin): 以輪詢的方式依次將請求調度不同的服務器,即每次調度執行i = (i + 1) mod n,並選出第i臺服務器。
  2. 隨機負載均衡 (Random): 隨機選擇狀態爲UP的Server。
  3. 加權響應時間負載均衡 (WeightedResponseTime): 根據相應時間分配一個weight,相應時間越長,weight越小,被選中的可能性越低。
  4. 區域感知輪詢負載均衡(ZoneAvoidanceRule): 複合判斷server所在區域的性能和server的可用性選擇server。

Spring Cloud Ribbon 搭建

  • pom (Eureka裏包含Ribbon)
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
	    </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • yml
spring:
  application:
    name: consumer-service
server:
  port: 8080
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
  • Application 類
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
  @Bean
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
  • Controller
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

	@GetMapping("/get/provider/test")
	public String rpc() {                  
		String forObject = restTemplate.getForObject("http://PROVIDER-SERVICE/test", String.class);
		return forObject;
	}

    @GetMapping("/log-user-instance")
    public void logUserInstance() {
    	ServiceInstance serviceInstance = this.loadBalancerClient.choose("PROVIDER-SERVICE");
    	// 打印當前選擇的是哪個節點
 	   MovieController.LOGGER.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
  • 如果要修改負載均衡算法
spring:
  application:
    name: consumer-service
server:
  port: 8080
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #訪問路徑可以顯示ip地址
provider-service:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

< ClientName >.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
在這裏插入圖片描述

  • 如果不添加eureka依賴
  • pom
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
	    </dependency>
  		<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  		</dependency>
  • yml
spring:
  application:
    name: consumer-service
server:
  port: 8080
provider-service:
  ribbon:
    listOfServers: localhost:8000,localhost:8001
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章