Ribbon负载均衡自定义实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012129558/article/details/83755163

Ribbon简介
负载均衡框架,支持可插拔式的负载均衡规则
支持多种协议,如HTTP、UDP等
提供负载均衡客户端

Ribbon子模块
ribbon-core
ribbon-eureka
ribbon-httpclient

负载均衡器组件
一个负载均衡器,至少提供以下功能:
要维护各个服务器的IP等信息
根据特定逻辑选取服务器
为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块:
Rule
Ping
ServerList

第一个Ribbon程序

实现第一个Ribbon程序
https://gitee.com/wangxuewaii/codes/ilk7sgcx2eozpbw04fh8t62

Ribbon负载均衡器

package com.example.demo;

import java.util.ArrayList;

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

import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.Server;

@SpringBootApplication
public class RibbonUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonUserApplication.class, args);
        BaseLoadBalancer lb = new BaseLoadBalancer();
        ArrayList<Server> servers = new ArrayList<Server>();
        servers.add(new Server("localhost", 8080));
        servers.add(new Server("localhost", 8081));
        lb.addServers(servers);
        for (int i = 0; i < 10; i++) {
            Server server = lb.chooseServer(null);
            System.out.println(server);

        }
    }
}
<?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.example</groupId>
    <artifactId>ribbon-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.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>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

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

自定义负载均衡器

继承IRule

package com.example.demo;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

public class MyRule implements IRule {

    private ILoadBalancer lb;

    @Override
    public Server choose(Object arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public ILoadBalancer getLoadBalancer() {
        // TODO Auto-generated method stub
        return this.lb;
    }
    @Override
    public void setLoadBalancer(ILoadBalancer lb) {
        this.lb=lb;
    }

}
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.Server;

@SpringBootApplication
public class RibbonUserApplication {

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

        BaseLoadBalancer lb = new BaseLoadBalancer();
        MyRule rule = new MyRule();
        rule.setLoadBalancer(lb);
        lb.setRule(rule);

        List<Server> servers = new ArrayList<Server>();
        servers.add(new Server("localhost", 8080));
        servers.add(new Server("localhost", 8081));
        lb.addServers(servers);
        for (int i = 0; i < 10; i++) {
            Server s = lb.chooseServer(null);
            System.out.println(s);
        }
    }
}
自定义的随机数大于7的就使用8081服务器,小于7就用8080服务器 
替换上面的MyRule ,别的不变

package com.example.demo;
import java.util.List;
import java.util.Random;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

public class MyRule implements IRule {

    private ILoadBalancer lb;
    @Override
    public Server choose(Object arg0) {
        //自定义的随机数大于7的就使用8081服务器,小于7就用8080服务器
        Random r = new Random();
        int rNum = r.nextInt(10);
        List<Server> servers = lb.getAllServers();
        if (rNum > 7) {
            return getServerByPort(servers, 8081);
        }
        return getServerByPort(servers, 8080);
    }

    private Server getServerByPort(List<Server> servers, int port) {
        for (Server s : servers) {

            if (s.getPort() == port) {
                return s;
            }

        }
        return null;
    }
    @Override
    public ILoadBalancer getLoadBalancer() {
        // TODO Auto-generated method stub
        return this.lb;
    }
    @Override
    public void setLoadBalancer(ILoadBalancer lb) {
        this.lb = lb;
    }

}

ribbon自定义负载均衡器实现,调用真正的服务实现

https://gitee.com/wangxuewaii/codes/knp01ylhsrj7x5miqb3uc49

Ribbon内置的负载均衡规则
RoundRobinRule
AvailabilityFilteringRule
WeightedResponseTimeRule
ZoneAvoidanceRule
BestAvailableRule
RandomRule
RetryRule

扫描二维码关注公众号,回复: 3975318 查看本文章

猜你喜欢

转载自blog.csdn.net/u012129558/article/details/83755163