使用 RestTemplate,如何首先将请求发送到代理,以便我可以将我的 junits 与 JMeter 一起使用?

发布于 2024-09-18 19:52:50 字数 286 浏览 8 评论 0原文

我的开发盒上运行着一个使用 Spring-MVC 3.0 实现的 Web 服务。我有各种 JUnit 使用 RestTemplate 对该服务进行测试。我想做的是让 JMeter 在运行 JUnits REST 请求时接收这些请求。然而,要做到这一点,我需要让 Spring 的 RestTemplate 将它们发送到我正在运行 JMeter 的代理。那么,问题是,我该如何做到这一点?

我已经用 CXF 及其 http:conduit 和 http:client 做了类似的事情,但我真的不知道如何使用 Spring-MVC 来做到这一点。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳男子 2024-09-25 19:52:50

@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);
}

您应该能够以这种方式创建restTemplate bean 的副本,并以正常方式创建另一个副本,这样您就可以使用或不使用代理人。

@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!)

@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.

不及他 2024-09-25 19:52:50

可悲的是,这真的很容易。


Properties props = System.getProperties();
props.put("http.proxyHost", "localhost");
props.put("http.proxyPort", "9080");

Sadly, this was really easy.


Properties props = System.getProperties();
props.put("http.proxyHost", "localhost");
props.put("http.proxyPort", "9080");
兮子 2024-09-25 19:52:50

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));

    }

}

以及应用 ProxyCustomizer 的调用是

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.additionalCustomizers(new ProxyCustomizer()).build();
}

Spring has a good documentation using a Customizer to determine different proxy

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

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.additionalCustomizers(new ProxyCustomizer()).build();
}
匿名。 2024-09-25 19:52:50

在调用 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);

put these lines before calling your get or post method. so proxy get set .

    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);
陌上芳菲 2024-09-25 19:52:50

或者,您可以使用运行时参数:

jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888

Alternatively you can use runtime parameters:

jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文