本文整理汇总了Java中com.netflix.loadbalancer.LoadBalancerBuilder类的典型用法代码示例。如果您正苦于以下问题:Java LoadBalancerBuilder类的具体用法?Java LoadBalancerBuilder怎么用?Java LoadBalancerBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LoadBalancerBuilder类属于com.netflix.loadbalancer包,在下文中一共展示了LoadBalancerBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateLoadBalancer
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
private void updateLoadBalancer(List<Server> newServers) {
if (serviceServers == null) {
serviceServers = new HashSet<Server>();
}
if (newServers == null) {
newServers = new ArrayList<Server>();
}
//return if nothing changed
if ((serviceServers.isEmpty() && newServers.isEmpty())
|| (newServers.size() == serviceServers.size() && serviceServers.containsAll(newServers))) {
return;
}
serviceServers = new HashSet<Server>(newServers);
loadBalancerModificationLock.writeLock().lock();
if (loadBalancer != null) {
loadBalancer.shutdown();
}
loadBalancer = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(newServers);
for (EndpointClientCollection<?> lb : endpointMap.values()) {
lb.updateServers(newServers);
}
loadBalancerModificationLock.writeLock().unlock();
}
示例2: setup
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Before
public void setup() throws Exception {
zookeeperServer = new TestingServer(2181, true);
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("localhost", 2181, "/services");
zookeeperServiceDiscovery = new ZookeeperServiceDiscovery(zookeeperConfiguration);
zookeeperServiceDiscovery.register(new ZookeeperInstance("myApi", "localhost", 7080, Collections.emptyMap()));
zookeeperServiceDiscovery.register(new ZookeeperInstance("myApi", "localhost", 7081, Collections.emptyMap()));
zookeeperServiceDiscovery.register(new ZookeeperInstance("myApi", "localhost", 7082, Collections.emptyMap()));
ZookeeperServers zookeeperServers = new ZookeeperServers("myApi", zookeeperServiceDiscovery);
ILoadBalancer loadBalancer = LoadBalancerBuilder.newBuilder()
.withDynamicServerList(zookeeperServers)
.buildDynamicServerListLoadBalancer();
RibbonHttpClientRequestFactory ribbonHttpClientRequestFactory = new RibbonHttpClientRequestFactory(loadBalancer, new RibbonZookeeperExceptionHandler(zookeeperServiceDiscovery));
myApi = new RestifyProxyBuilder()
.client(ribbonHttpClientRequestFactory)
.target(MyApi.class, "http://myApi")
.build();
}
示例3: build
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
public ZoneAwareLoadBalancer<Server> build(String schemeName) {
String servicename = URI.create(schemeName).getHost();
log.info("creating a loadbalancer {} ", servicename);
final DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
clientConfig.setClientName(servicename);
clientConfig.set(CommonClientConfigKey.ServerListRefreshInterval, 300 * 1000);
return LoadBalancerBuilder.newBuilder().withClientConfig(clientConfig)
.withRule(new WeightedResponseTimeRule()).withDynamicServerList(factory.build(servicename))
.buildDynamicServerListLoadBalancer();
}
示例4: testFixedServerList
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Test
public void testFixedServerList() throws Exception {
List<RibbonServer> servers = new ArrayList<>();
servers.add(new RibbonServer("localhost", 9090));
servers.add(new RibbonServer("localhost", 9091));
ServerList<RibbonServer> list = new RibbonServiceCallStaticServerListStrategy(servers);
ZoneAwareLoadBalancer<RibbonServer> lb = LoadBalancerBuilder.<RibbonServer>newBuilder()
.withDynamicServerList(list)
.withRule(new RoundRobinRule()).buildDynamicServerListLoadBalancer();
Server server = lb.chooseServer();
assertEquals("localhost", server.getHost());
assertEquals(9091, server.getPort());
server = lb.chooseServer();
assertEquals("localhost", server.getHost());
assertEquals(9090, server.getPort());
}
示例5: testBuildWithDiscoveryEnabledNIWSServerList
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Test
public void testBuildWithDiscoveryEnabledNIWSServerList() {
IRule rule = new AvailabilityFilteringRule();
ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<DiscoveryEnabledServer>();
ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
.withDynamicServerList(list)
.withRule(rule)
.withServerListFilter(filter)
.buildDynamicServerListLoadBalancer();
assertNotNull(lb);
assertEquals(Lists.newArrayList(expected), lb.getAllServers());
assertSame(filter, lb.getFilter());
assertSame(list, lb.getServerListImpl());
Server server = lb.chooseServer();
// make sure load balancer does not recreate the server instance
assertTrue(server instanceof DiscoveryEnabledServer);
}
示例6: testBuildWithDiscoveryEnabledNIWSServerListAndUpdater
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Test
public void testBuildWithDiscoveryEnabledNIWSServerListAndUpdater() {
IRule rule = new AvailabilityFilteringRule();
ServerList<DiscoveryEnabledServer> list = new DiscoveryEnabledNIWSServerList("dummy:7001");
ServerListFilter<DiscoveryEnabledServer> filter = new ZoneAffinityServerListFilter<DiscoveryEnabledServer>();
ServerListUpdater updater = new PollingServerListUpdater();
ZoneAwareLoadBalancer<DiscoveryEnabledServer> lb = LoadBalancerBuilder.<DiscoveryEnabledServer>newBuilder()
.withDynamicServerList(list)
.withRule(rule)
.withServerListFilter(filter)
.withServerListUpdater(updater)
.buildDynamicServerListLoadBalancerWithUpdater();
assertNotNull(lb);
assertEquals(Lists.newArrayList(expected), lb.getAllServers());
assertSame(filter, lb.getFilter());
assertSame(list, lb.getServerListImpl());
assertSame(updater, lb.getServerListUpdater());
Server server = lb.chooseServer();
// make sure load balancer does not recreate the server instance
assertTrue(server instanceof DiscoveryEnabledServer);
}
示例7: testBuildWithArchaiusProperties
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Test
public void testBuildWithArchaiusProperties() {
Configuration config = ConfigurationManager.getConfigInstance();
config.setProperty("client1.niws.client." + Keys.DeploymentContextBasedVipAddresses, "dummy:7001");
config.setProperty("client1.niws.client." + Keys.InitializeNFLoadBalancer, "true");
config.setProperty("client1.niws.client." + Keys.NFLoadBalancerClassName, DynamicServerListLoadBalancer.class.getName());
config.setProperty("client1.niws.client." + Keys.NFLoadBalancerRuleClassName, RoundRobinRule.class.getName());
config.setProperty("client1.niws.client." + Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName());
config.setProperty("client1.niws.client." + Keys.NIWSServerListFilterClassName, ZoneAffinityServerListFilter.class.getName());
config.setProperty("client1.niws.client." + Keys.ServerListUpdaterClassName, PollingServerListUpdater.class.getName());
IClientConfig clientConfig = IClientConfig.Builder.newBuilder(NiwsClientConfig.class, "client1").build();
ILoadBalancer lb = LoadBalancerBuilder.newBuilder().withClientConfig(clientConfig).buildLoadBalancerFromConfigWithReflection();
assertNotNull(lb);
assertEquals(DynamicServerListLoadBalancer.class.getName(), lb.getClass().getName());
DynamicServerListLoadBalancer<Server> dynamicLB = (DynamicServerListLoadBalancer<Server>) lb;
assertTrue(dynamicLB.getServerListUpdater() instanceof PollingServerListUpdater);
assertTrue(dynamicLB.getFilter() instanceof ZoneAffinityServerListFilter);
assertTrue(dynamicLB.getRule() instanceof RoundRobinRule);
assertTrue(dynamicLB.getPing() instanceof DummyPing);
assertEquals(Lists.newArrayList(expected), lb.getAllServers());
}
示例8: FilterBackendLoadBalancer
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Autowired
FilterBackendLoadBalancer(
@Value("${gateway.filter.endPointsAsString}") String endPointsAsString) {
ImmutableSet<EndPoint> endPoints = ImmutableSet.copyOf(
new LoadBalancerConfiguration(endPointsAsString, "gateway.filter.endPointsAsString")
.getEndPoints());
List<Server> serverList = endPoints.stream()
.map(e -> new Server(e.getHost(), e.getPort()))
.collect(Collectors.toList());
loadBalancer = LoadBalancerBuilder.newBuilder()
.buildFixedServerListLoadBalancer(serverList);
}
示例9: ApiBackendLoadBalancer
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Autowired
ApiBackendLoadBalancer(
@Value("${gateway.guestbook.endPointsAsString}") String endPointsAsString) {
ImmutableSet<EndPoint> endPoints = ImmutableSet.copyOf(
new LoadBalancerConfiguration(endPointsAsString, "gateway.guestbook.endPointsAsString")
.getEndPoints());
List<Server> serverList = endPoints.stream()
.map(e -> new Server(e.getHost(), e.getPort()))
.collect(Collectors.toList());
loadBalancer = LoadBalancerBuilder.newBuilder()
.buildFixedServerListLoadBalancer(serverList);
}
示例10: build
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
/**
* 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(@Nonnull final String name,
@Nonnull final Client jerseyClient,
@Nonnull 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.setClientName(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(
configuration.getScheme(), 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;
}
示例11: setUp
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
Server server1 = new Server("host1", 4711);
Server server2 = new Server("host2", 4712);
List<Server> servers = Arrays.asList(server1, server2);
loadBalancer = LoadBalancerBuilder.newBuilder()
.withRule(new RoundRobinRule())
.buildFixedServerListLoadBalancer(servers);
filter = new RibbonClientRequestFilter(loadBalancer);
}
示例12: apply
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Override
public LoadBalancerBuilder<Server> apply(
final LoadBalancerBuilder<Server> builder, final Optional<String> searchDomain
) {
final String record = searchDomain.map(s -> this.record + "." + s).orElse(this.record);
return builder.withDynamicServerList(new SrvServerList(record));
}
示例13: module
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Override
public Module module(final Key<PluginSink> key, final String id) {
return new OutputPluginModule(id) {
@Provides
@Singleton
@Named("http")
public ExecutorService threadPool() {
return Executors.newCachedThreadPool(
new ThreadFactoryBuilder().setNameFormat("ffwd-okhttp-async-%d").build());
}
@Provides
@Singleton
public ILoadBalancer setupRibbonClient(
HttpPing httpPing, @Named("searchDomain") final Optional<String> searchDomain
) {
return discovery
.apply(LoadBalancerBuilder.newBuilder(), searchDomain)
.withPing(httpPing)
.buildDynamicServerListLoadBalancer();
}
@Override
protected void configure() {
bind(BatchedPluginSink.class).to(HttpPluginSink.class);
Key<PluginSink> sinkKey = Key.get(PluginSink.class, Names.named("httpSink"));
bind(sinkKey).to(HttpPluginSink.class);
install(wrapPluginSink(sinkKey, key));
expose(key);
}
};
}
示例14: apply
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
@Override
public LoadBalancerBuilder<Server> apply(
final LoadBalancerBuilder<Server> builder, final String searchDomain
) {
final List<Server> out = new ArrayList<>();
for (final HostAndPort hostAndPort : this.servers) {
final HostAndPort modified = hostAndPort.withOptionalSearchDomain(searchDomain);
out.add(new Server(modified.getHost(), modified.getPort()));
}
return builder.withDynamicServerList(new StaticServerList(out));
}
示例15: build
import com.netflix.loadbalancer.LoadBalancerBuilder; //导入依赖的package包/类
public HttpClient build() {
final HttpDiscovery discovery =
this.discovery == null ? HttpDiscovery.Static.supplyDefault() : this.discovery;
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new GzipRequestInterceptor());
final ObjectMapper objectMapper = setupApplicationJson();
final RawHttpClientFactory httpClientFactory =
new RawHttpClientFactory(objectMapper, builder.build());
final HttpPing httpPing = new HttpPing(httpClientFactory);
final ILoadBalancer loadBalancer = discovery
.apply(LoadBalancerBuilder.newBuilder(), searchDomain)
.withPing(httpPing)
.withRule(new RetryRule(new AvailabilityFilteringRule()))
.buildDynamicServerListLoadBalancer();
final int retries = this.retries == null ? DEFAULT_RETRIES : this.retries;
;
final long baseDelayMillis =
this.baseDelayMillis == null ? DEFAULT_BASE_DELAY_MILLIS : this.baseDelayMillis;
;
final long maxDelayMillis =
this.maxDelayMillis == null ? DEFAULT_MAX_DELAY_MILLIS : this.maxDelayMillis;
;
return new HttpClient(httpClientFactory, loadBalancer, retries, baseDelayMillis,
maxDelayMillis);
}