一、Ribbon全局配置
@RibbonClients注解配置导入全局Ribbon客户端默认设置
public class GlobalRibbonConfig {//负载均衡策略@Beanpublic IRule rule() {return new MicroServiceLoadBalanceRule();}//负载均衡器@Beanpublic <E extends Server> ILoadBalancer loadBalancer(IClientConfig config, ServerList<E> serverList, ServerListFilter<E> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {return new MicroServiceLoadBalancer<>(config, rule, ping, serverList, serverListFilter, serverListUpdater);}//IPing@Beanpublic IPing iping() {return new DummyPing();}//服务列表@Beanpublic ServerList<NacosServer> serverList(NacosDiscoveryProperties properties) {return new NacosServerList(properties);}//服务列表过滤器@Beanpublic ServerListSubsetFilter serverListFilter() {ServerListSubsetFilter filter = new ServerListSubsetFilter();return filter;}}@RibbonClients(defaultConfiguration = GlobalRibbonConfig.class)
public class GlobalRibbonClientConfig {}
二、单个服务Ribbon配置
@RibbonClient配置导入单个服务ribbon客户端配置
public class MicroRibbonConfig {@Beanpublic IPing iping() {return new DummyPing();}@Beanpublic <E extends Server> ILoadBalancer loadBalancer(IClientConfig config, ServerList<E> serverList, ServerListFilter<E> serverListFilter, IRule rule, IPing ping, ServerListUpdater serverListUpdater) {return new MicroServiceLoadBalancer<E>(config, rule, ping, serverList,serverListFilter, serverListUpdater);}@Beanpublic IRule rule() {return new MicroServiceLoadBalanceRule();}//服务列表@Beanpublic ServerList<NacosServer> serverList(NacosDiscoveryProperties properties) {return new NacosServerList(properties);}//服务列表过滤器@Beanpublic ServerListSubsetFilter serverListFilter() {ServerListSubsetFilter filter = new ServerListSubsetFilter();return filter;}
}@RibbonClient(name = "micro-service", configuration = MicroRibbonConfig.class)
public class RibbonClientConfig {}