SpringCloud(四)——Ribbon

SpringCloud(四)——Ribbon

Ribbon簡介

  • 負載均衡框架,支持可插拔式的負載均衡規則
  • 支持多種協議,如HTTP,UDP等
  • 提供負載均衡客戶端

Ribbon子模塊

  • ribbon-core:核心,主要包含負載均衡器java

  • ribbon-eureka:爲Eureka客戶端提供負載均衡的實現類web

  • ribbon-httpclient:對HttpClient進行了封裝,成爲了一個具備負載均衡功能的REST客戶端spring

負載均衡器

  • 負載均衡器只少須要提供如下兩種功能
    • 維護各個服務器的信息,如經過負載均衡器來決定請求去哪一個服務器進行處理
    • 根據特定的邏輯規則選擇服務器,如A服務器處理70%的請求,B服務器處理剩下的30%的請求

Ribbon負載均衡器——三大子模塊

  1. Rule:規則,經過規則來決定請求訪問那臺服務器
  2. Ping
  3. ServerList:服務列表,全部服務器列表

第一個Ribbon程序

程序結構圖以下apache

1-1

建立兩個項目,客戶端ribbon-client,服務端ribbon-servicejson

ribbon-service源代碼服務器

pom.xmlapp

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.zgy.cloud</groupId>
	<artifactId>ribbon-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>ribbon-service</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.14.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

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

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


</project>

RibbonServiceApplication.java負載均衡

package com.zgy.cloud.ribbonservice;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import java.util.Scanner;

@SpringBootApplication
public class RibbonServiceApplication {

	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String port = scanner.next();
        new SpringApplicationBuilder(RibbonServiceApplication.class).properties("server.port="+port).run(args);
	}
}

Person.java框架

package com.zgy.cloud.ribbonservice;

public class Person {

    private Integer id;
    private String name;
    private String message;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

MyController.javamaven

package com.zgy.cloud.ribbonservice;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MyController {

    @GetMapping("/person")
    public Person getPerson(HttpServletRequest request){
        Person person = new Person();
        person.setId(1);
        person.setName("ZGY");
        person.setMessage(request.getRequestURL().toString());

        return person;
    }
}

啓動兩個服務,在控制檯輸入第一個服務的端口爲8080,第二個服務的端口爲8081,而後分別訪問兩個服務,圖以下:

端口爲8080的服務

1532534921064

端口爲8081的服務

1532534946696

ribbon-client源代碼

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zgy.cloud</groupId>
    <artifactId>ribbon-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ribbon-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

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

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

        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.10</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.ribbon</groupId>
            <artifactId>ribbon-loadbalancer</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.archaius</groupId>
            <artifactId>archaius-core</artifactId>
            <version>0.7.4</version>
        </dependency>
    </dependencies>

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


</project>

RibbonClientApplication.java

package com.zgy.cloud.ribbonclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RibbonClientApplication {

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

TestRibbon.java

package com.zgy.cloud.ribbonclient;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;
import java.net.URI;

public class TestRibbon {

    public static void main(String[] args) throws Exception{

        ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
        RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/person")).build();
        for (int i = 0; i < 10; i++)  {
            HttpResponse response = client.executeWithLoadBalancer(request);
            String json = response.getEntity(String.class);
            System.out.println(json);
        }
    }
}

運行TestRibbon.java的mian方法,獲得以下圖結果

1532608312228

在控制檯,咱們輸出的信息,message屬性的端口號是輪詢的,8080和8081之間切換的

ribbon配置

前面TestRibbon.java代碼中,咱們是把配置寫在代碼中的,代碼以下

ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon.listOfServers", "localhost:8080,localhost:8081");

其實咱們能夠將這段配置寫在一個xxx.properties文件中,配置格式爲

客戶端名稱.命名空間.屬性 = 值

sample-client.ribbon.listOfServers = localhost:8080,localhost:8081

ribbon讀取properties文件自行解決

相關文章
相關標籤/搜索