use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.
the class RetryableOkHttpLoadBalancingClient method execute.
@Override
public OkHttpRibbonResponse execute(final OkHttpRibbonRequest ribbonRequest, final IClientConfig configOverride) throws Exception {
final LoadBalancedRetryPolicy retryPolicy = loadBalancedRetryFactory.createRetryPolicy(this.getClientName(), this);
RetryCallback<OkHttpRibbonResponse, Exception> retryCallback = new RetryCallback<OkHttpRibbonResponse, Exception>() {
@Override
public OkHttpRibbonResponse doWithRetry(RetryContext context) throws Exception {
// on retries the policy will choose the server and set it in the context
// extract the server and update the request being made
OkHttpRibbonRequest newRequest = ribbonRequest;
if (context instanceof LoadBalancedRetryContext) {
ServiceInstance service = ((LoadBalancedRetryContext) context).getServiceInstance();
validateServiceInstance(service);
// Reconstruct the request URI using the host and port set in the retry context
newRequest = newRequest.withNewUri(new URI(service.getUri().getScheme(), newRequest.getURI().getUserInfo(), service.getHost(), service.getPort(), newRequest.getURI().getPath(), newRequest.getURI().getQuery(), newRequest.getURI().getFragment()));
}
if (isSecure(configOverride)) {
final URI secureUri = UriComponentsBuilder.fromUri(newRequest.getUri()).scheme("https").build().toUri();
newRequest = newRequest.withNewUri(secureUri);
}
OkHttpClient httpClient = getOkHttpClient(configOverride, secure);
final Request request = newRequest.toRequest();
Response response = httpClient.newCall(request).execute();
if (retryPolicy.retryableStatusCode(response.code())) {
ResponseBody responseBody = response.peekBody(Integer.MAX_VALUE);
response.close();
throw new OkHttpStatusCodeException(RetryableOkHttpLoadBalancingClient.this.clientName, response, responseBody, newRequest.getURI());
}
return new OkHttpRibbonResponse(response, newRequest.getUri());
}
};
return this.executeWithRetry(ribbonRequest, retryPolicy, retryCallback, new LoadBalancedRecoveryCallback<OkHttpRibbonResponse, Response>() {
@Override
protected OkHttpRibbonResponse createResponse(Response response, URI uri) {
return new OkHttpRibbonResponse(response, uri);
}
});
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancedRetryPolicy method updateServerInstanceStats.
private void updateServerInstanceStats(LoadBalancedRetryContext context) {
ServiceInstance serviceInstance = context.getServiceInstance();
if (serviceInstance instanceof RibbonServer) {
Server lbServer = ((RibbonServer) serviceInstance).getServer();
ServerStats serverStats = lbContext.getServerStats(lbServer);
serverStats.incrementSuccessiveConnectionFailureCount();
serverStats.addToFailureCount();
LOGGER.debug(lbServer.getHostPort() + " RetryCount: " + context.getRetryCount() + " Successive Failures: " + serverStats.getSuccessiveConnectionFailureCount() + " CircuitBreakerTripped:" + serverStats.isCircuitBreakerTripped());
}
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.
the class EurekaDiscoveryClient method getInstances.
@Override
public List<ServiceInstance> getInstances(String serviceId) {
List<InstanceInfo> infos = this.eurekaClient.getInstancesByVipAddress(serviceId, false);
List<ServiceInstance> instances = new ArrayList<>();
for (InstanceInfo info : infos) {
instances.add(new EurekaServiceInstance(info));
}
return instances;
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-netflix by spring-cloud.
the class CommonsInstanceDiscovery method getInstancesForApp.
/**
* helper that fetches the Instances for each application from DiscoveryClient.
* @param serviceId
* @return List<Instance>
* @throws Exception
*/
protected List<Instance> getInstancesForApp(String serviceId) throws Exception {
List<Instance> instances = new ArrayList<>();
log.info("Fetching instances for app: " + serviceId);
List<ServiceInstance> serviceInstances = discoveryClient.getInstances(serviceId);
if (serviceInstances == null || serviceInstances.isEmpty()) {
log.warn("DiscoveryClient returned null or empty for service: " + serviceId);
return instances;
}
try {
log.info("Received instance list for service: " + serviceId + ", size=" + serviceInstances.size());
for (ServiceInstance serviceInstance : serviceInstances) {
Instance instance = marshall(serviceInstance);
if (instance != null) {
instances.add(instance);
}
}
} catch (Exception e) {
log.warn("Failed to retrieve instances from DiscoveryClient", e);
}
return instances;
}
use of org.springframework.cloud.client.ServiceInstance in project spring-cloud-gateway by spring-cloud.
the class LoadBalancerClientFilterTests method shouldFilter.
@Test
public void shouldFilter() {
URI url = UriComponentsBuilder.fromUriString("lb://myservice").build().toUri();
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, url);
ServiceInstance serviceInstance = new DefaultServiceInstance("myservice", "localhost", 8080, true);
when(loadBalancerClient.choose("myservice")).thenReturn(serviceInstance);
URI requestUrl = UriComponentsBuilder.fromUriString("https://localhost:8080").build().toUri();
when(loadBalancerClient.reconstructURI(any(ServiceInstance.class), any(URI.class))).thenReturn(requestUrl);
loadBalancerClientFilter.filter(exchange, chain);
assertThat((LinkedHashSet<URI>) exchange.getAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR)).contains(url);
verify(loadBalancerClient).choose("myservice");
ArgumentCaptor<URI> urlArgumentCaptor = ArgumentCaptor.forClass(URI.class);
verify(loadBalancerClient).reconstructURI(any(), urlArgumentCaptor.capture());
URI uri = urlArgumentCaptor.getValue();
assertThat(uri).isNotNull();
assertThat(uri.toString()).isEqualTo("loadbalancerclient.org");
verifyNoMoreInteractions(loadBalancerClient);
assertThat((URI) exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR)).isEqualTo(requestUrl);
verify(chain).filter(exchange);
verifyNoMoreInteractions(chain);
}
Aggregations