下面列出了org.mockito.MockingDetails#feign.httpclient.ApacheHttpClient 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private HttpEntity toApacheHttpEntity(RequestTemplate requestTemplate)
throws IOException, URISyntaxException {
final List<HttpUriRequest> request = new ArrayList<>(1);
BDDMockito.given(this.httpClient.execute(ArgumentMatchers.<HttpUriRequest>any()))
.will(new Answer<HttpResponse>() {
@Override
public HttpResponse answer(InvocationOnMock invocationOnMock)
throws Throwable {
request.add((HttpUriRequest) invocationOnMock.getArguments()[0]);
return new BasicHttpResponse(new BasicStatusLine(
new ProtocolVersion("http", 1, 1), 200, null));
}
});
new ApacheHttpClient(this.httpClient).execute(
requestTemplate.resolve(new HashMap<>()).request(),
new feign.Request.Options());
HttpUriRequest httpUriRequest = request.get(0);
return ((HttpEntityEnclosingRequestBase) httpUriRequest).getEntity();
}
/**
* This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a REST endpoint with
* Hystrix fallback support.
*
* @return The feign pointing to the service URL and with Hystrix fallback.
*/
@Produces
@Singleton
private AlohaService alohaService(Tracer tracer) {
// bind current span to Hystrix thread
TracingConcurrencyStrategy.register();
return HystrixFeign.builder()
// Use apache HttpClient which contains the ZipKin Interceptors
.client(new TracingClient(new ApacheHttpClient(HttpClientBuilder.create().build()), tracer))
// Bind Zipkin Server Span to Feign Thread
.logger(new Logger.ErrorLogger()).logLevel(Logger.Level.BASIC)
.decoder(new JacksonDecoder())
.target(AlohaService.class,"http://aloha:8080/",
() -> Collections.singletonList("Aloha response (fallback)"));
}
@Bean
@ConditionalOnMissingBean
public Client feignClient(LoadBalancerClient loadBalancerClient,
HttpClient httpClient) {
ApacheHttpClient delegate = new ApacheHttpClient(httpClient);
return new FeignBlockingLoadBalancerClient(delegate, loadBalancerClient);
}
@Test
public void shouldInstantiateHttpFeignClientWhenEnabled() {
ConfigurableApplicationContext context = initContext(
"spring.cloud.loadbalancer.ribbon.enabled=false");
assertThatOneBeanPresent(context, BlockingLoadBalancerClient.class);
assertLoadBalanced(context, ApacheHttpClient.class);
}
@Bean
public ApacheHttpClient client() {
/*
* We know our client is an AppacheHttpClient because we disabled the OK HTTP
* client. FeignAcceptGzipEncodingAutoConfiguration won't load unless there is
* a bean of type ApacheHttpClient (not Client) in this test because the bean
* is not yet created and so the application context doesnt know that the
* Client bean is actually an instance of ApacheHttpClient, therefore
* FeignAcceptGzipEncodingAutoConfiguration will not be loaded. We just create
* a bean here of type ApacheHttpClient so that the configuration will be
* loaded correctly.
*/
return (ApacheHttpClient) this.client;
}
@Test
public void testHttpClientWithFeign() {
Client delegate = this.feignClient.getDelegate();
assertThat(ApacheHttpClient.class.isInstance(delegate)).isTrue();
ApacheHttpClient apacheHttpClient = (ApacheHttpClient) delegate;
HttpClient httpClient = getField(apacheHttpClient, "client");
MockingDetails httpClientDetails = mockingDetails(httpClient);
assertThat(httpClientDetails.isMock()).isTrue();
}
@Bean
@ConditionalOnMissingBean(Client.class)
public Client createRaptorFeignClient() {
HttpClient httpClient = applicationContext.getBean(HttpClient.class);
if (httpClient != null) {
return new ApacheHttpClient(httpClient);
} else {
return new ApacheHttpClient();
}
}
/**
*
* This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a
* REST endpoint with Hystrix fallback support.
*/
@Bean
public HolaService holaService(Tracer tracer) {
// bind current span to Hystrix thread
TracingConcurrencyStrategy.register();
return HystrixFeign.builder()
.client(new TracingClient(new ApacheHttpClient(HttpClientBuilder.create().build()), tracer))
.logger(new Logger.ErrorLogger()).logLevel(Logger.Level.BASIC)
.decoder(new JacksonDecoder())
.target(HolaService.class, "http://hola:8080/",
() -> Collections.singletonList("Hola response (fallback)"));
}
@GET
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject search(@QueryParam("q") final String query, @Context final TracerContext tracing) throws Exception {
final GoogleBooksApi api = Feign
.builder()
.client(new TracingClient(new ApacheHttpClient(), tracing.unwrap(Tracer.class)))
.target(GoogleBooksApi.class, "https://www.googleapis.com");
final feign.Response response = api.search(query);
try (final Reader reader = response.body().asReader()) {
return Json.createReader(reader).readObject();
}
}
@Bean
@ConditionalOnMissingBean(Client.class)
public Client feignClient(HttpClient httpClient) {
return new ApacheHttpClient(httpClient);
}