当前位置: 首页 > 知识库问答 >
问题:

如何为多个用户隔离Jetty HttpClient?

苏昂雄
2023-03-14

我正在使用Eclipse Jetty HttpClient向服务器发送POST请求,以进行负载测试。

TL;DR:有没有一种方法可以将一个HttpClient实例与多个用户凭据集一起使用到一个目标URL?

为此,我需要作为单独的用户登录到测试中的服务器。尽管HttpClient是线程安全的,但由于其共享的身份验证存储,它似乎不支持单个实例。

解决方案似乎很简单,只需每个用户或每个线程使用一个HttpClient。

这工作正常,只是HttpClient为每个实例创建了许多线程(似乎是5到10个),因此我的负载测试需要一个非常大的堆,否则它将在尝试创建新线程时抛出OutOfMemory异常。

例如,在这个非常基本的测试中,第一组凭据用于所有后续的职位:

public class Test 
{
    static class User
    {
        String username;
        String password;
        User(String username, String password)
        {
            this.username = username;
            this.password = password;
        }
    }

    public static void main(String[] args) throws Exception 
    {
        SslContextFactory sslContextFactory = new SslContextFactory.Client();
        HttpClient httpClient = new HttpClient(sslContextFactory);
        httpClient.start();
        
        List<User> users = new ArrayList<>();
        
        users.add(new User("fry", "1234"));
        users.add(new User("leela", "2345"));
        users.add(new User("zoidberg", "3456"));
        
        URI uri = new URI("http://localhost:8080/myapi");

        for (User user : users)
        {
            AuthenticationStore auth = httpClient.getAuthenticationStore();
            auth.addAuthentication(new DigestAuthentication(uri, DigestAuthentication.ANY_REALM, user.username, user.password));

            Request request = httpClient.newRequest(uri);
            request.method("POST");
            
            ContentResponse result = request.send();

            System.out.println(result.getStatus());
        }
    }
}

现在,我在这个精心设计的测试中认识到,我可以在循环之间调用HttpClient.GetAuthenticationStore().ClearAuthenticationResults()HttpClient.GetAuthenticationStore().ClearAuthentications();,但是这对我的实际测试不起作用,因为我有多个线程同时发布。

我是否需要为每个用户使用单独的HttpClient实例?

谢谢你的点子!

共有1个答案

闽经纬
2023-03-14

您所需要的可以通过“抢占”每个请求的身份验证头来完成,如文档中所解释的。

你会这样做:

// Single HttpClient instance.
HttpClient httpClient = new HttpClient();

// The server URI.
URI uri = URI.create("http://example.com/secure");

// The authentication credential for each user.
Authentication.Result authn1 = new BasicAuthentication.BasicResult(uri, "user1", "password1");
Authentication.Result authn2 = new BasicAuthentication.BasicResult(uri, "user2", "password2");

// Create a request instance.
Request request1 = httpClient.newRequest(uri);
// Add the authorization headers for user1.
authn1.apply(request1);
request1.send();

Request request2 = httpClient.newRequest(uri);
// Add the authorization headers for user2.
authn2.apply(request2);
request2.send();

发送请求不需要是顺序的,也不需要像上面的简单示例中那样使用阻塞API。

 类似资料:
  • 本文向大家介绍请解释下什么是cookie隔离?为什么要隔离?如何隔离?相关面试题,主要包含被问及请解释下什么是cookie隔离?为什么要隔离?如何隔离?时的应答技巧和注意事项,需要的朋友参考一下 什么是 Cookie 隔离? 或者说:请求资源的时候不要让它带 cookie 怎么做 cookie 隔离技术和传统的多域名拆分请求,提高浏览器并发请求数有点类似,均是采用多域名来处理请求 传统做法是将 c

  • 本文向大家介绍如何隔离docker容器中的用户的方法,包括了如何隔离docker容器中的用户的方法的使用技巧和注意事项,需要的朋友参考一下 笔者在前文《理解 docker 容器中的 uid 和 gid》介绍了 docker 容器中的用户与宿主机上用户的关系,得出的结论是:docker 默认没有隔离宿主机用户和容器中的用户。如果你已经了解了 Linux 的 user namespace 技术(参考《

  • 问题内容: 我想将隔离级别设置为。如何使用gorm orm for postgres实现此目的。 示例代码: 问题答案: 我在这里有完全一样的问题: 和pg完全一样。

  • 本文向大家介绍使用 Iisftp.vbs 设置Active Directory 用户隔离,包括了使用 Iisftp.vbs 设置Active Directory 用户隔离的使用技巧和注意事项,需要的朋友参考一下 应用到: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1 可使用命令行脚本 iisftp

  • 我已经在c中创建了kafka消费者,并创建了一个具有10个分区的主题,当我尝试使用消费者读取数据时,它仅从2个分区读取,然后说没有更多的消息。我尝试使用这两种方法,即订阅和分配,但它们都不起作用。我应该如何将所有10个分区分配给单个使用者,这是将分区分配给使用者的正确方法吗?我已经使用此存储库构建了自定义消费者 https://github.com/edenhill/librdkafka/blob

  • 考虑一下这样的场景:有一个大应用(对应 <BigApp> 组件)包含了很多小的“子应用”(对应 SubApp 组件): import React, { Component } from 'react' import SubApp from './subapp' class BigApp extends Component { render() { return ( <di