Jersey Client 是否可以在多线程环境下使用来消费 rest api?

Can Jersey Client be used in multithreaded environment for consuming rest api?

我想为多线程项目创建一个 Jersey 客户端。我需要为客户端创建连接池吗?

服务器上有 500 TPS 流量。如何计算以下参数以获得最佳性能。

ConnectionTimout、SocketTimeout、ReadTimeout、MaxConnectionPerHost、MaxConnections。

什么是重置连接池的概念以及何时使用它?

Jersey 客户端 thread-safe 用于执行多个请求。 Documentation

Methods to create instances of WebResource are thread-safe. Methods that modify configuration and or filters are not guaranteed to be thread-safe.

The creation of a Client instance is an expensive operation and the instance may make use of and retain many resources. It is therefore recommended that a Client instance is reused for the creation of WebResource instances that require the same configuration settings.

建议Clientclass的同一个实例重复用于多个请求执行。更改 Client 配置不是线程保存,必须适当处理。

是否需要为客户端创建连接池?
简答是的。
默认情况下 Jersey 客户端使用 BasicHttpClientConnectionManagerDocumentation

It is a simple connection manager that maintains only one connection at a time. Even though this class is thread-safe it ought to be used by one execution thread only. BasicHttpClientConnectionManager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and re-open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated, then java.lang.IllegalStateException is thrown.

对于多线程应用,需要用PoolingHttpClientConnectionManager覆盖默认值。

It is a more complex implementation that manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route for which the manager already has a persistent connection available in the pool will be serviced by leasing a connection from the pool rather than creating a brand new connection.

PoolingHttpClientConnectionManager maintains a maximum limit of connections on a per route basis and in total. Per default this implementation will create no more than 2 concurrent connections per given route and no more 20 connections in total. For many real-world applications these limits may prove too constraining, especially if they use HTTP as a transport protocol for their services.

详情Apache Connection Management

配置
一般建议是避免 Jersey 默认设置的无限超时。如果出现问题,它会产生卡住的线程。请参阅 Best practices for web service timeouts 选择正确的值。没有具体值,应根据服务和环境性能设置。正确的超时和连接大小将在性能测试或 real-time 使用后出现。
灵活实施,添加即时调整设置的能力。


Read Timeout

Read timeout interval property, in milliseconds. The value MUST be an instance of Integer. If the property is absent then the default value is an interval of infinity. A value of zero 0 is equivalent to an interval of infinity

可以像初始值一样设置1分钟。此外,您可以在特殊情况下覆盖每个请求的超时。


Connection Timeout

Connect timeout interval property, in milliseconds. The value MUST be an instance of Integer. If the property is absent then the default value is an interval of infinity. A value of 0 is equivalent to an interval of infinity

像初始值一样设置 500 - 1000 毫秒。


MaxConnectionPerHost
像初始值一样设置20个连接。


最大连接数
像初始值一样设置200个连接。