4

我知道 REST 并且知道在不使用会话的情况下创建 RESTful Web 服务,我知道它更好,但我并不真正理解无状态的概念以及使用 REST 如何使您的应用程序具有可扩展性

有人可以解释 REST 的这两个方面,无状态和可伸缩性,以及 SOAP 有何不同且不可取?

4

3 回答 3

6

有状态 API 将为所有连接的客户端维护会话。这意味着会话需要在服务器之间共享,这限制了可扩展性。

通过将此状态存储在客户端并随每个请求一起发送,您可以拥有相同的状态,但具有更好的可扩展性。

于 2012-12-21T12:04:28.527 回答
2

Creating RESTful services doesn't make your application scalable, you can, in fact, create RESTful services which are session based and can't live across multiple machines, or sticky session, or completely stateless.

As for scalability, many factors determine whether or not a service is scalable, and in fact a service can be scalable even if it requires a session (for example if your service has sticky sessions meaning that a session is hosted on one machine and the web visitor always comes back to the same machine where his session lives).

Stateless services make scaling out easier because it means that once a request has been processed, the next request from that web visitor does not necessarily have to come back to the same machine, which means you can (in theory) have as many machines as you want service the same kind of requests. If your traffic doubles in volume you just add the same number of machines and boom, you can now service twice as many requests.

In practice, however, things get complicated, and it is rarely possible to create completely stateless services. Most times, if your web visitors can update data, you have concurrency issues as two web visitors can change the same data and some kind of synchronization has to happen to prevent data corruption. In those cases, you might want to split your service so that the services that let you change data are NOT stateless, and those that let you extract data are. In most cases I would guess that most of the heavy lifting is in the data extraction side rather than data update, so this works out fine.

As for SOAP, I can only say that the world is moving towards REST as it is more light-weight.

于 2012-12-21T12:16:34.330 回答
1

无状态意味着您可以拥有任意数量的前端,每个前端都可以以相同的方式回答请求,无需处理文件系统会话,即

所以你可以扩展(添加任意数量的前端),这是无状态的最明显原因。

于 2012-12-21T12:01:25.460 回答