@Override public T apply(ServiceInstance instance) throws Exception { return request.apply(instance); } }
@Override public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public <T> T execute(String serviceId, LoadBalancerRequest<T> request) { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException { try { return request.apply(choose(serviceId)); } catch (Exception e) { throw new RuntimeException(e); } }
@Test public void noTransformer() throws Exception { init(NoTransformer.class); lbRequest.apply(instance); verify(execution).execute(httpRequestCaptor.capture(), eq(body)); assertEquals( "ServiceRequestWrapper should be executed", ServiceRequestWrapper.class, httpRequestCaptor.getValue().getClass()); }
@Test public void transformer() throws Exception { ConfigurableApplicationContext context = init(Transformer.class); LoadBalancerRequestTransformer transformer = context.getBean("transformer", LoadBalancerRequestTransformer.class); when(transformer.transformRequest(any(ServiceRequestWrapper.class), eq(instance))) .thenReturn(transformedRequest); lbRequest.apply(instance); verify(execution).execute(httpRequestCaptor.capture(), eq(body)); assertEquals( "transformer should have transformed the ServiceRequestWrapper into transformedRequest", transformedRequest, httpRequestCaptor.getValue()); }
private void executeLbRequest(List<LoadBalancerRequestTransformer> transformers) throws Exception { LoadBalancerRequestFactory lbReqFactory = new LoadBalancerRequestFactory(loadBalancer, transformers); LoadBalancerRequest<ClientHttpResponse> lbRequest = lbReqFactory.createRequest(request, body, execution); lbRequest.apply(instance); }
@Test public void transformersAreOrdered() throws Exception { ConfigurableApplicationContext context = init(TransformersAreOrdered.class); LoadBalancerRequestTransformer transformer = context.getBean("transformer", LoadBalancerRequestTransformer.class); when(transformer.transformRequest(any(ServiceRequestWrapper.class), eq(instance))) .thenReturn(transformedRequest); LoadBalancerRequestTransformer transformer2 = context.getBean("transformer2", LoadBalancerRequestTransformer.class); when(transformer2.transformRequest(transformedRequest, instance)).thenReturn(transformedRequest2); lbRequest.apply(instance); verify(execution).execute(httpRequestCaptor.capture(), eq(body)); assertEquals( "transformer2 should run after transformer", transformedRequest2, httpRequestCaptor.getValue()); }
@Override public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException { Server server = null; if(serviceInstance instanceof RibbonServer) { server = ((RibbonServer)serviceInstance).getServer(); } if (server == null) { throw new IllegalStateException("No instances available for " + serviceId); } RibbonLoadBalancerContext context = this.clientFactory .getLoadBalancerContext(serviceId); RibbonStatsRecorder statsRecorder = new RibbonStatsRecorder(context, server); try { T returnVal = request.apply(serviceInstance); statsRecorder.recordStats(returnVal); return returnVal; } // catch IOException and rethrow so RestTemplate behaves correctly catch (IOException ex) { statsRecorder.recordStats(ex); throw ex; } catch (Exception ex) { statsRecorder.recordStats(ex); ReflectionUtils.rethrowRuntimeException(ex); } return null; }