简体   繁体   English

用于设计可伸缩Web服务的模式

[英]Pattern for designing a scalable web service

I am writing a web service in Java which needs to handle a large number of requests / second. 我正在用Java编写一个Web服务,它需要处理大量的请求/秒。 The general flow will be: 一般流程将是:

  • Web service receives a request from client Web服务接收来自客户端的请求
  • Returns a 'keep polling me' response to client 返回对客户端的“保持轮询我”响应
  • Calls another web service (or services), and waits for them to respond (with a timeout) 调用另一个Web服务(或服务),并等待它们响应(超时)
  • Client polls our web service, until it receives a response (with a timeout) 客户端轮询我们的Web服务,直到它收到响应(超时)

Researching on the Internet, I have found two general approaches to writing web services: 在互联网上研究,我发现了两种编写Web服务的通用方法:

  • Spawn a thread for each request 为每个请求生成一个线程
  • Use the Reactor pattern (central dispatcher thread responds to IO events) 使用Reactor模式(中央调度程序线程响应IO事件)

Do you have a recommendation for which approach is generally better, and what are the pros/cons of each approach? 您是否建议哪种方法通常更好,每种方法的优缺点是什么? I would also appreciate pointers to examples. 我也很感激指向示例的指针。

Don't think multi-threading. 不要以为多线程。 Think asynchronously. 异步思考。 I happened to have just coded an async handler that ran 2,000 RPS with <10 threads in IIS. 我碰巧编写了一个异步处理程序,在IIS中运行了2000 RPS,其中包含<10个线程。 Not sure how java works since I'm a .net guy but I gotta believe they have similar BeginXXX/EndXXX methods. 不知道java是如何工作的,因为我是一个.net人,但我相信他们有类似的BeginXXX / EndXXX方法。 If you ever spawn a thread then you're not considering all the places your code can block: data base IO, File I/O, web services, etc. These are the places your performance will cause your site to be slow. 如果您生成一个线程,那么您不会考虑代码可以阻止的所有位置:数据库IO,文件I / O,Web服务等。这些是您的性能将导致您的站点变慢的地方。

Async, Async, Async. 异步,异步,异步。

Chant and repeat. 吟唱并重复。

In addition to "No Refunds No Returns" response, I'd say yeah "Think Asynchronously" as you should be allowing your container to manage the multi-threading/scalability and high-availability issues of the web services it has deployed, this allows you to set-up things like clustering and so forth using your application container. 除了“No Refunds No Returns”响应之外,我会说“异步思考”,因为您应该允许容器管理它已部署的Web服务的多线程/可伸缩性和高可用性问题,这允许您可以使用应用程序容器设置聚类等内容。

EDIT: So in conclusion, there isn't a pattern as such, maybe you should explore the scalability/availability features of your application container... 编辑:总而言之,没有这样的模式,也许您应该探索应用程序容器的可伸缩性/可用性功能...

Asynchronism is indeed the right approach but don't manage this yourself, use something that supports asynchronous web service invocation like JAX-WS 2.0 (which uses the Future interface and/or the Executor framework from java.util.concurrent ). 异步确实是正确的方法,但不要自己管理,使用支持异步Web服务调用的东西,如JAX-WS 2.0(它使用Future接口和/或java.util.concurrentExecutor框架)。 See Asynchronous Web Service Invocation with JAX-WS 2.0 . 请参阅使用JAX-WS 2.0进行异步Web服务调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM