修改Ribbon默认配置

场景

在spring cloud gateway中,新增了负载均衡策略(修改同理),大多情况是使用“service.ribbon.NFLoadBalancerRuleClassName=IRule.class”进行配置,这种方式只针对单个服务,因为我们服务比较多,单个服务配置太过繁琐,所以想通过修改Ribbon的默认配置达到目的。

方案

通过查找文档:Spring Cloud Netflix

发现@RibbonClients标签,是对Ribbon的默认配置修改。

支持修改下列配置

返回值方法名默认值

IClientConfig

ribbonClientConfig

DefaultClientConfigImpl

IRule

ribbonRule

ZoneAvoidanceRule

IPing

ribbonPing

DummyPing

ServerList<Server>

ribbonServerList

ConfigurationBasedServerList

ServerListFilter<Server>

ribbonServerListFilter

ZonePreferenceServerListFilter

ILoadBalancer

ribbonLoadBalancer

ZoneAwareLoadBalancer

ServerListUpdater

ribbonServerListUpdater

PollingServerListUpdater

使用格式如下,也可以参考org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration中的方法。

    @Bean
    public 返回类型 方法名() {
        return new 默认值对象();//可以用继承实现类型
    }

实现如下:

  • 新建默认配置,设置ribbonRule方法设置默认负载规则。
    
    import com.lizz.loadbalancer.VersionRoundRobinRule;
    import com.netflix.client.config.IClientConfig;
    import com.netflix.loadbalancer.IRule;
    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @description: 调整ribbon默认配置
     * 原始默认org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration
     * @author: lizz
     */
    @Configuration(proxyBeanMethods = false)
    public class RibbonConfiguration {
        @RibbonClientName
        private String name = "client";
    
        @Bean
        public IRule ribbonRule() {
            //自定义负载规则
            return new VersionRoundRobinRule();
        }
    
        @Bean
        @ConditionalOnMissingBean
        public IClientConfig ribbonClientConfig() {
            DefaultClientConfigImpl config = new DefaultClientConfigImpl();
            config.loadProperties(this.name);
            config.set(CommonClientConfigKey.ConnectTimeout, DEFAULT_CONNECT_TIMEOUT);
            config.set(CommonClientConfigKey.ReadTimeout, DEFAULT_READ_TIMEOUT);
            config.set(CommonClientConfigKey.GZipPayload, DEFAULT_GZIP_PAYLOAD);
            config.set(CommonClientConfigKey.ServerListRefreshInterval, 5000);
            return config;
        }
    }
    
  • 使用@RibbonClients启用Ribbon默认配置
    import com.lizz.gateway.config.RibbonConfiguration;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.ribbon.RibbonClients;
    
    
    /**
     * 程序启动类
     *
     * @author: lizz
     */
    @SpringBootApplication
    @RibbonClients(defaultConfiguration = RibbonConfiguration.class)
    public class GatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class, args);
        }
    }
    注意:
  • RibbonClients设置的必须为@Configuration类
  • 该@Configuration类不能被@ComponentScan引用,不能与@SpringBootApplication类在同一个包下。否则将作为共享方法使用,无法针对单个服务配置。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lizz666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值