标签:ati zed ace src single 随机 cli lis color
@Configuration public class TestConfiguration { public IRule ribbonRule() { return new RandomRule();//随机负载 } }
其中:RibbonClient中name 微服务名称,configuration配置类
注意:configuration等于的TestConfiguration必须是@Configuration,但要注意它不在主应用程序上下文的@ComponentScan中,否则它将被所有@RibbonClients共享。如果使用@ComponentScan(或@SpringBootApplication),则需要采取措施以避免包含它(例如,将其放在单独的,不重叠的包中,或者指定要在@ComponentScan中显式扫描的包)。
方式1、TestConfiguration不放在spring boot启动类的当前包或子包中即可
方式2、如果TestConfiguration确实需要放在当前包,需要设置如下
增加注解
public @interface ExcludeFromComponentScan { }
将注解增加至TestConfiguration
@Configuration @ExcludeFromComponentScan public class TestConfiguration { public IRule ribbonRule() { return new RandomRule(); } }
将排除注解增加至启动类
@ComponentScan(excludeFilters= {@ComponentScan.Filter(type=FilterType.ANNOTATION,value=ExcludeFromComponentScan.class)})
多个可以@RibbonClient(name = "microservice-provider-user2", configuration = TestConfiguration.class)
此时只是自定义的按照新的规则负载,原有的还是按照默认的轮询方式使用
查看:@RibbonClient注解发现主要属性为 RibbonClientConfiguration,查看其实现
Spring Cloud Netflix默认为Ribbon提供以下Bean(BeanType beanName:ClassName):
IClientConfig
ribbonClientConfig: DefaultClientConfigImpl
IRule
ribbonRule: ZoneAvoidanceRule
IPing
ribbonPing: DummyPing
ServerList<Server>
ribbonServerList: ConfigurationBasedServerList
ServerListFilter<Server>
ribbonServerListFilter: ZonePreferenceServerListFilter
ILoadBalancer
ribbonLoadBalancer: ZoneAwareLoadBalancer
ServerListUpdater
ribbonServerListUpdater: PollingServerListUpdater
从1.2.0版开始,Spring Cloud Netflix现在支持使用属性自定义Ribbon客户端以与Ribbon文档兼容。
配置属性
<clientName>.ribbon.:
NFLoadBalancerClassName: should implement ILoadBalancer
NFLoadBalancerRuleClassName: should implement IRule
NFLoadBalancerPingClassName: should implement IPing
NIWSServerListClassName: should implement ServerList
NIWSServerListFilterClassName should implement ServerListFilter
注意:配置优先级高于代码自定义和默认配置,并且不会有代码方式的干扰
示例
users: #微服务名称
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
0404-Ribbon通过代码自定义配置、使用配置文件自定义Ribbon Client
标签:ati zed ace src single 随机 cli lis color
原文地址:https://www.cnblogs.com/bjlhx/p/8859088.html