1. 程式人生 > 程式設計 >SpringCloud Ribbon負載均衡例項解析

SpringCloud Ribbon負載均衡例項解析

這篇文章主要介紹了SpringCloud Ribbon負載均衡例項解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Spring Cloud集成了Ribbon,結合Eureka,可實現客戶端的負載均衡。

下面實現一個例子,結構下圖所示。

一、伺服器端

1、建立專案

開發工具:IntelliJ IDEA 2019.2.3

IDEA中建立一個新的SpringBoot專案,名稱為“cloud-server”,SpringBoot版本選擇2.1.10,在選擇Dependencies(依賴)的介面勾選Spring Cloud Discovert ->

Eureka Server,建立完成後的pom.xml配置檔案自動新增SpringCloud最新穩定版本依賴,當前為Greenwich.SR3。

pom.xml完整內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>cloud-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>cloud-server</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

2、修改配置application.yml

server:
 port: 8761
eureka:
 client:
  register-with-eureka: false
  fetch-registry: false

3、修改啟動類程式碼CloudServerApplication.java

增加註解@EnableEurekaServer

package com.example.cloudserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class CloudServerApplication {

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

}

二、服務提供者

1、建立專案

IDEA中建立一個新的SpringBoot專案,除了名稱為“cloud-provider”,其它步驟和上面建立伺服器端一樣。

2、修改配置application.yml

spring:
 application:
  name: cloud-provider
eureka:
 instance:
  hostname: localhost
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

3、修改啟動類程式碼CloudProviderApplication.java

增加註解@EnableEurekaClient;

讓類在啟動時讀取控制檯輸入,決定使用哪個埠啟動伺服器;

增加一個測試用的控制器方法。

package com.example.cloudprovider;

//import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Scanner;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class CloudProviderApplication {

  public static void main(String[] args) {
    //SpringApplication.run(CloudProviderApplication.class,args);
    Scanner scan = new Scanner(System.in);
    String port = scan.nextLine();
    new SpringApplicationBuilder(CloudProviderApplication.class).properties("server.port=" + port).run(args);
  }

  @RequestMapping("/")
  public String index(HttpServletRequest request) {
    return request.getRequestURL().toString();
  }
}

三、服務呼叫者

1、建立專案

IDEA中建立一個新的SpringBoot專案,除了名稱為“cloud-invoker”,其它步驟和上面建立伺服器端一樣。

2、修改配置application.yml

server:
 port: 9000
spring:
 application:
  name: cloud-invoker
eureka:
 instance:
  hostname: localhost
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

3、修改啟動類程式碼CloudInvokerApplication.java

增加註解@EnableDiscoveryClient。

package com.example.cloudinvoker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudInvokerApplication {

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

}

4、配置Ribbon有2種方式:使用程式碼、使用配置檔案

方式一:使用程式碼

(1)新建一個自定義負載規則類MyRule.java

Ribbon的負載均衡器介面定義了伺服器的操作,主要是用於進行伺服器選擇。

呼叫ILoadBalancer的getAllServers方法可以返回全部伺服器,這裡只返回第一個伺服器。

package com.example.cloudinvoker;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

import java.util.List;

public class MyRule implements IRule {

  private ILoadBalancer iLoadBalancer;

  @Override
  public Server choose(Object o) {
    List<Server> servers = iLoadBalancer.getAllServers();
    System.out.println("自定義伺服器規則類,輸出伺服器資訊:");
    for(Server s: servers){
      System.out.println(" " + s.getHostPort());
    }
    return servers.get(0);
  }

  @Override
  public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
    this.iLoadBalancer = iLoadBalancer;
  }

  @Override
  public ILoadBalancer getLoadBalancer() {
    return this.iLoadBalancer;
  }
}

(2)新建一個Ping類MyPing.java

負載均衡器中提供了Ping機制,每隔一段時間去Ping伺服器,判斷伺服器是否存活。

該工作由IPing介面的實現類負責。

package com.example.cloudinvoker;

import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.Server;

public class MyPing implements IPing {

  @Override
  public boolean isAlive(Server server) {
    System.out.println("自定義Ping類,伺服器資訊:" + server.getHostPort() + ",狀態:" + server.isAlive());
    return true;
  }
}

(3)新建配置類MyConfig.java

package com.example.cloudinvoker.config;

import com.example.cloudinvoker.MyPing;
import com.example.cloudinvoker.MyRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;

public class MyConfig {
  @Bean
  public IRule getRule(){
    return new MyRule();
  }
  @Bean
  public IPing getPing(){
    return new MyPing();
  }
}

(4)新建配置類CloudProviderConfig.java

package com.example.cloudinvoker.config;

import org.springframework.cloud.netflix.ribbon.RibbonClient;

@RibbonClient(name = "cloud-provider",configuration = MyConfig.class)
public class CloudProviderConfig {
}

方式二:使用配置檔案

把方式一的兩個配置類註釋掉,在application.yml的最後面新增下面配置

cloud-provider:
 ribbon:
  NFLoadBalancerRuleClassName: com.example.cloudinvoker.MyRule
  NFLoadBalancerPingClassName: com.example.cloudinvoker.MyPing
  listOfServers: http://localhost:8080/,http://localhost:8081/

5、新增控制器 InvokerController.java

package com.example.cloudinvoker;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Configuration
public class InvokerController {

  @LoadBalanced
  @Bean
  public RestTemplate getRestTemplate(){
    return new RestTemplate();
  }

  @RequestMapping(value="/router",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
  public String router(){
    RestTemplate restTemplate = getRestTemplate();
    //根據名稱呼叫服務
    String json = restTemplate.getForObject("http://cloud-provider/",String.class);
    return json;
  }
}

四、測試

1、啟動伺服器端。

2、啟動兩個服務提供者,在控制檯中分別輸入8080和8081啟動。

3、啟動服務呼叫者。

4、瀏覽器訪問http://localhost:9000/router,多次重新整理頁面,結果都是:

http://localhost:8081/

服務呼叫者專案IDEA控制檯定時輸出:

自定義伺服器規則類,輸出伺服器資訊:
 localhost:8081
 localhost:8080
自定義Ping類,伺服器資訊:localhost:8081,狀態:true
自定義Ping類,伺服器資訊:localhost:8080,狀態:true

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。