Spring Cloud:服務消費(Ribbon)【Dalston版】

Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基於HTTP和TCP的客戶端負載均衡器。它能夠經過在客戶端中配置ribbonServerList來設置服務端列表去輪詢訪問以達到均衡負載的做用。web

當Ribbon與Eureka聯合使用時,ribbonServerList會被DiscoveryEnabledNIWSServerList重寫,擴展成從Eureka註冊中心中獲取服務實例列表。同時它也會用NIWSDiscoveryPing來取代IPing,它將職責委託給Eureka來肯定服務端是否已經啓動。spring

而當Ribbon與Consul聯合使用時,ribbonServerList會被ConsulServerList來擴展成從Consul獲取服務實例列表。同時由ConsulPing來做爲IPing接口的實現。apache

咱們在使用Spring Cloud Ribbon的時候,不管是與Eureka仍是Consul結合,都會在引入Spring Cloud Eureka或Spring Cloud Consul依賴的時候經過自動化配置來加載上述所說的配置內容,因此咱們能夠快速在Spring Cloud中實現服務間調用的負載均衡。app

實戰:負載均衡

利用以前博客中構建的eureka-server做爲服務註冊中心、eureka-client做爲服務提供者做爲基礎,構建項目,命名爲:eureka-consumer-ribbon。在pom.xml中增長下面的依賴:maven

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>springCloud</groupId>
    <artifactId>eureka-consumer-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>spring-boot

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-actuator</artifactId>
        </dependency>
    </dependencies>ui

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <profiles>
        <profile>
            <id>jdk-1.8</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
            </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
        </profile>
    </profiles>
</project>spa

  • 修改應用主類。爲RestTemplate增長@LoadBalanced註解:
  • package com.springCloud;

    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;

    @SpringBootApplication
    @EnableDiscoveryClient
    public class ApplicationRibbon {
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }

        public static void main(String[] args) {
            new SpringApplicationBuilder(ApplicationRibbon.class).web(true).run(args);
        }
    }

  • 修改Controller。去掉原來經過LoadBalancerClient選取實例和拼接URL的步驟,直接經過RestTemplate發起請求。

package com.springCloud.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
public class DcController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/consumer")
    public String dc() {
        return restTemplate.getForObject("http://eureka-client/dc", String.class);
    }
}
紅色部分:使用這種方式能夠調用成功的緣由:

Spring Cloud Ribbon有一個攔截器,它可以在這裏進行實際調用的時候,自動的去選取服務實例,並將實際要請求的IP地址和端口替換這裏的服務名,從而完成服務接口的調用。

相關文章
相關標籤/搜索