本文整理汇总了Java中com.netflix.loadbalancer.BaseLoadBalancer类的典型用法代码示例。如果您正苦于以下问题:Java BaseLoadBalancer类的具体用法?Java BaseLoadBalancer怎么用?Java BaseLoadBalancer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BaseLoadBalancer类属于com.netflix.loadbalancer包,在下文中一共展示了BaseLoadBalancer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSecureClient2
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testSecureClient2() throws Exception {
ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.IsSecure, "true");
ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStore, secureServer.getTrustStore().getAbsolutePath());
ConfigurationManager.getConfigInstance().setProperty("test3.ribbon." + CommonClientConfigKey.TrustStorePassword, SecureGetTest.PASSWORD);
RestClient client = (RestClient) ClientFactory.getNamedClient("test3");
BaseLoadBalancer lb = new BaseLoadBalancer();
Server[] servers = new Server[]{new Server("localhost", secureServer.getServerPort())};
lb.addServers(Arrays.asList(servers));
client.setLoadBalancer(lb);
HttpRequest request = HttpRequest.newBuilder().uri(new URI("/")).build();
HttpResponse response = client.executeWithLoadBalancer(request);
assertStatusIsOk(response.getStatus());
assertEquals(secureServer.getServerPath("/"), response.getRequestedURI().toString());
}
示例2: setup
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Before
public void setup() {
ILoadBalancer loadBalancer = new BaseLoadBalancer();
loadBalancer.addServers(Arrays.asList(new Server("localhost", 7080)));
loadBalancer.addServers(Arrays.asList(new Server("localhost", 7081)));
loadBalancer.addServers(Arrays.asList(new Server("localhost", 7082)));
IClientConfig clientConfig = new DefaultClientConfigImpl();
RibbonHttpClientRequestFactory ribbonHttpClientRequestFactory = new RibbonHttpClientRequestFactory(loadBalancer, clientConfig);
myApi = new RestifyProxyBuilder()
.client(ribbonHttpClientRequestFactory)
.target(MyApi.class, "http://myApi")
.build();
}
示例3: beforeTest
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Before
public void beforeTest() {
ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.NFLoadBalancerClassName", BaseLoadBalancer.class.getName());
ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.client.NFLoadBalancerPingClassName", DummyPing.class.getName());
ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon.ReadTimeout", "1000");
ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.ConnectTimeout, "500");
ConfigurationManager.getConfigInstance().setProperty("RetryTest.ribbon." + CommonClientConfigKey.OkToRetryOnAllOperations, "true");
client = (RestClient) ClientFactory.getNamedClient("RetryTest");
lb = (BaseLoadBalancer) client.getLoadBalancer();
lb.setServersList(Lists.newArrayList(localServer));
httpClient = NFHttpClientFactory.getNamedNFHttpClient("RetryTest");
connectionPoolManager = (MonitoredConnectionManager) httpClient.getConnectionManager();
client.setMaxAutoRetries(0);
client.setMaxAutoRetriesNextServer(0);
client.setOkToRetryOnAllOperations(false);
lb.setServersList(Lists.newArrayList(localServer));
// reset the server index
lb.setRule(new AvailabilityFilteringRule());
lb.getLoadBalancerStats().getSingleServerStat(localServer).clearSuccessiveConnectionFailureCount();
}
示例4: addLoadBalancerListener
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
/**
* Add a listener that is responsible for removing an HttpClient and shutting down
* its connection pool if it is no longer available from load balancer.
*/
private void addLoadBalancerListener() {
if (!(lbContext.getLoadBalancer() instanceof BaseLoadBalancer)) {
return;
}
((BaseLoadBalancer) lbContext.getLoadBalancer()).addServerListChangeListener(new ServerListChangeListener() {
@Override
public void serverListChanged(List<Server> oldList, List<Server> newList) {
Set<Server> removedServers = new HashSet<Server>(oldList);
removedServers.removeAll(newList);
for (Server server: rxClientCache.keySet()) {
if (removedServers.contains(server)) {
// this server is no longer in UP status
removeClient(server);
}
}
}
});
}
示例5: testStreamWithLoadBalancer
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testStreamWithLoadBalancer() throws Exception {
// NettyHttpLoadBalancerErrorHandler errorHandler = new NettyHttpLoadBalancerErrorHandler(1, 3, true);
// IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
IClientConfig config = IClientConfig.Builder.newBuilder().withRetryOnAllOperations(true)
.withMaxAutoRetries(1)
.withMaxAutoRetriesNextServer(3)
.build();
BaseLoadBalancer lb = new BaseLoadBalancer(new DummyPing(), new AvailabilityFilteringRule());
LoadBalancingHttpClient<ByteBuf, ServerSentEvent> lbObservables = (LoadBalancingHttpClient<ByteBuf, ServerSentEvent>) RibbonTransport.newSSEClient(lb, config);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/personStream");
List<Person> result = Lists.newArrayList();
Server goodServer = new Server("localhost:" + port);
Server badServer = new Server("localhost:12245");
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
lb.setServersList(servers);
result = getPersonListFromResponse(lbObservables.submit(request, null, null));
assertEquals(EmbeddedResources.entityStream, result);
}
示例6: ribbonLoadBalancer
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Bean
public ILoadBalancer ribbonLoadBalancer() {
BaseLoadBalancer balancer = new BaseLoadBalancer();
balancer.setServersList(
Collections.singletonList(new Server("localhost", this.port)));
return balancer;
}
示例7: exceptionServiceRibbonLoadBalancer
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Bean
public ILoadBalancer exceptionServiceRibbonLoadBalancer() {
BaseLoadBalancer balancer = new BaseLoadBalancer();
balancer.setServersList(Collections
.singletonList(new Server("invalid.host.to.break.tests", 1234)));
return balancer;
}
示例8: testBuildStaticServerListLoadBalancer
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testBuildStaticServerListLoadBalancer() {
List<Server> list = Lists.newArrayList(expected, expected);
IRule rule = new AvailabilityFilteringRule();
IClientConfig clientConfig = IClientConfig.Builder.newBuilder()
.withDefaultValues()
.withMaxAutoRetriesNextServer(3).build();
assertEquals(3, clientConfig.get(Keys.MaxAutoRetriesNextServer).intValue());
BaseLoadBalancer lb = LoadBalancerBuilder.newBuilder()
.withRule(rule)
.buildFixedServerListLoadBalancer(list);
assertEquals(list, lb.getAllServers());
assertSame(rule, lb.getRule());
}
示例9: shutdown
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
public void shutdown() {
ILoadBalancer lb = this.getLoadBalancer();
if (lb instanceof BaseLoadBalancer) {
((BaseLoadBalancer) lb).shutdown();
}
NFHttpClientFactory.shutdownNFHttpClient(restClientName);
}
示例10: testUdpClientWithoutTimeout
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testUdpClientWithoutTimeout() throws Exception {
int port = choosePort();
UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 0).createServer();
server.start();
BaseLoadBalancer lb = new BaseLoadBalancer();
lb.setServersList(Lists.newArrayList(new Server("localhost", port)));
RxClient<DatagramPacket, DatagramPacket> client = RibbonTransport.newUdpClient(lb,
DefaultClientConfigImpl.getClientConfigWithDefaultValues());
try {
String response = client.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>,
Observable<DatagramPacket>>() {
@Override
public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) {
connection.writeStringAndFlush("Is there anybody out there?");
return connection.getInput();
}
}).take(1)
.map(new Func1<DatagramPacket, String>() {
@Override
public String call(DatagramPacket datagramPacket) {
return datagramPacket.content().toString(Charset.defaultCharset());
}
})
.toBlocking()
.first();
assertEquals(HelloUdpServer.WELCOME_MSG, response);
} finally {
server.shutdown();
}
}
示例11: testUdpClientTimeout
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testUdpClientTimeout() throws Exception {
int port = choosePort();
UdpServer<DatagramPacket, DatagramPacket> server = new HelloUdpServer(port, 5000).createServer();
server.start();
BaseLoadBalancer lb = new BaseLoadBalancer();
Server myServer = new Server("localhost", port);
lb.setServersList(Lists.newArrayList(myServer));
MyUDPClient client = new MyUDPClient(lb, DefaultClientConfigImpl.getClientConfigWithDefaultValues());
try {
String response = client.submit("Is there anybody out there?")
.map(new Func1<DatagramPacket, String>() {
@Override
public String call(DatagramPacket datagramPacket) {
return datagramPacket.content().toString(Charset.defaultCharset());
}
})
.toBlocking()
.first();
fail("Exception expected");
} catch (Exception e) {
assertTrue(e.getCause() instanceof TimeoutException);
assertEquals(1, client.getLoadBalancerContext().getServerStats(myServer).getSuccessiveConnectionFailureCount());
}
finally {
server.shutdown();
}
}
示例12: testServerListRefresh
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
/**
* This test ensures that when server list is refreshed in the load balancer, the set of servers
* which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server
* that is not part of oldList should stay in the map.
*
* @throws IOException
*/
@Test
public void testServerListRefresh() throws IOException {
String content = "Hello world";
MockWebServer server1 = new MockWebServer();
MockWebServer server2 = new MockWebServer();
MockWebServer server3 = new MockWebServer();
MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain")
.setBody(content);
server1.enqueue(mockResponse);
server2.enqueue(mockResponse);
server3.enqueue(mockResponse);
server1.play();
server2.play();
server3.play();
try {
BaseLoadBalancer lb = new BaseLoadBalancer();
List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()));
lb.setServersList(initialList);
LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
client.submit(request).toBlocking().last();
client.submit(request).toBlocking().last();
HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort());
client.submit(request2).toBlocking().last();
Set<Server> cachedServers = client.getRxClients().keySet();
assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers);
List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999));
lb.setServersList(newList);
cachedServers = client.getRxClients().keySet();
assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers);
} finally {
server1.shutdown();
server2.shutdown();
server3.shutdown();
}
}
示例13: testObservableWithMultipleServers
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testObservableWithMultipleServers() throws Exception {
IClientConfig config = DefaultClientConfigImpl
.getClientConfigWithDefaultValues()
.withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
Server goodServer = new Server("localhost:" + port);
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
.withRule(new AvailabilityFilteringRule())
.withPing(new DummyPing())
.buildFixedServerListLoadBalancer(servers);
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
new NettyHttpLoadBalancerErrorHandler(1, 3, true));
Person person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
assertEquals(EmbeddedResources.defaultPerson, person);
ServerStats stats = lbObservables.getServerStats(badServer);
// two requests to bad server because retry same server is set to 1
assertEquals(4, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(4, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
person = getPersonObservable(lbObservables.submit(request)).toBlocking().single();
assertEquals(EmbeddedResources.defaultPerson, person);
HttpClientListener listener = lbObservables.getListener();
assertEquals(1, listener.getPoolReuse());
}
示例14: testObservableWithMultipleServersWithOverrideRxConfig
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testObservableWithMultipleServersWithOverrideRxConfig() throws Exception {
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
Server goodServer = new Server("localhost:" + port);
List<Server> servers = Lists.newArrayList(badServer, badServer, badServer, goodServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
.withRule(new AvailabilityFilteringRule())
.withPing(new DummyPing())
.buildFixedServerListLoadBalancer(servers);
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
new NettyHttpLoadBalancerErrorHandler(1, 3, true));
HttpClientConfig rxconfig = HttpClientConfig.Builder.newDefaultConfig();
Person person = getPersonObservable(lbObservables.submit(request, rxconfig)).toBlocking().single();
assertEquals(EmbeddedResources.defaultPerson, person);
ServerStats stats = lbObservables.getServerStats(badServer);
// two requests to bad server because retry same server is set to 1
assertEquals(4, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(4, stats.getSuccessiveConnectionFailureCount());
stats = lbObservables.getServerStats(goodServer);
assertEquals(1, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
assertEquals(0, stats.getSuccessiveConnectionFailureCount());
final HttpClientListener listener = lbObservables.getListener();
assertEquals(1, listener.getConnectionCount());
waitUntilTrueOrTimeout(1000, new Func0<Boolean>() {
@Override
public Boolean call() {
return listener.getPoolReleases() == 1;
}
});
}
示例15: testObservableWithRetrySameServer
import com.netflix.loadbalancer.BaseLoadBalancer; //导入依赖的package包/类
@Test
public void testObservableWithRetrySameServer() throws Exception {
IClientConfig config = DefaultClientConfigImpl.getClientConfigWithDefaultValues().withProperty(CommonClientConfigKey.ConnectTimeout, "1000");
HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/testAsync/person");
Server badServer = new Server("localhost:12345");
Server goodServer = new Server("localhost:" + port);
List<Server> servers = Lists.newArrayList(badServer, badServer, goodServer);
BaseLoadBalancer lb = LoadBalancerBuilder.<Server>newBuilder()
.withRule(new AvailabilityFilteringRule())
.withPing(new DummyPing())
.buildFixedServerListLoadBalancer(servers);
LoadBalancingHttpClient<ByteBuf, ByteBuf> lbObservables = RibbonTransport.newHttpClient(lb, config,
new NettyHttpLoadBalancerErrorHandler(1, 0, true));
Observable<Person> observableWithRetries = getPersonObservable(lbObservables.submit(request));
ObserverWithLatch<Person> observer = new ObserverWithLatch<Person>();
observableWithRetries.subscribe(observer);
observer.await();
assertNull(observer.obj);
assertTrue(observer.error instanceof ClientException);
ServerStats stats = lbObservables.getServerStats(badServer);
// two requests to bad server because retry same server is set to 1
assertEquals(2, stats.getTotalRequestsCount());
assertEquals(0, stats.getActiveRequestsCount());
stats = lbObservables.getServerStats(goodServer);
assertEquals(0, stats.getTotalRequestsCount());
}