基於Spring cloud Ribbon和Eureka實現客戶端負載均衡

前言

本案例將基於Spring cloud Ribbon和Eureka實現客戶端負載均衡,其中Ribbon用於實現客戶端負載均衡,Eureka主要是用於服務註冊及發現;java

傳統的服務端負載均衡

常見的服務端負載均衡有基於nginx實現的,Nginx收到請求後,經過輪詢,IP哈希等算法來決定轉發該請求到哪一個服務來處理,這種方式缺點仍是比較多的;nginx

客戶端負載均衡

在微服務架構中,會有不少服務,每一個服務有可能會有多個實例,爲了治理這些服務,咱們能夠經過eureka、consoul、 zookeeper來實現,解決完服務管理後,可是還有問題,若是當某個服務但願調用其它服務,一般會經過eureka去查詢服務列表,而後eureka返回該服務的全部實例,and 而後調用方應該用哪一個服務呢?算法

這時候,就須要客戶端負載均衡來處理這個問題了,客戶端負載均衡是和應用程序綁定在一塊兒的,咱們不須要單獨部署一個額外的服務,本文將使用Spring cloud Ribbon,能夠幫助客戶端去決定選擇哪一個服務實例來調用,並能夠設定負載均衡算法;spring

Netflix ribbon介紹

一個客戶端的負載均衡實現,Netflix ribbon提供瞭如下功能:後端

一、負載均衡緩存

二、故障容錯springboot

三、支持多種協議(HTTP, TCP, UDP)架構

四、緩存app

在maven項目中,可使用如下方式引入:負載均衡

<dependency>
    <groupId>com.netflix.ribbon</groupId>
    <artifactId>ribbon</artifactId>
    <version>2.2.2</version>
</dependency>

Netflix ribbon例子

一種快速建立工程的方法是去https://start.spring.io/網站,添加對應的依賴,而後直接下載引入到IDE便可;

一、建立Eureka服務管理工程

很簡單,主要是要在Spring boot工程里加上@EnableEurekaServer註解,並添加如下application.properties中的配置;

RibbonEurekaServerApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
@SpringBootApplication
@EnableEurekaServer
public class RibbonEurekaServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(RibbonEurekaServerApplication.class, args);
    }
}

application.properties

spring.application.name= ${springboot.app.name:eureka-serviceregistry}
server.port = ${server-port:8761}
eureka.instance.hostname= ${springboot.app.name:eureka-serviceregistry}
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry= false
eureka.client.serviceUrl.defaultZone: http://${registry.host:localhost}:${server.port}/eureka/

而後啓動,界面以下:

二、建立服務端工程

首先,建一個controller

@RestController
public class MyRestController {

    @Autowired
    Environment environment;

    @GetMapping("/")
    public String health() {
        return "I am Ok";
    }

    @GetMapping("/backend")
    public String backend() {
        System.out.println("Inside MyRestController::backend...");

        String serverPort = environment.getProperty("local.server.port");

        System.out.println("Port : " + serverPort);

        return "Hello form Backend!!! " + " Host : localhost " + " :: Port : " + serverPort;
    }
}

而後新建啓動類,並加上@EnableDiscoveryClient註解

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

最後,加上配置文件application.properties

spring.application.name=server
server.port = 9090
 
eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2

三、建立客戶端工程

注意客戶端工程須要添加spring-cloud-starter-netflix-ribbon依賴;

首先,咱們新建啓動類,並加上@RibbonClient@EnableDiscoveryClient註解

@EnableDiscoveryClient
@SpringBootApplication
@RibbonClient(name = "server", configuration = RibbonConfiguration.class)
public class RibbonClientApplication {

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

注意這裏的RibbonConfiguration,是咱們的自定義配置類,這裏面的方法能夠本身根據狀況重寫,本例子只是一個簡單的測試,用的是Ribbon提供的默認方式,代碼以下:

public class RibbonConfiguration {

    @Autowired
    IClientConfig config;

    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

最後,添加application.properties配置文件,以下:

spring.application.name=client
server.port=8888

eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2


server.ribbon.eureka.enabled=true
#server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092
server.ribbon.ServerListRefreshInterval=1000
#logging.level.root=TRACE

例子測試驗證

首先,打包,而後啓動全部的以上服務,使用java -jar -Dserver.port=XXXX YYYYY.jar命令啓動便可;

啓動後,能夠在eureka中看到咱們啓動的服務:

因爲咱們要演示客戶端負載均衡功能,因此再啓動幾個服務端服務,端口爲9091 and 9092,啓動後以下:

OK,準備工做搞好了,能夠開測了,點擊http://localhost:8888/client/frontend幾回,結果以下,,

一、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9090

二、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9092

三、Server Response :: Hello form Backend!!! Host : localhost :: Port : 9091

而後能夠再啓動或刪除幾個後端服務,如啓動一個9093端口,在測試一下,9093端口對應的服務也能夠訪問到,效果一樣OK。。。

那麼,若是隻想寫死,從幾個服務中選擇,而不是自動變更呢,能夠在客戶端用如下配置,那麼就會一直從這三個服務中選取了。

server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092
相關文章
相關標籤/搜索