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


前言

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

傳統的服務端負載均衡

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

客戶端負載均衡

在微服務架構中,會有很多服務,每個服務有可能會有多個實例,為了治理這些服務,我們可以通過eureka、consoul、 zookeeper來實現,解決完服務管理后,但是還有問題,如果當某個服務希望調用其它服務,通常會通過eureka去查詢服務列表,然后eureka返回該服務的所有實例,and 然后調用方應該用哪個服務呢?

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

Netflix ribbon介紹

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

1、負載均衡

2、故障容錯

3、支持多種協議(HTTP, TCP, UDP)

4、緩存

在maven項目中,可以使用以下方式引入:

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

Netflix ribbon例子

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

1、創建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/

然后啟動,界面如下:

2、創建服務端工程

首先,建一個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

3、創建客戶端工程

注意客戶端工程需要添加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幾次,結果如下,,

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

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

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

然后可以再啟動或刪除幾個后端服務,如啟動一個9093端口,在測試一下,9093端口對應的服務也可以訪問到,效果同樣OK。。。

那么,如果只想寫死,從幾個服務中選擇,而不是自動變動呢,可以在客戶端用以下配置,那么就會一直從這三個服務中選取了。

server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM