com.netflix.loadbalancer.Server Java Examples
The following examples show how to use
com.netflix.loadbalancer.Server.
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: LBBuilderTest.java From ribbon with Apache License 2.0 | 6 votes |
@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()); }
Example #2
Source File: DiscoveryLoadBalancerTest.java From ribbon with Apache License 2.0 | 6 votes |
@Test public void testLoadBalancer() { IClientConfig config = IClientConfig.Builder.newBuilder().withDefaultValues() .withDeploymentContextBasedVipAddresses(getVipAddress()).build() .set(IClientConfigKey.Keys.NIWSServerListClassName, DiscoveryEnabledNIWSServerList.class.getName()); LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(config); LoadBalancerContext lbContext = client.getLoadBalancerContext(); List<Server> serverList = lbContext.getLoadBalancer().getAllServers(); assertEquals(getMockServerList(), serverList); }
Example #3
Source File: TestSessionSticknessRule.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@Test public void testRuleFullOperation() { SessionStickinessRule rule = new SessionStickinessRule(); LoadBalancer mockedLb = mock(LoadBalancer.class); Transport transport = mock(Transport.class); MicroserviceInstance instance1 = new MicroserviceInstance(); instance1.setInstanceId("1234"); ServiceCombServer mockedServer = new ServiceCombServer(transport, new CacheEndpoint("rest:127.0.0.1:8889", instance1)); Invocation invocation = mock(Invocation.class); LoadBalancerStats stats = mock(LoadBalancerStats.class); Mockito.when(mockedLb.getLoadBalancerStats()).thenReturn(stats); Deencapsulation.invoke(rule, "chooseServerWhenTimeout", Arrays.asList(mockedServer), invocation); mockedServer.setAlive(true); mockedServer.setReadyToServe(true); List<ServiceCombServer> allServers = Arrays.asList(mockedServer); rule.setLoadBalancer(mockedLb); Server s = rule.choose(allServers, invocation); Assert.assertEquals(s, mockedServer); s = rule.choose(allServers, invocation); Assert.assertEquals(s, mockedServer); }
Example #4
Source File: ConnectionPool.java From suro with Apache License 2.0 | 6 votes |
private SuroConnection chooseFromPool() { SuroConnection connection = null; int count = 0; while (connection == null) { Server server = lb.chooseServer(null); if (server != null) { if (!serverSet.contains(server)) { newConnectionBuilder.execute(createNewConnection(server, true)); } else { connection = connectionPool.remove(server); } } else { break; } ++count; if (count >= 10) { logger.error("no connection available selected in 10 retries"); break; } } return connection; }
Example #5
Source File: PooledConnection.java From zuul with Apache License 2.0 | 6 votes |
public PooledConnection(final Channel channel, final Server server, final ClientChannelManager channelManager, final InstanceInfo serverKey, final ServerStats serverStats, final Counter closeConnCounter, final Counter closeWrtBusyConnCounter) { this.channel = channel; this.server = server; this.channelManager = channelManager; this.serverKey = serverKey; this.serverStats = serverStats; this.creationTS = System.currentTimeMillis(); this.closeConnCounter = closeConnCounter; this.closeWrtBusyConnCounter = closeWrtBusyConnCounter; this.connectionState = ConnectionState.WRITE_READY; // Store ourself as an attribute on the underlying Channel. channel.attr(CHANNEL_ATTR).set(this); }
Example #6
Source File: GroovyGrayScriptEngine.java From summerframework with Apache License 2.0 | 6 votes |
@Override public List<Map<String, String>> execute(List<Server> servers, Map<String, Object> context) { List<Map<String, String>> list = new ArrayList<>(); Map<String, String> node = new HashMap<>(); String toServerName = servers.get(0).getMetaInfo().getAppName(); String fromServerName = registration.getApplicationInfoManager().getEurekaInstanceConfig().getAppname(); List<String> groovyScript = grayRulesStore.findGroovyScript(fromServerName, toServerName); groovyScript.forEach(s -> { try { Map<String, String> nodeInfo = (Map)GroovyScriptEngineUtil.executeChoose(s, context); list.add(nodeInfo); } catch (Exception e) { logger.error(e.getMessage(), e); } }); return list; }
Example #7
Source File: MyUDPClient.java From ribbon with Apache License 2.0 | 6 votes |
public Observable<DatagramPacket> submit(final String content) { return LoadBalancerCommand.<DatagramPacket>builder() .withLoadBalancerContext(lbContext) .build() .submit(new ServerOperation<DatagramPacket>() { @Override public Observable<DatagramPacket> call(Server server) { RxClient<DatagramPacket, DatagramPacket> rxClient = getOrCreateRxClient(server); return rxClient.connect().flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, Observable<? extends DatagramPacket>>() { @Override public Observable<? extends DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) { connection.writeStringAndFlush(content); return connection.getInput().timeout(10, TimeUnit.MILLISECONDS).take(1); } }); } }); }
Example #8
Source File: LoadBalancingRxClient.java From ribbon with Apache License 2.0 | 6 votes |
/** * Look up the client associated with this Server. * @param host * @param port * @return */ protected T getOrCreateRxClient(Server server) { T client = rxClientCache.get(server); if (client != null) { return client; } else { client = createRxClient(server); client.subscribe(listener); client.subscribe(eventSubject); T old = rxClientCache.putIfAbsent(server, client); if (old != null) { return old; } else { return client; } } }
Example #9
Source File: LoadbalanceHandler.java From servicecomb-java-chassis with Apache License 2.0 | 6 votes |
@Override public Server chooseServer(Object key) { Invocation invocation = (Invocation) key; boolean isRetry = null != lastServer; for (int i = 0; i < COUNT; i++) { Server s = delegate.chooseServer(invocation); if (s == null) { break; } if (!s.equals(lastServer)) { lastServer = s; break; } } if (isRetry) { invocation.getTraceIdLogger().info(LOGGER, "retry to instance [{}]", lastServer.getHostPort()); } return lastServer; }
Example #10
Source File: EurekaServerListProcessor.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
private void reloadUpServersMap() { Map<String, List<Server>> unUpServersMap = new HashMap<>(); grayHoldoutServerProperties.getServices().forEach( (serviceId, instanceStatuses) -> { if (CollectionUtils.isEmpty(instanceStatuses)) { return; } List<Server> unUpServers = getUnUpServerList(serviceId, instanceStatuses); if (CollectionUtils.isNotEmpty(unUpServers)) { unUpServersMap.put(serviceId, unUpServers); } }); this.unUpServersMap = unUpServersMap; }
Example #11
Source File: NacosServerListProcessor.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
@Override protected List<Server> getServers(String serviceId, List<Server> servers) { List<InstanceStatus> statusList = getHoldoutInstanceStatus(serviceId); List<Server> holdoutServers = getInstances(serviceId).stream().filter(instance -> statusList.contains(getInstanceStatus(instance))) .map(NacosServer::new).collect(Collectors.toList()); if(CollectionUtils.isEmpty(holdoutServers)){ return servers; } return ListUtils.union(servers, holdoutServers); }
Example #12
Source File: GrayDecisionPredicate.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
@Override public boolean apply(PredicateKey input) { GrayLoadBalanceRule grayRule = getIRule(); GrayRequest grayRequest = grayRule.getRequestLocalStorage().getGrayRequest(); Server server = input.getServer(); String serviceId = grayRequest.getServiceId(); String instanceId = server.getMetaInfo().getInstanceId(); try { ServerSpec serverSpec = grayRule.getServerExplainer().apply(server); GrayDecisionInputArgs decisionInputArgs = GrayDecisionInputArgs .builder().grayRequest(grayRequest).server(serverSpec).build(); List<GrayDecision> grayDecisions = grayRule.getGrayManager().getGrayDecision(serviceId, instanceId); for (GrayDecision grayDecision : grayDecisions) { if (grayDecision.test(decisionInputArgs)) { return true; } } }catch (Exception e){ log.error("", e); } return false; }
Example #13
Source File: LoadBalancingHttpClient.java From ribbon with Apache License 2.0 | 5 votes |
/** * Subject an operation to run in the load balancer * * @param request * @param errorHandler * @param requestConfig * @param rxClientConfig * @return */ private Observable<HttpClientResponse<O>> submit(final Server server, final HttpClientRequest<I> request, final RetryHandler errorHandler, final IClientConfig requestConfig, final ClientConfig rxClientConfig) { RetryHandler retryHandler = errorHandler; if (retryHandler == null) { retryHandler = getRequestRetryHandler(request, requestConfig); } final IClientConfig config = requestConfig == null ? DefaultClientConfigImpl.getEmptyConfig() : requestConfig; final ExecutionContext<HttpClientRequest<I>> context = new ExecutionContext<HttpClientRequest<I>>(request, config, this.getClientConfig(), retryHandler); Observable<HttpClientResponse<O>> result = submitToServerInURI(request, config, rxClientConfig, retryHandler, context); if (result == null) { LoadBalancerCommand<HttpClientResponse<O>> command; if (retryHandler != defaultRetryHandler) { // need to create new builder instead of the default one command = LoadBalancerCommand.<HttpClientResponse<O>>builder() .withExecutionContext(context) .withLoadBalancerContext(lbContext) .withListeners(listeners) .withClientConfig(this.getClientConfig()) .withRetryHandler(retryHandler) .withServer(server) .build(); } else { command = defaultCommandBuilder; } result = command.submit(requestToOperation(request, getRxClientConfig(config, rxClientConfig))); } return result; }
Example #14
Source File: HttpPing.java From ffwd with Apache License 2.0 | 5 votes |
@Override public boolean isAlive(final Server server) { try { clientFactory.newClient(server).ping().get(); } catch (Exception e) { log.warn("Error when pinging server ({}): {}", server, e.getMessage()); log.trace("Error when pinging server ({}): {}", server, e.getMessage(), e); return false; } return true; }
Example #15
Source File: ServiceLoadBalancer.java From TeaStore with Apache License 2.0 | 5 votes |
private <T, R> List<R> multicastRESTOperation(String endpointURI, Class<T> entityClass, Function<RESTClient<T>, R> operation, Server exception) { List<R> responses = new ArrayList<>(); List<Server> servers = null; loadBalancerModificationLock.readLock().lock(); try { if (loadBalancer != null) { servers = new ArrayList<>(loadBalancer.getAllServers()); } if (servers != null) { if (exception != null) { servers.remove(exception); } responses = servers.parallelStream().map( server -> { try { return operation.apply((RESTClient<T>) getEndpointClientCollection(endpointURI, entityClass) .getRESTClient(server)); } catch (Exception e) { return null; } }).collect(Collectors.toList()); } } finally { loadBalancerModificationLock.readLock().unlock(); } return responses; }
Example #16
Source File: NettyClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test @Ignore public void testRedirect() throws Exception { HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet(SERVICE_URI + "testAsync/redirect?port=" + port); LoadBalancingHttpClient<ByteBuf, ByteBuf> observableClient = RibbonTransport.newHttpClient( IClientConfig.Builder.newBuilder().withDefaultValues() .withFollowRedirects(true) .build()); Person person = getPersonObservable(observableClient.submit(new Server(host, port), request)).toBlocking().single(); assertEquals(EmbeddedResources.defaultPerson, person); }
Example #17
Source File: DiscoveryEnabledServerListTest.java From ribbon with Apache License 2.0 | 5 votes |
@Override protected List<Server> getMockServerList() { List<Server> servers = new ArrayList<Server>(); servers.add(new Server("localhost", 12345)); servers.add(new Server("localhost", server.getPort())); return servers; }
Example #18
Source File: ServerListLoabBalancerTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testChooseServer() { assertNotNull(lb); Set<Server> result = new HashSet<Server>(); for (int i = 0; i < 100; i++) { Server s = lb.chooseServer(null); result.add(s); } Set<Server> expected = new HashSet<Server>(); expected.addAll(serverList); assertEquals(expected, result); }
Example #19
Source File: DefaultClientChannelManagerTest.java From zuul with Apache License 2.0 | 5 votes |
@Test public void pickAddressInternal_discovery() { InstanceInfo instanceInfo = Builder.newBuilder().setAppName("app").setHostName("192.168.0.1").setPort(443).build(); Server s = new DiscoveryEnabledServer(instanceInfo, true); SocketAddress addr = DefaultClientChannelManager.pickAddressInternal(s, "originname"); Truth.assertThat(addr).isInstanceOf(InetSocketAddress.class); InetSocketAddress socketAddress = (InetSocketAddress) addr; assertEquals(InetAddresses.forString("192.168.0.1"), socketAddress.getAddress()); assertEquals(443, socketAddress.getPort()); }
Example #20
Source File: DefaultClientChannelManager.java From zuul with Apache License 2.0 | 5 votes |
protected IConnectionPool createConnectionPool( Server chosenServer, ServerStats stats, InstanceInfo instanceInfo, SocketAddress serverAddr, NettyClientConnectionFactory clientConnFactory, PooledConnectionFactory pcf, ConnectionPoolConfig connPoolConfig, IClientConfig clientConfig, Counter createNewConnCounter, Counter createConnSucceededCounter, Counter createConnFailedCounter, Counter requestConnCounter, Counter reuseConnCounter, Counter connTakenFromPoolIsNotOpen, Counter maxConnsPerHostExceededCounter, PercentileTimer connEstablishTimer, AtomicInteger connsInPool, AtomicInteger connsInUse) { return new PerServerConnectionPool( chosenServer, stats, instanceInfo, serverAddr, clientConnFactory, pcf, connPoolConfig, clientConfig, createNewConnCounter, createConnSucceededCounter, createConnFailedCounter, requestConnCounter, reuseConnCounter, connTakenFromPoolIsNotOpen, maxConnsPerHostExceededCounter, connEstablishTimer, connsInPool, connsInUse ); }
Example #21
Source File: DefaultClientChannelManagerTest.java From zuul with Apache License 2.0 | 5 votes |
@Test public void deriveInstanceInfoInternal_discovery() { InstanceInfo instanceInfo = Builder.newBuilder().setAppName("app").build(); Server s = new DiscoveryEnabledServer(instanceInfo, true); InstanceInfo actual = DefaultClientChannelManager.deriveInstanceInfoInternal(s); assertSame(instanceInfo, actual); }
Example #22
Source File: LBBuilderTest.java From ribbon with Apache License 2.0 | 5 votes |
@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()); }
Example #23
Source File: UdpClientTest.java From ribbon with Apache License 2.0 | 5 votes |
@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(); } }
Example #24
Source File: RibbonServerChooser.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
@Override public ServerListResult<Server> distinguishServerList(List<Server> servers) { String serviceId = getServiceId(servers); if(StringUtils.isEmpty(serviceId)){ return null; } return distinguishServerList(serviceId, servers); }
Example #25
Source File: RegistryClient.java From TeaStore with Apache License 2.0 | 5 votes |
/** * Unregister a server for a service in the registry. * * @param service * The service for which to unregister. * @param server * The server address to remove. * @return True, if unregistration succeeded. */ private boolean unregisterOnce(Service service, Server server) { try { Response response = getRESTClient(1000).target(registryRESTURL).path(service.getServiceName()) .path(server.toString()).request(MediaType.APPLICATION_JSON).delete(); return (response.getStatus() == Response.Status.OK.getStatusCode()); } catch (ProcessingException e) { return false; } }
Example #26
Source File: ServerListLoabBalancerTest.java From ribbon with Apache License 2.0 | 5 votes |
@BeforeClass public static void init() { Configuration config = ConfigurationManager.getConfigInstance(); config.setProperty("ServerListLoabBalancerTest.ribbon.NFLoadBalancerClassName", com.netflix.loadbalancer.DynamicServerListLoadBalancer.class.getName()); config.setProperty("ServerListLoabBalancerTest.ribbon.NIWSServerListClassName", FixedServerList.class.getName()); lb = (DynamicServerListLoadBalancer<Server>) ClientFactory.getNamedLoadBalancer("ServerListLoabBalancerTest"); }
Example #27
Source File: NetflixRibbonGrayAutoConfiguration.java From spring-cloud-gray with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean public RibbonServerChooser ribbonServerChooser( GrayManager grayManager, RequestLocalStorage requestLocalStorage, GrayPredicate grayPredicate, ServerExplainer<Server> serverExplainer, @Autowired(required = false) ServerListProcessor serverListProcess){ if(serverListProcess==null){ serverListProcess = GrayClientHolder.getServereListProcessor(); } return new RibbonServerChooser(grayManager, requestLocalStorage, grayPredicate, serverExplainer, serverListProcess); }
Example #28
Source File: DefaultClientChannelManagerTest.java From zuul with Apache License 2.0 | 5 votes |
@Test public void pickAddressInternal_discovery_unresolved() { InstanceInfo instanceInfo = Builder.newBuilder().setAppName("app").setHostName("localhost").setPort(443).build(); Server s = new DiscoveryEnabledServer(instanceInfo, true); SocketAddress addr = DefaultClientChannelManager.pickAddressInternal(s, "originname"); Truth.assertThat(addr).isInstanceOf(InetSocketAddress.class); InetSocketAddress socketAddress = (InetSocketAddress) addr; assertTrue(socketAddress.toString(), socketAddress.getAddress().isLoopbackAddress()); assertEquals(443, socketAddress.getPort()); }
Example #29
Source File: PrimeConnectionsTest.java From ribbon with Apache License 2.0 | 5 votes |
@Test public void testPrimeConnectionsLargePool() throws Exception { Configuration config = ConfigurationManager.getConfigInstance(); config.setProperty("PrimeConnectionsTest2.ribbon.NFLoadBalancerClassName", com.netflix.loadbalancer.DynamicServerListLoadBalancer.class.getName()); config.setProperty("PrimeConnectionsTest2.ribbon.NIWSServerListClassName", LargeFixedServerList.class.getName()); config.setProperty("PrimeConnectionsTest2.ribbon.EnablePrimeConnections", "true"); DynamicServerListLoadBalancer<Server> lb = (DynamicServerListLoadBalancer<Server>) ClientFactory.getNamedLoadBalancer("PrimeConnectionsTest2"); PrimeConnectionEndStats stats = lb.getPrimeConnections().getEndStats(); assertEquals(stats.success, LARGE_FIXED_SERVER_LIST_SIZE); }
Example #30
Source File: DefaultClientChannelManagerTest.java From zuul with Apache License 2.0 | 5 votes |
@Test public void pickAddressInternal_nonDiscovery_unresolved() { Server s = new Server("localhost", 443); SocketAddress addr = DefaultClientChannelManager.pickAddressInternal(s, "originname"); Truth.assertThat(addr).isInstanceOf(InetSocketAddress.class); InetSocketAddress socketAddress = (InetSocketAddress) addr; assertTrue(socketAddress.toString(), socketAddress.getAddress().isLoopbackAddress()); assertEquals(443, socketAddress.getPort()); }