Spring Cloud 之 Ribbon: 負載均衡

前一篇講了服務註冊和發現,沒有看過的朋友先看上一篇,這篇是在上一篇基礎上展開。java

Ribbon簡介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.git

<!-- more -->web

Ribbon是負載均衡客戶端,能夠很好的控制HTTP和TCP客戶端的行爲。 Feign已經集成了Ribbon。spring

Spring Cloud Netflix默認爲ribbon(BeanType beanName:ClassName)提供如下bean:apache

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl瀏覽器

  • IRule ribbonRule: ZoneAvoidanceRuleapp

  • IPing ribbonPing: NoOpPing負載均衡

  • ServerList<Server> ribbonServerList: ConfigurationBasedServerListmaven

  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilteride

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

準備工做

這篇文章基於上一篇文章項目,啓動eureka-server項目;啓動eureka-client項目,端口爲8040; 將eureka-client的配置文件端口改成8041,並啓動,同一個項目修改端口號,啓動多個實例,只須要在IDEA中勾選Allow parallel run

此時你會發現註冊服務中註冊了兩個eureka-client實例,至關於起了2個節點的集羣。 訪問http://localhost:9090:

新建一個服務消費者

使用Spring Initializr新建一個項目,取名爲ribbon-service, 在Spring Cloud Discovery中勾選Eureka Discovery Client,在Spring Cloud Routing中勾選Ribbon,在Web中勾選Spring Web:

建立成功後,項目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.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.noodles.mars</groupId>
    <artifactId>ribbon-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ribbon-service</name>
    <description>Ribbon Service</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-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

ribbon-service配置服務中心地址、應用名、端口,配置文件內容:

server:
  port: 8050

spring:
  application:
    name: ribbon-service

eureka:
  client:
    service-url: 
      defaultZone: http://localhost:9090/eureka/

在項目啓動類上註解@EnableDiscoveryClient, 開啓向服務中心註冊;定義一個 RestTemplate Bean, 並註解@LoadBalanced開啓負載均衡功能:

@EnableEurekaClient
@SpringBootApplication
public class RibbonServiceApplication {

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

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

}

寫一個controller, 經過以前定義的RestTemplate來消費eureka-client服務的/hello接口,url中使用應用名,ribbon會根據應用名來選擇具體的服務實例,根據服務實例在請求的時候會用具體的url替換掉服務名:

@RestController
public class HelloController {

    private final RestTemplate restTemplate;

    @Autowired
    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);
    }
}

在瀏覽器上屢次訪問 http://localhost:8050/hello?name=Mars :

Hello, My name is Mars, I'm from port: 8040
Hello, My name is Mars, I'm from port: 8041

這說明負載均衡器已經工做了。

關注公衆號:JAVA九點半課堂,回覆【資料】獲取1T最新技術資料!

我的博客:http://blog.hxpgxt.cn

點擊進入源碼倉庫

相關文章
相關標籤/搜索