简体   繁体   English

静态变量如何在Web场中运行?

[英]How would a static variable work in a web farm?

I have a static class with a static dictionary to keep track of some stats. 我有一个带静态字典的静态类来跟踪一些统计数据。 Is this approach viable in a single and multi server environment? 这种方法在单一和多服务器环境中是否可行?

Not in a multi-server environment. 不在多服务器环境中。 Unless the dictionary isn't the .Net dictionary, but one that works against a database or some other outside storage. 除非字典不是.Net字典,而是针对数据库或其他外部存储的字典。 Also in a single server environment you should remember the Dictionary will be emptied with every IIS refresh. 此外,在单个服务器环境中,您应该记住,每次IIS刷新都会清空字典。

No, a static variable in memory in one server won't be visible or synchronized with other servers. 不,一台服务器内存中的静态变量将不可见或与其他服务器同步。

Since you mention "keeping track of stats", what many multi-instance server farms end up doing is post-processing locally gathered data into an aggregate view of activity across all the servers. 由于您提到“跟踪统计信息”,许多多实例服务器场最终会做的是将本地收集的数据后处理到所有服务器的活动聚合视图中。 Each server accumulates stats in memory for a short while (a few minutes), then writes the data from memory to a log file on the local file system. 每个服务器将内存中的统计信息累积一会儿(几分钟),然后将数据从内存写入本地文件系统上的日志文件。 Every few hours or daily a batch process copies the log files from each server in the farm to a central location and merges all the log records together to form one collection of data. 每隔几小时或每天批处理过程将日志文件从服务器场中的每个服务器复制到一个中心位置,并将所有日志记录合并在一起以形成一个数据集合。 One useful byproduct of this might be consolidating user activity across multiple servers if a user's web requests were served by different servers in the same farm (load balancing). 如果用户的Web请求由同一服务器场中的不同服务器提供服务(负载平衡),则此一个有用的副产品可能是在多个服务器之间整合用户活动。

Depending on the volume of data, this batch processing of log data may itself require quite a bit of computational power, with multiple machines crunching different subsets of the server logs, and then those intermediates getting reduced again. 根据数据量,这种日志数据的批处理本身可能需要相当多的计算能力,多台机器处理服务器日志的不同子集,然后这些中间体再次减少。

Google Sawzall is an example of a very large scale distributed data processing system. Google Sawzall是一个非常大规模的分布式数据处理系统的例子。 I believe Sawzall is (or was) used at Google to process server access logs to detect fraudulent ad click patterns. 我相信Sawzall曾(或曾在)用于处理服务器访问日志以检测欺诈性广告点击模式。

No, it's not viable in a web farm multi-server environment. 不,它在Web场多服务器环境中不可行。

The State Mode options are: 状态模式选项是:

  • InProc mode, which stores session state in memory on the Web server. InProc模式,用于将会话状态存储在Web服务器的内存中。 This is the default . 这是默认值

  • StateServer mode, which stores session state in a separate process called the ASP.NET state service. StateServer模式,它将会话状态存储在称为ASP.NET状态服务的单独进程中。 This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. 这可确保在重新启动Web应用程序时保留会话状态,并使会话状态可用于Web场中的多个Web服务器。

  • SQLServer mode stores session state in a SQL Server database. SQLServer模式将会话状态存储在SQL Server数据库中。 This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. 这可确保在重新启动Web应用程序时保留会话状态,并使会话状态可用于Web场中的多个Web服务器。

  • Custom mode, which enables you to specify a custom storage provider. 自定义模式,使您可以指定自定义存储提供程序。

  • Off (disables session state) 关闭 (禁用会话状态)

通常,这是您将使用Application状态的内容。

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

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