com.netflix.loadbalancer.WeightedResponseTimeRule Java Examples

The following examples show how to use com.netflix.loadbalancer.WeightedResponseTimeRule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RouteListener.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
/**
 * 监听EnvironmentChangeEvent 事件,更改相关环境变量
 * @param event
 */
@Override
@EventListener(EnvironmentChangeEvent.class)
public void onApplicationEvent(EnvironmentChangeEvent event) {
    try {
        LOGGER.info("environment change.");
        Map<String, Object> propertySource = Maps.newHashMap();
        if (!routeMatcher.match()) {
            LOGGER.info("this route rules does not match this instance.");
            return;
        }
        // 多条路由规则已先后顺序进行匹配
        FormulaRouteProperty formulaRouteProperty = routeMatcher.getMatchedFormulaRouteProperty();
        // 获取新的负载均衡策略
        String iRuleName = formulaRouteProperty.getLoadbalance();
        String destServiceName = formulaRouteProperty.getDestServiceName();
        IRule oldRule = springClientFactory.getInstance(destServiceName, IRule.class);
        if (oldRule instanceof WeightedResponseTimeRule) {
            // 关闭线程池
            ((WeightedResponseTimeRule) oldRule).shutdown();
        }
        // 清理ribbon 中 所有的client的负载均衡器配置,更改环境变量值,等待下次重新加载client的负载均衡配置
        springClientFactory.destroy();

        // 按照ribbon的规范,配置IRule
        String configClientRule = destServiceName + "." + CONFIG_NAMESPACE + "." + CONFIG_RULE_CLASS;

        propertySource.put(configClientRule, IRuleInfo.getRulePath(iRuleName));
        // 加入至环境变量中
        this.configurableEnvironment.getPropertySources().addFirst(new RoutePropertySource(ROUTE_PROPERTY_SOURCE,
                propertySource));
    } catch (Exception e) {
        LOGGER.error("refresh route rule exception: {}", e);
    }

}
 
Example #2
Source File: ResponseTimeWeightedRuleTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerWeights(){
    try{
        ConfigurationManager.loadPropertiesFromResources("sample-client.properties"); 

        ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon.NFLoadBalancerClassName", "com.netflix.loadbalancer.DynamicServerListLoadBalancer");
        ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon.NFLoadBalancerRuleClassName", "com.netflix.loadbalancer.WeightedResponseTimeRule");
        // shorter weight adjusting interval
        ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon." + WeightedResponseTimeRule.WEIGHT_TASK_TIMER_INTERVAL_CONFIG_KEY, "5000");
        ConfigurationManager.getConfigInstance().setProperty(
                "sample-client.ribbon.InitializeNFLoadBalancer", "true");       

        RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client"); 

        HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build(); 

        for (int i = 0; i < 20; i++) {
            client.executeWithLoadBalancer(request);
        }
        System.out.println(((AbstractLoadBalancer) client.getLoadBalancer()).getLoadBalancerStats());
        // wait for the weights to be adjusted
        Thread.sleep(5000);
        for (int i = 0; i < 50; i++) {
            client.executeWithLoadBalancer(request);
        }
        System.out.println(((AbstractLoadBalancer) client.getLoadBalancer()).getLoadBalancerStats());
    }
    catch (Exception e){
        e.printStackTrace();
    }
}
 
Example #3
Source File: RibbonConfiguration.java    From Mastering-Spring-Cloud with MIT License 4 votes vote down vote up
@Bean
public IRule ribbonRule() {
	return new WeightedResponseTimeRule();
}
 
Example #4
Source File: RibbonJerseyClientBuilder.java    From dropwizard-consul with Apache License 2.0 4 votes vote down vote up
/**
 * Builds a new {@link RibbonJerseyClient} with an existing Jersey Client and service discoverer
 *
 * @param name Client name
 * @param jerseyClient Jersey Client
 * @param serviceDiscoverer Service discoverer
 * @return new RibbonJerseyClient
 */
public RibbonJerseyClient build(
    final String name,
    final Client jerseyClient,
    final ConsulServiceDiscoverer serviceDiscoverer) {

  // dynamic server list that is refreshed from Consul
  final ConsulServerList serverList = new ConsulServerList(consul, serviceDiscoverer);

  // build a new load balancer based on the configuration
  final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
  clientConfig.set(CommonClientConfigKey.AppName, name);
  clientConfig.set(
      CommonClientConfigKey.ServerListRefreshInterval,
      Ints.checkedCast(configuration.getRefreshInterval().toMilliseconds()));

  final ZoneAwareLoadBalancer<Server> loadBalancer =
      LoadBalancerBuilder.newBuilder()
          .withClientConfig(clientConfig)
          .withRule(new WeightedResponseTimeRule())
          .withDynamicServerList(serverList)
          .buildDynamicServerListLoadBalancer();

  final RibbonJerseyClient client = new RibbonJerseyClient(loadBalancer, jerseyClient);

  environment
      .lifecycle()
      .manage(
          new Managed() {
            @Override
            public void start() throws Exception {
              // nothing to start
            }

            @Override
            public void stop() throws Exception {
              client.close();
            }
          });
  return client;
}
 
Example #5
Source File: RibbonConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public IRule ribbonRule() {
    return new WeightedResponseTimeRule();
}
 
Example #6
Source File: RibbonConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public IRule ribbonRule(IClientConfig config) {
    return new WeightedResponseTimeRule();
}