检测传出 HTTP 调用 - AWS X-Ray

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

检测传出 HTTP 调用

用户工厂类显示应用程序如何使用适用于 Java 的 X-Ray 开发工具包的 HTTPClientBuilder 版本来检测传出 HTTP 调用。

src/main/java/scorekeep/UserFactory.java - HTTPClient 检测
import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder; public String randomName() throws IOException { CloseableHttpClient httpclient = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet("http://uinames.com/api/"); CloseableHttpResponse response = httpclient.execute(httpGet); try { HttpEntity entity = response.getEntity(); InputStream inputStream = entity.getContent(); ObjectMapper mapper = new ObjectMapper(); Map<String, String> jsonMap = mapper.readValue(inputStream, Map.class); String name = jsonMap.get("name"); EntityUtils.consume(entity); return name; } finally { response.close(); } }

如果您当前使用 org.apache.http.impl.client.HttpClientBuilder,则只需使用 com.amazonaws.xray.proxies.apache.http.HttpClientBuilder 的语句换出该类的导出语句。