1. 程式人生 > >Spring-cloud之Ribbon負載均衡的使用及負載均衡策略配置(與Eurka配合使用)

Spring-cloud之Ribbon負載均衡的使用及負載均衡策略配置(與Eurka配合使用)

       什麼是Ribbon,ribbon有什麼用,個人先總結一下(不正確請提出討論):Ribbon是基於客戶端的負載均衡器,為我們提供了多樣的負載均衡的方案,比如輪詢,最小的併發請求的server,隨機server等;其預設的策略是ZoneAvoidanceRule,也就是複合判斷server所在區域的效能和server的可用性選擇server,使用ZoneAvoidancePredicate和AvailabilityPredicate來判斷是否選擇某個server,前一個判斷判定一個zone的執行效能是否可用,剔除不可用的zone(的所有server),AvailabilityPredicate用於過濾掉連線數過多的Server。

       在上面兩篇博文中我們介紹了Eureka的使用和相關的博文教程。在上一篇博文中(https://blog.csdn.net/asd529735325/article/details/85044158),在我們服務呼叫方,我們就用到了Ribbon,我們使用Ribbon的RestTemplate,其整合了EurekaClient,Ribbon基於客戶端的負載均衡器為我們提供了多樣的負載均衡的功能,我們所需要做的就是在Spring中註冊一個RestTemplate,並且新增@LoadBalanced 註解。(上一篇我們新增的是LoadBalanced,預設

ZoneAvoidanceRule策略)。

     這裡我們詳細實驗一下Ribbon如何實現的負載均衡。

     在https://github.com/wades2/EurekaDemo2這個程式碼的基礎上我們添加了一個Client端,也是作為server例項的。

     

       為了區分是最終究竟訪問的哪個EurekaClient,我們的Cotroller返回的和第一個有差異(但是實際開發中不可以!我們這裡只是為了實驗!!強調!!)。

       我們按照之前註冊到服務端一樣把新的Client註冊到Eurekaserver,以下是我個人的配置檔案:

       

spring.application.name=Eureka-client

#eureka.client.allow-redirects=false
#修改啟動埠
server.port=8085
eureka.client.serviceUrl.defaultZone=http://user:[email protected]:8083/eureka,http://user:[email protected]:8082/eureka
#eureka.client.register-with-eureka=true
eureka.instance.ip-address=true

注意:spring.application.name必須和EurekaClient的一致,因為restTemplate是通過spring.application.name+請求介面地址來實現請求的類似於httpClient的框架(可以理解成域名或IP+埠號就能實現請求,只是在Ribbon中換了個形式而已)。

       

package com.example.demo.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private EurekaClient eurekaClients;

//    @Autowired
//    private DiscoveryClient discoveryClient;

    @GetMapping("getUser")
    public String getUser() {
        //todo 得到eureka server的服務例項
//        InstanceInfo info=eurekaClients.getNextServerFromEureka("Eureka-client",false);
//        return info.getHomePageUrl();
        return "hello client2";
    }
}

Controller除了返回的資訊不一樣,其他都一樣。

       然後啟動訪問http://localhost:8086/getUser/routine,我們可以看到,請求同樣的url,返回的一次是hello client2,一次是hello one,我們可以知道,客戶端的負載均衡已經完成了。

       接下來我們來說說如何根據自己的需求配置負載均衡策略,我們剛在前文提到Ribbon預設的策略是ZoneAvoidanceRule,我們通過實驗來試試其他策略:

       在Eurakacaller中Controller下new一新的Class作為config,來配置我們的策略。

      

       然後重構下Controller(因為之前是把Config直接寫在Controller裡面的):

      

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
//@Configuration
public class Controller {
//    @Bean
//    @LoadBalanced
//    public RestTemplate getRestTemplate() {
//        return new RestTemplate();
//    }
//
//    @GetMapping("getUser/routine")
//    public String routine() {
//        RestTemplate restTemplate = getRestTemplate();
//
//        String json = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
//        return json;
//    }

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("getUser/routine")
    public String routine() {
//        RestTemplate restTemplate = configBean.getRestTemplate();
        String json = restTemplate.getForObject("http://Eureka-client/user/getUser", String.class);
        return json;
    }

//    @GetMapping("getUser/routine2")
//    public String routine2() {
//        RestTemplate restTemplate = getRestTemplate();
//        String json = restTemplate.getForObject("http://Eureka-client/user/getUser2", String.class);
//        return json;
//    }
}

然後配置下我們的Ribbon策略:

package com.example.demo.controller;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {
    @Bean
    @LoadBalanced // ribbon負載均衡
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @Bean
    public IRule muRule() {
         return new RoundRobinRule();//輪詢
//        return new RandomRule();// 隨機
    }
}

選擇輪詢還是隨機看你,如果登出了muRule,則變為預設策略。

OK,有關Ribbon的配置就結束了,歡迎大家一起討論評論。

這裡推薦一個對Ribbon有深入研究大佬的部落格地址,看了後會明白很多,Ribbon是如何通過restTemplate來發送http請求呼叫的:https://blog.csdn.net/puhaiyang/article/details/79682177