简体   繁体   English

用于在服务器和客户端之间实现心跳的模式

[英]Pattern to implement heartbeat between server and the client

I would like to implement a heartbeat functionality in the server that would periodically alert window clients of its liveness. 我想在服务器中实现一个心跳功能,该功能会定期向窗口客户端提醒其活跃度。 There are few ideas I am considering but would appreciate suggestions and examples/references 我正在考虑的想法很少,但会很感激建议和例子/参考

  • have a separate thread that would send a heartbeat to connected clients 有一个单独的线程,可以发送心跳到连接的客户端

  • have different types of heartbeat to indicating different states of the server (slow, fast, overwhelmed with clients, up and ready) 有不同类型的心跳来指示服务器的不同状态(缓慢,快速,不堪重负的客户端,up和ready)

  • perhaps let clients subscribe to different levels, have up heartbeat sent by default 也许让客户订阅不同级别,默认发送心跳

I would really like to see how it is done in practice, examples are best. 我真的很想看看它在实践中是如何完成的,例子是最好的。

EDIT 1 clients and server are not web-based! 编辑1客户端和服务器不是基于Web的! (server might migrate to the web, but I dont think it should change the protocol much) (服务器可能会迁移到Web,但我认为它不应该更改协议)

Using the pull model mentioned by Josh is the simplest approach. 使用Josh提到的拉模型是最简单的方法。 First of all, you'll get past a lot of security issues with that. 首先,你会遇到很多安全问题。 No need to worry about client-side firewalls. 无需担心客户端防火墙。 Plus, you don't need to worry about having to open the same port on each client, or opening dynamic ports and notifying the server about what port on what client is being used. 此外,您无需担心必须在每个客户端上打开相同的端口,或打开动态端口并通知服务器有关正在使用的客户端上的端口。

In addition, you won't have to maintain a subscriber list on the server. 此外,您不必在服务器上维护订户列表。 Plus, you will not need to worry about cleaning up the subscriber list if a client disconnects in a not so clean manner (application crash, power failure, etc). 此外,如果客户端以不太干净的方式(应用程序崩溃,电源故障等)断开连接,您将无需担心清理订户列表。

Basically, a simple polling from the client to a service on the server is the simplest and cleanest approach, IMHO. 基本上,从客户端到服务器上的服务的简单轮询是最简单和最干净的方法,恕我直言。 I've used it several times. 我已经好几次使用它了。 You can even have the polling interval be user-configurable if you choose. 如果选择,您甚至可以使用户可配置轮询间隔。

Edit: 编辑:

While I cannot provide a reference or example code, I'll describe what I've done in the past. 虽然我无法提供参考或示例代码,但我将描述我过去所做的事情。

Basically, I had a web service that, when queried, would return the state of the system. 基本上,我有一个Web服务,当查询时,它将返回系统的状态。 This web service obviously ran on the server. 这个Web服务显然在服务器上运行。 The clients, when started, would launch a separate thread that would query the web service every 30 seconds to get the state of the server system. 客户端在启动时将启动一个单独的线程,该线程每30秒查询一次Web服务以获取服务器系统的状态。 Then, the UI would be updated to indicate that state. 然后,UI将被更新以指示该状态。 Once that task was complete the thread would go back to sleep for 30 seconds. 一旦该任务完成,该线程将重新进入休眠状态30秒。 The update time was configurable through a configuration file. 更新时间可通过配置文件进行配置。 Just make sure to trap errors so that if the request to the service fails for a reason other than the server being down, the entire application doesn't crash. 只需确保捕获错误,以便如果服务请求因服务器关闭以外的原因而失败,则整个应用程序不会崩溃。

This is almost surely overkill, but there is an entire (and very lively) discipline looking at classes of failure detectors, what kinds of assurances they can provide, and how they can practically be implemented. 这几乎肯定是矫枉过正,但是有一整套(并且非常生动)的学科正在研究故障探测器的类别,它们可以提供什么样的保证,以及它们如何实际实施。 For those wanting to go a bit further than this questioner implies, have a look at: 对于那些想要比这个提问者暗示更进一步的人,请看看:

Book Series - 
Book Title  - Distributed Computing
Chapter Title  - On the Impact of Fast Failure Detectors on Real-Time Fault-Tolerant Systems
First Page  - 354
Last Page  - 369
Copyright  - 2002
Author  - Marcos K. Aguilera
Author  - Gérard Le Lann
Author  - Sam Toueg
DOI  - 10.1007/3-540-36108-1_24
Link  - http://www.springerlink.com/content/e03yf4etbnle9728

Book Title  - Distributed Algorithms
Chapter Title  - Heartbeat: A timeout-free failure detector for quiescent reliable communication
First Page  - 126
Last Page  - 140
Copyright  - 1997
Author  - Marcos Kawazoe Aguilera
Author  - Wei Chen
Author  - Sam Toueg
DOI  - 10.1007/BFb0030680
Link  - http://www.springerlink.com/content/dj5n71hl17841416

Also, it's not clear from the question, but if the language in question is Java, an excellent resource on this topic is: Introduction to Reliable Distributed Programming , with lots of excellent sample code. 此外,问题还不清楚,但如果所讨论的语言是Java,那么这个主题的优秀资源是: 可靠的分布式编程简介 ,有很多优秀的示例代码。

What kind of client are we talking about? 我们在谈论什么样的客户? Windows client or asp.net? Windows客户端还是asp.net? There are two very general patterns for this. 这有两种非常普遍的模式。 You can either push or pull the data. 您可以推送或拉取数据。 Pushing doesn't work if your on the internet you'll run into firewalls and nats. 如果您在互联网上遇到防火墙和nat,推送不起作用。 So you end up with a third variation where the client initates the connection, and the server leaves the connection open to send information back and forth. 因此,您最终得到第三个变体,其中客户端启动连接,服务器保持连接打开以来回发送信息。

You need to provide a lot more information, are we talking about internet or intranet? 您需要提供更多信息,我们是在谈论互联网还是内部网? What .net framework are you targeting? 您定位的.net框架是什么? How many clients are you talking? 你在说几个客户? A solution that can handle a dozen clients (Especially in a push model or the third model) could look very different from a solution which can scale to thousands of clients. 可以处理十几个客户端的解决方案(特别是在推送模型或第三个模型中)可能与可扩展到数千个客户端的解决方案看起来非常不同。

The easiest solution is to do polling from the client side, which unless you want the server to have instant communication to the client is the way to go. 最简单的解决方案是从客户端进行轮询,除非您希望服务器与客户端进行即时通信,否则这样做是可行的。 And a heart beat is not instant communication. 心跳不是即时沟通。

Edit 编辑

Ok you indicated sockets, are you really sure you want to do lower level network type programing? 好的,你指的是套接字,你真的确定要做更低级别的网络类型编程吗? Why not build upon existing network strategies such as HTTP. 为什么不建立在现有的网络策略上,比如HTTP。 You can do a simple remoting service over HTTP which will let you bypass firewalls. 您可以通过HTTP执行简单的远程服务,以便绕过防火墙。 Or even better if your server is a web server then just setup a plain old xml service. 或者甚至更好,如果您的服务器是一个Web服务器,那么只需设置一个普通的旧xml服务。

I don't have any examples I can share of this, but there should be plenty around. 我没有任何可以分享的例子,但应该有很多。

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

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