简体   繁体   English

有2个Web服务器,单例类有2个实例吗?

[英]With 2 web servers, will a singleton class have 2 instances?

有2个Web服务器,单例类有2个实例吗?

Both the web-servers will have separate instances of their application processes be it .net or java. 两个Web服务器都将具有其应用程序进程的单独实例,无论是.net还是Java。 So Yes , both the servers will have their individual instances of your singleton class. 所以, 是的 ,两个服务器都将拥有单个类的单独实例。

Regardless of the fact that these two web servers are two different physical machines, even if they are on the same server, they will definitely run entirely on different processes. 无论这两个Web服务器是两个不同的物理机器,即使它们位于同一台服务器上,它们肯定会完全在不同的进程上运行。 Each process will load its objects in memory separately from any other process. 每个进程都将其对象与其他任何进程分开加载到内存中。

specifically in case of asp.net - Even in the single web server, each site will cause separate instance of the Singleton class. specifically in case of asp.net - 即使在单个Web服务器中,每个站点也会导致Singleton类的单独实例。 Because each site in asp.net worker process is loaded in separate application domain, no two domains can interfere between each others' objects. 因为asp.net工作进程中的每个站点都加载在单独的应用程序域中,所以没有两个域可以干扰彼此的对象。 So in case of asp.net even the single web server having single asp.net worker process can/will have multiple instances of the singleton class each separate from another. 因此,在asp.net的情况下,即使具有单个asp.net工作进程的单个Web服务器也可以/将具有单个类的多个实例,每个实例与另一个实例分离。

Yes, you have a Singleton per each JVM and even class loaders. 是的,每个JVM甚至是类加载器都有一个Singleton。

See this When is a Singleton not a Singleton? 看到这个单身人士什么时候不是单身人士? article (for Java). 文章(针对Java)。

What do you mean by "2 web servers"? 你是什​​么意思“2 Web服务器”? Static field (in singleton) is scoped to Application Domain (in .net). 静态字段(单例)的作用域为Application Domain(在.net中)。

So if your two web servers run in two separate application domains, then yes, otherwise no. 因此,如果您的两个Web服务器在两个单独的应用程序域中运行,则为是,否则为否。

In fact you may well have 2 instances of a singleton calls in ONE web server, if its part of a webapp that is deployed twice. 实际上,如果它是部署了两次的webapp的一部分,那么在一个Web服务器中可能有2个单例调用实例。

In Java, the classloader is part of a class's identity. 在Java中,类加载器是类的标识的一部分。 You can load the same class twice with different classloaders, and all static fields will exist twice. 您可以使用不同的类加载器两次加载相同的类,并且所有静态字段将存在两次。 C# has a similar mechanism. C#有类似的机制。

It is possible to create a web server that will multiplex over everything - connections, listening sockets, even interfaces. 可以创建一个将在所有内容上复用的Web服务器 - 连接,侦听套接字,甚至接口。 It will be still one instance of the class, only one thread, and on top of that a pretty small memory footprint. 它仍然是该类的一个实例,只有一个线程,并且除此之外还有相当小的内存占用。 The caveat is, that while from most practical standpoints it will look like two servers, it will still be only one server (and if it crashes, it crashes whole...) 需要注意的是,从大多数实际角度来看,它看起来像两台服务器,它仍然只有一台服务器(如果它崩溃了,它会崩溃整个...)

This approach is not nearly as popular as multi-threaded webservers though, because while lighter on hardware, it is harder for developer to handle - you have to explicitly multiplex between everything, and juggle all connections in non-blocking calls. 这种方法并不像多线程Web服务器那样流行,因为虽然硬件较轻,但开发人员难以处理 - 您必须在所有内容之间进行显式多路复用,并在非阻塞调用中处理所有连接。 If you spawn some extra threads, the OS takes away a lot of work from you, allowing you to write a more feature-rich server easier. 如果你产生了一些额外的线程,操作系统会从你身上带走很多工作,让你更容易编写功能更丰富的服务器。

Of course even in a single-threaded server spawning of a second server in a separate task is still possible, just by the user/admin/whoever executing the binary again, with different config. 当然,即使在单线程服务器中,在单独的任务中产生第二台服务器仍然是可能的,只需用户/ admin /再次执行二进制文件的人,使用不同的配置。 It takes some pretty fancy programming to prevent that from happening. 它需要一些非常精美的编程来防止这种情况发生。

This is why JSP/Servlets provide the idea of "session" and "application" data. 这就是JSP / Servlets提供“会话”和“应用程序”数据的想法的原因。 These should be shared across servers in a multi-server environment. 这些应该在多服务器环境中的服务器之间共享。

As an extension of this question, specifically for .NET web apps, you also need to pay attention to SessionState handling. 作为此问题的扩展,特别是对于.NET Web应用程序,您还需要注意SessionState处理。 Assuming that sessions aren't "sticky" (user stays on one web server once session is established), you'll need to change SessionState to out-of-process. 假设会话不是“粘性”(一旦建立会话,用户就停留在一个Web服务器上),您需要将SessionState更改为进程外。 This can either be the ASP.NET session state server, or SQL Server, but the key point to remember is that SessionState isn't automatically shared across servers, unless you make it shared by going out-of-process. 这可以是ASP.NET会话状态服务器,也可以是SQL Server,但要记住的关键点是SessionState不会在服务器之间自动共享,除非您通过进程外共享它。 Also, anything you put in SessionState needs to be serializable; 此外,您在SessionState中放置的任何内容都需要可序列化; add the [Serializable] attribute to any classes you use in SessionState. 将[Serializable]属性添加到您在SessionState中使用的任何类。

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

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