/** * Create a Netflix {@link RestClient} integrated with Ribbon if none already exists in the * application context. It is not required for Ribbon to work properly and is therefore created * lazily if ever another component requires it. * * @param config the configuration to use by the underlying Ribbon instance * @param loadBalancer the load balancer to use by the underlying Ribbon instance * @return a {@link RestClient} instances backed by Ribbon */ @Bean @Lazy @ConditionalOnMissingBean public RestClient ribbonRestClient( IClientConfig config, ILoadBalancer loadBalancer, ServerIntrospector serverIntrospector) { RestClient client = new OverrideRestClient(config, serverIntrospector); client.setLoadBalancer(loadBalancer); Monitors.registerObject("Client_" + this.name, client); return client; }
@Override protected String run() { try { // The named client param must match the prefix for the ribbon // configuration specified in the edge.properties file RestClient client = (RestClient) ClientFactory.getNamedClient("middletier-client"); // MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); // headers.putSingle("Content-Type", "text/plain"); // Note: If running locally on MacOS, you'll need to make sure your // /etc/hosts file contains the following entry: // 127.0.0.1 <hostname>.local // or else this will likely fail. HttpClientRequest request = HttpClientRequest.newBuilder() .setVerb(Verb.GET) .setUri( new URI( "/" + FluxConstants.MIDDLETIER_WEB_RESOURCE_ROOT_PATH + "/" + FluxConstants.MIDDLETIER_WEB_RESOURCE_VERSION + "/" + FluxConstants.MIDDLETIER_WEB_RESOURCE_GET_PATH + "/" + key)) // .setHeaders(headers) .build(); HttpClientResponse response = client.executeWithLoadBalancer(request); if (response.getStatus() != 200) { logger.error("error status: {}", response.getStatus()); throw new Exception("error status: " + response.getStatus()); } return IOUtils.toString(response.getRawEntity(), Charsets.UTF_8); } catch (Exception exc) { logger.error("Exception calling middletier while retrieving logs.", exc); throw new RuntimeException("Exception", exc); } }
HttpClientResponse forward() throws Exception { NFRequestContext context = NFRequestContext.getCurrentContext(); HttpClientRequest httpClientRequest = HttpClientRequest.newBuilder() .setVerb(verb) .setUri(uri) .setHeaders(headers) .setEntity(requestEntity) .setQueryParams(params) .build(); HttpClientResponse response = restClient.executeWithLoadBalancer(httpClientRequest); context.setZuulResponse(response); return response; }