服务状态Service state

服务状态指的是服务正常运行所需的内存中数据或磁盘上数据。Service state refers to the in-memory or on disk data that a service requires to function. 这包括(例如)服务为正常工作而读取和写入的数据结构和成员变量等。It includes, for example, the data structures and member variables that the service reads and writes to do work. 这可能还包括存储在磁盘上的文件或其他资源,具体取决于服务的体系结构。Depending on how the service is architected, it could also include files or other resources that are stored on disk. 例如,数据库用于存储数据和事务日志的文件。For example, the files a database would use to store data and transaction logs.

例如,请设想一个计算器服务。As an example service, let's consider a calculator. 基本计算器服务需要两个数字并返回总和。A basic calculator service takes two numbers and returns their sum. 执行此计算不涉及成员变量或其他信息。Performing this calculation involves no member variables or other information.

现在,考虑相同的计算器,但采用其他方法来存储和返回上次所计算的总和。Now consider the same calculator, but with an additional method for storing and returning the last sum it has computed. 此服务现在有状态。This service is now stateful. 有状态表示它包含写入的状态(计算新的总和时)和读取的状态(请求其返回上次计算出的总和时)。Stateful means it contains some state that it writes to when it computes a new sum and reads from when you ask it to return the last computed sum.

在 Azure Service Fabric 中,第一个服务称为无状态服务。In Azure Service Fabric, the first service is called a stateless service. 第二个服务则称为有状态服务。The second service is called a stateful service.

存储服务状态Storing service state

状态可以外部化或与操作状态的代码共存。State can be either externalized or co-located with the code that is manipulating the state. 通常使用一个外部数据库或(在网络中不同计算机上运行或同一计算机进程外运行的)其他存储空间来实现状态的外部化。Externalization of state is typically done by using an external database or other data store that runs on different machines over the network or out of process on the same machine. 在我们的计算器示例中,数据存储可以是 SQL 数据库或 Azure 表存储实例。In our calculator example, the data store could be a SQL database or instance of Azure Table Store. 每个计算总和的请求均对此数据执行更新,且要求服务返回相应值的请求会导致从存储中提取当前值。Every request to compute the sum performs an update on this data, and requests to the service to return the value result in the current value being fetched from the store.

状态也可以与操作此状态的代码共存。State can also be co-located with the code that manipulates the state. Service Fabric 中的有状态服务通常是使用此模型生成的。Stateful services in Service Fabric are typically built using this model. Service Fabric 提供基础结构,以确保此状态高度可用、一致和持久,并且以这种方式构建的服务可轻松进行缩放。Service Fabric provides the infrastructure to ensure that this state is highly available, consistent, and durable, and that the services built this way can easily scale.

后续步骤Next steps

有关 Service Fabric 概念的详细信息,请参阅以下文章:For more information on Service Fabric concepts, see the following articles: