SpringCloud Ribbon 自定义负载均衡算法


程序结构:

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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.9.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.zyugroupId>
    <artifactId>consumer_serviceartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>consumer_servicename>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Greenwich.SR3version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

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

project>

方式一

在springboot主程序扫描的包外定义配置类,然后为springboot主程序添加 @RibbonClient 注解引入配置类

Springboot 主程序:

package com.zyu;

import com.ribbonconfig.MySelfRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@SpringBootApplication
//该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务
@EnableDiscoveryClient
//在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
@RibbonClient(value = "user-service", configuration = {MySelfRule.class})
public class ConsumerServiceApplication {

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

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

Rule配置文件类,配置类不应该在SpringBoot的包路径下通过@RibbonClient 注解加载:

package com.ribbonconfig;

import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MySelfRule {
    @Bean
    public IRule getIRule() {
        /**
         * 修改Ribbon 负载均衡策略
         */
        //return new RandomRule();   //默认轮询,自定义为随机

        /**
         * 自定义 Ribbon 负载均衡策略
         */
        return new MyRule();
    }
}

自定义LoadBalance:

package com.ribbonconfig;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;

import java.util.List;
import java.util.Random;


public class MyRule extends AbstractLoadBalancerRule  {

    /**
     * 服务选择算法,随机调用
     * @param lb
     * @param o
     * @return
     */
    public Server choose(ILoadBalancer lb, Object o) {
        // 获取服务器列表
        List servers = lb.getAllServers();
        // 生产随机数
        Random r = new Random();
        int rand = r.nextInt(10);
        System.out.println("随机数: " + rand);
        if(rand > 7){
            return getServerByPort(servers, 9091);
        } else if (rand > 3) {
            return getServerByPort(servers, 9092);
        }else{
            return getServerByPort(servers, 9093);
        }
    }

    /**
     * 根据传入的端口号,返回服务对象
     * @param servers
     * @param port
     * @return
     */
    private Server getServerByPort(List servers, int port){
        for(Server s : servers){
            if(s.getPort() == port){
                return s;
            }
        }
        return null;
    }


    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }

    @Override
    public Server choose(Object o) {
        return choose(getLoadBalancer(), o);
    }
}

方式二

application.yml文件配置方式

#@RibbonClient(name = "user-service") 与name相同,表示针对该微服务使用自定义负载均衡规则
user-service:
  ribbon:
    NFLoadBalancerRuleClassName: com.ribbonconfig.MyRule

配置的优先级

配置文件的优先级 > java代码的配置方式 > netflix自定义的配置方式