Java 使用 RestTemplate,如何首先将请求发送到代理,以便我可以在 JMeter 中使用我的 junit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3687670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?
提问by AHungerArtist
I have a web service running on my dev box implemented using Spring-MVC 3.0. I have various JUnits that test against that service using RestTemplate. What I would like to do is have JMeter pick up those JUnits REST requests when I run them. However, to do that, I need to have Spring's RestTemplate send them to the proxy that I'm running JMeter on. So, the question is, how can I do that?
我在使用 Spring-MVC 3.0 实现的开发箱上运行了一个 Web 服务。我有各种使用 RestTemplate 测试该服务的 JUnit。我想要做的是让 JMeter 在我运行它们时接收那些 JUnits REST 请求。但是,要做到这一点,我需要让 Spring 的 RestTemplate 将它们发送到我正在运行 JMeter 的代理。所以,问题是,我该怎么做?
I've done something similar with CXF and their http:conduit and http:client stuff, but I really have no idea how to do this with Spring-MVC.
我已经用 CXF 和他们的 http:conduit 和 http:client 做了类似的事情,但我真的不知道如何用 Spring-MVC 做到这一点。
采纳答案by CorayThan
@AHungerArtist's answer works for simple use cases, where you want all requests to use the same proxy. If you need some requests through restTemplate to use the proxy, and others to not, though, you may find this more useful. (Or if you just like doing it programmatically more than you like mucking with system properties!)
@AHungerArtist 的答案适用于简单的用例,您希望所有请求都使用相同的代理。如果您需要通过 restTemplate 的某些请求来使用代理,而其他请求则不需要,您可能会发现这更有用。(或者,如果您只是喜欢以编程方式进行操作,而不喜欢处理系统属性!)
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("my.host.com", 8080));
requestFactory.setProxy(proxy);
return new RestTemplate(requestFactory);
}
You should be able to create a copy of the restTemplate bean that way, and another one the normal way, so you can send requests with and without the proxy.
您应该能够以这种方式创建 restTemplate bean 的副本,并以正常方式创建另一个副本,因此您可以使用和不使用代理发送请求。
回答by AHungerArtist
Sadly, this was really easy.
可悲的是,这真的很容易。
Properties props = System.getProperties();
props.put("http.proxyHost", "localhost");
props.put("http.proxyPort", "9080");
回答by abhishek ringsia
put these lines before calling your get or post method. so proxy get set .
在调用 get 或 post 方法之前放置这些行。所以代理设置。
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
HttpHost proxy = new HttpHost("proxtserver", port);
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
restTemplate.setRequestFactory(requestFactory);
回答by Nicolas
Spring has a good documentation using a Customizer to determine different proxy
Spring 有一个很好的文档使用定制器来确定不同的代理
public class ProxyCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
final String proxyUrl = "proxy.example.com";
final int port = 3128;
HttpHost proxy = new HttpHost(proxyUrl, port);
HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
@Override
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context)
throws HttpException {
if (target.getHostName().equals("gturnquist-quoters.cfapps.io")) {
return super.determineProxy(target, request, context);
}
return null;
}
}).build();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
}
and the call to apply the ProxyCustomizer is
和应用 ProxyCustomizer 的调用是
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.additionalCustomizers(new ProxyCustomizer()).build();
}
回答by itstata
Alternatively you can use runtime parameters:
或者,您可以使用运行时参数:
jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888