Ribbon 是一个客户端负载均衡器,它为你提供了对 HTTP 和 TCP 客户端行为的大量控制。Feign 已经使用了 Ribbon,因此,如果你使用
Ribbon 中的一个核心概念是命名客户端。每个负载均衡器都是一组组件的一部分,这些组件共同工作以按需联系远程服务器,并且该组有一个作为应用程序开发人员提供的名称(例如,通过使用
要在项目中包含 Ribbon,请使用 group ID 为
你可以通过使用
Spring Cloud 还允许你使用 @Configuration @RibbonClient(name = "custom", configuration = CustomConfiguration.class) public class TestConfiguration { }
在这种情况下,客户端由已经在
下表展示了 Spring Cloud Netflix 默认为 Ribbon 提供的 bean:
创建其中一种类型的 bean 并将其放置在 @Configuration protected static class FooConfiguration { @Bean public ZonePreferenceServerListFilter serverListFilter() { ZonePreferenceServerListFilter filter = new ZonePreferenceServerListFilter(); filter.setZone("myTestZone"); return filter; } @Bean public IPing ribbonPing() { return new PingUrl(); } }
前面示例中的 include 语句将
通过使用 @RibbonClients(defaultConfiguration = DefaultRibbonConfig.class) public class RibbonClientDefaultConfigurationTestsConfig { public static class BazServiceList extends ConfigurationBasedServerList { public BazServiceList(IClientConfig config) { super.initWithNiwsConfig(config); } } } @Configuration class DefaultRibbonConfig { @Bean public IRule ribbonRule() { return new BestAvailableRule(); } @Bean public IPing ribbonPing() { return new PingUrl(); } @Bean public ServerList<Server> ribbonServerList(IClientConfig config) { return new RibbonClientDefaultConfigurationTestsConfig.BazServiceList(config); } @Bean public ServerListSubsetFilter serverListFilter() { ServerListSubsetFilter filter = new ServerListSubsetFilter(); return filter; } } 从 1.2.0 版本开始,Spring Cloud Netflix 现在支持与 Ribbon 文档兼容的通过属性自定义 Ribbon 客户端。 这允许你在不同环境中启动时更改行为。 以下列表展示支持的属性:
要为名为 application.yml. users: ribbon: NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule 有关 Ribbon 提供的实现,请参阅 Ribbon 文档。
当 Eureka 与 Ribbon(即两者都在类路径上)一起使用时,
Eureka 是一种抽象远程服务器发现的方便方法,这样你就不必在客户端中硬编码它们的 URL。但是,如果你不喜欢用 Eureka,Ribbon 和 Feign 也可以。假设你已经为 "stores" 声明了 application.yml. stores: ribbon: listOfServers: example.com,google.com
将 application.yml. ribbon: eureka: enabled: false
你还可以直接使用 public class MyClass { @Autowired private LoadBalancerClient loadBalancer; public void doStuff() { ServiceInstance instance = loadBalancer.choose("stores"); URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort())); // ... do something with the URI } } 每个名为 client 的 Ribbon 都有一个 Spring Cloud 维护的相应子应用程序上下文。此应用程序上下文在向命名客户端发出第一个请求时被延迟加载。通过指定功能区客户端的名称,可以将这种延迟加载行为更改为在启动时急切地加载这些子应用程序上下文,如下例所示: application.yml. ribbon: eager-load: enabled: true clients: client1, client2, client3
如果将 application.yml. zuul: threadPool: useSeparateThreadPools: true 前面的示例导致在每个路由的 Hystrix 线程池中执行 HystrixCommands。
在这种情况下,默认的 application.yml. zuul: threadPool: useSeparateThreadPools: true threadPoolKeyPrefix: zuulgw
如果你需要提供自己的 com.netflix.loadbalancer.IRule.java. public interface IRule{ public Server choose(Object key); :
你可以提供一些 RequestContext.getCurrentContext() .set(FilterConstants.LOAD_BALANCER_KEY, "canary-test");
如果将具有 |