Ribbon:自定义负载均衡

自定义负载均衡算法

java 复制代码
package com.kuang.myconfig;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;


public class KuangRandomRule extends AbstractLoadBalancerRule {

    //每个机器,访问5次,换下一个服务

//total=0,默认=0,如果等于5 我们指向下一个服务节点

    //index=0 ,默认0,如果total=5 index++, 若index>3 则index=0
    private int total=0;//被调用的次数
    private int currentIndex=0;//当前是谁在提供服务

    //   @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            List<Server> upList = lb.getReachableServers();//获得活着的服务
            List<Server> allList = lb.getAllServers();//获得全部的服务

            int serverCount = allList.size();
            if (serverCount == 0) {

                return null;
            }

//            int index = chooseRandomInt(serverCount);//生成区间随机数
//            server = upList.get(index);//活着的服务去获得某一个

            //=====================================================
         if (total<5){
             server = upList.get(currentIndex);
             total++;
         }else {
             total=1;
             currentIndex++;
             if (currentIndex>upList.size()-1){
                 currentIndex=0;
             }
             server  =  upList.get(currentIndex);
         }




            //=====================================================


            if (server == null) {

                Thread.yield();
                continue;
            }

            if (server.isAlive()) {
                return (server);
            }

            // Shouldn't actually happen.. but must be transient or a bug.
            server = null;
            Thread.yield();
        }

        return server;

    }

    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }

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

	@Override
	public void initWithNiwsConfig(IClientConfig clientConfig) {
		// TODO Auto-generated method stub
		
	}
}

Myconfig

java 复制代码
package com.kuang.myconfig;

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

@Configuration
public class KuangRule {


    @Bean
    public IRule myRule(){
        return new KuangRandomRule();//默认是轮询;现在我们自定义为KuangRandomRule()
    }
}

主启动类

java 复制代码
package com.kuang.springcloud;

import com.kuang.myconfig.KuangRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

//Ribbon和Eureka 整合以后 ,客户端可以直接调用,不用关心IP地址和端口号~
@SpringBootApplication
@EnableEurekaClient
//在微服务启动的时候就能取加载我们自定义Ribbon类
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = KuangRule.class )
public class DeptConsumer80 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer80.class,args);
    }
}

开启

复制代码
Ribbon  实现负载均衡
相关推荐
牛奔12 小时前
docker compose up 命令,默认配置文件自动查找规则
java·spring cloud·docker·容器·eureka
云飞云共享云桌面12 小时前
非标自动化工厂的设计云桌面为什么要选云飞云智能共享云桌面?
大数据·运维·服务器·网络·自动化·负载均衡
华如锦12 小时前
四:从零搭建一个RAG
java·开发语言·人工智能·python·机器学习·spring cloud·计算机视觉
遇见火星16 小时前
RabbitMQ 高可用:HAProxy 负载均衡实战指南
分布式·消息队列·rabbitmq·负载均衡·haproxy
笑笑�17 小时前
OpenResty + nginx_upstream_check_module 构建主动健康检查模块
运维·nginx·负载均衡·openresty
鸽鸽程序猿18 小时前
【JavaEE】【SpringCloud】负载均衡_LoadBalancer
spring cloud·java-ee·负载均衡
Coder_Boy_18 小时前
基于SpringAI的智能推荐影视平台设计和业务思路
大数据·人工智能·spring boot·spring cloud·langchain
齐 飞18 小时前
Spring Cloud Alibaba快速入门-Gateway
spring cloud·微服务·gateway
lhrimperial19 小时前
微服务架构深度解析-Spring Cloud Alibaba技术体系
spring cloud·微服务·架构
后端小张19 小时前
【JAVA 进阶】Spring Boot 中 AOP 切面编程全解析:从基础到实战进阶
java·开发语言·人工智能·spring boot·后端·spring·spring cloud