自定义Ribbon规则--客户端写法

本文涉及的产品
网络型负载均衡 NLB,每月750个小时 15LCU
传统型负载均衡 CLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介: 自定义Ribbon规则--客户端写法

代码结构


88b9988b40447cb37c7e3c492d49867f.png


controller的代码


package com.demo.springcloud.controller;
import com.demo.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
    @Autowired
    private RestTemplate restTemplate;
//  private static final String REST_URI_PREFIX = "http://localhost:8001";
    private static final String REST_URI_PREFIX = "http://springcloud-provider-dept";
    @GetMapping("/consumer/dept/queryAll")
    public List<Dept> queryAll(){
        return restTemplate.getForObject(REST_URI_PREFIX+"/dept/queryAll", List.class);
    }
}


springcloud-provider-dep是服务提供者的id:

1dc618a0ed9580ce8bfa6facb208c08f.png


自定义配置文件


5d4c6812c8535adbb050f4ddf2e1bce8.png


自定义的规则:


//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.demo.springcloud.myRule;
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;
/**
 * 自定义的规则:
 * 每个服务走5次,然后才切换到另一个服务上
 */
public class HflRandomRule extends AbstractLoadBalancerRule {
    private int total;
    private int curentIndex;
    public HflRandomRule() {
    }
//    @SuppressWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"})
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        } else {
            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;
                }
                if(total<5){
                    server = upList.get(curentIndex);
                }else{
                    total = 0;
                    curentIndex ++;
                    if(curentIndex > upList.size()){
                        curentIndex = 0;
                    }
                    server = upList.get(curentIndex);
                }
                total++;
                if (server == null) {
                    Thread.yield();
                } else {
                    if (server.isAlive()) {
                        return server;
                    }
                    server = null;
                    Thread.yield();
                }
            }
            return server;
        }
    }
    public Server choose(Object key) {
        return this.choose(this.getLoadBalancer(), key);
    }
    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }
}


application.yml


46a9d80a6e05e4e3b19d57a0ee70bcdf.png


效果如下:



66ba272a0bfc97be54a5fa679e3d5482.png



相关实践学习
SLB负载均衡实践
本场景通过使用阿里云负载均衡 SLB 以及对负载均衡 SLB 后端服务器 ECS 的权重进行修改,快速解决服务器响应速度慢的问题
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
Yii2.0框架中如何进行路由设置?它支持哪些路由规则?
Yii2.0框架中如何进行路由设置?它支持哪些路由规则?
517 0
|
3月前
|
存储 前端开发 JavaScript
springboot中路径默认配置与重定向/转发所存在的域对象
Spring Boot 提供了简便的路径默认配置和强大的重定向/转发机制,通过合理使用这些功能,可以实现灵活的请求处理和数据传递。理解并掌握不同域对象的生命周期和使用场景,是构建高效、健壮 Web 应用的关键。通过上述详细介绍和示例,相信读者能够更好地应用这些知识,优化自己的 Spring Boot 应用。
83 3
|
9月前
自定义Ribbon规则--客户端写法
自定义Ribbon规则--客户端写法
|
9月前
|
Java 数据安全/隐私保护
Filter概述、执行流程、拦截路径配置及过滤器链
Filter概述、执行流程、拦截路径配置及过滤器链
111 0
|
9月前
|
数据库 数据安全/隐私保护
SpringSecurity请求授权规则配置与注解使用说明
SpringSecurity请求授权规则配置与注解使用说明
94 0
过滤器简介--操作步骤--过滤器生命周期--过滤器匹配规则-- 过滤器链
过滤器简介--操作步骤--过滤器生命周期--过滤器匹配规则-- 过滤器链
92 0
|
存储 XML 消息中间件
filter功能演示-鉴权、声明缓存
filter功能演示-鉴权、声明缓存
183 0
|
负载均衡 算法 Java
Ribbon默认负载均衡规则替换为NacosRule
Ribbon默认负载均衡规则替换为NacosRule
121 0
|
前端开发 Java 数据库
SpringBoot之自动配置类的解析和过滤机制
1.提炼三句话 整体来讲Spring Boot是通过条件注解、条件评估器和自动配置导入器等机制来实现自动配置的。 条件评估器来判断是否需要加载某个自动配置类。条件评估器通常被定义在“org.springframework.boot.autoconfigure.condition”包中,例如,ClassCondition、BeanCondition、MissingBeanCondition、WebApplicationCondition等 条件注解来判断是否需要加载某个自动配置类。条件注解通常被定义在“org.springframework.boot.autoconfigure.conditi
191 0
|
应用服务中间件
Servlet中的生命周期、基础配置、三种匹配规则、缺省Servlet以及路径问题
Servlet中的生命周期、基础配置、三种匹配规则、缺省Servlet以及路径问题
100 0