作者: | 来源:互联网 | 2023-09-16 10:05
IhaveanodejsApplicationrunningonserverwithnode-mysqlandexpress,AtfirstIfacedproblem
I have a nodejs Application running on server with node-mysql and express, At first I faced problem where some exceptions were not handled and the application would go down with network connectivity issues.
我有一个nodejs应用程序在运行带有node-mysql和express的服务器上运行,起初我遇到了一些问题,其中一些异常没有得到处理,应用程序会因网络连接问题而崩溃。
I handled all uncaught exceptions and the sever wouldn't go down this time but instead it would hang. I figured it was because I returned response only if query didn't raise any exception, so I handled all query related exceptions too.
我处理了所有未被捕获的异常,而且这次服务器不会停机,而是会挂起。我认为这是因为我只在查询没有引发任何异常时返回响应,所以我也处理了所有与查询相关的异常。
Next if MySQL server terminate connection for some reason my application wouldn't reconnect, i tried reconnecting but it would give an error related to "enqueue connection handshake or something". From another stack question I was supposed to use connection pool so if server terminates connection it will regain connectivity some how, which I did.
接下来,如果MySQL服务器由于某种原因终止连接我的应用程序将不会重新连接,我尝试重新连接但它会给出与“排队连接握手或某事”相关的错误。从另一个堆栈问题我应该使用连接池,所以如果服务器终止连接它将重新连接一些如何,我做了。
My here question is that each time I faced an issue I had to shut down whole application and thanks to nodejs where server is configured programmatically goes down too. Can I or better yet how can I decouple my Server and Application almost completely so that if I make some change in my application I wouldn't have to re-deploy?
我在这里的问题是每次遇到问题时我都必须关闭整个应用程序,并且感谢nodejs,其中以编程方式配置服务器也是如此。我可以或者更好的是如何几乎完全解耦我的服务器和应用程序,以便如果我在我的应用程序中进行一些更改,我将不必重新部署?
Specially for case that right now everything is okay and my application is constantly giving me connection pool error on server and in development version its working fine, so even if I restart my application I am not sure how will I face this problem again so I can properly diagnose this.
特别是对于现在一切都很好的情况我的应用程序不断给我连接池错误在服务器和开发版本它工作正常,所以即使我重新启动我的应用程序我不知道我将如何再次面对这个问题所以我可以正确诊断这一点。
Let me know if anyone needs more info regarding my question.
如果有人需要有关我的问题的更多信息,请告诉我。
1 个解决方案
1
Are you using a front-end framework to serve your application, or are you serving it all from server calls?
您是使用前端框架来为您的应用程序提供服务,还是通过服务器调用提供服务?
So fundamentally, if your server barfs for any reason (i.e. 500 error), you WANT to shut down and restart, because once your server is in that state, all of your in-transit data and your stack is in an unknown state. There's no way to correctly recover from that, so you are safer from both a server and an end-user point of view to shutdown the process and restart.
所以从根本上说,如果您的服务器由于任何原因(即500错误)barfs,您想要关闭并重新启动,因为一旦您的服务器处于该状态,您的所有传输中数据和您的堆栈都处于未知状态。无法从中正确恢复,因此从服务器和最终用户的角度来看,您可以更安全地关闭进程并重新启动。
You can minimise the impact of this by using something like Node's Cluster module, which allows you to fork child processes of your server and generate multiple instances of the same server, connected to the same database, allowing access on the same port etc, therefore, if your user (or your server), manages to hit an unhandled exception, it can kill the process and restart without shutting down your entire server.
您可以通过使用Node的Cluster模块来最小化这种影响,它允许您分叉服务器的子进程并生成同一服务器的多个实例,连接到同一个数据库,允许访问同一个端口等,因此,如果您的用户(或您的服务器)设法遇到未处理的异常,它可以终止进程并重新启动而无需关闭整个服务器。
Edit: Here's a snippet:
编辑:这是一个片段:
var cluster = require('cluster');
var threads = require('os').cpus().length;
if(cluster.isMaster) {
for(var i = 0; i
That being said, there's no way for you to run up your application and server separately if Node is serving your client content. Once you terminate your server, your Endpoints are down. If you really wanted to be able to keep a client-side application active and reboot your server, you'd have to entirely separate the logic, i.e. have your Application in a different project to your server, and use your server as API endpoints only.
话虽这么说,如果Node正在为您的客户端内容提供服务,则无法单独运行您的应用程序和服务器。终止服务器后,您的终端将关闭。如果您真的希望能够保持客户端应用程序处于活动状态并重新启动服务器,则必须完全分离逻辑,即将应用程序放在与服务器不同的项目中,并仅将服务器用作API端点。
As for Connection Pools in Node-mysql: I have never used that module so I couldn't say what best practice is there.
至于Node-mysql中的连接池:我从未使用过该模块,因此我无法说出最佳实践。