降低Node.js服务器上的CPU利用率 [英] Reduction of CPU utilization on a nodejs server

查看:131
本文介绍了降低Node.js服务器上的CPU利用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究降低CPU利用率的有趣方法 在NodeJS服务器上.

I am looking into interesting ways to reduce CPU utilization on a NodeJS server.

在研究过程中,我发现了以下文章: http://engineering.linkedin.com/nodejs /blazing-fast-nodejs-10-10-performance-tips-linkedin-mobile

During my research I have found the following article: http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin-mobile

这些都是非常好的技巧,但是我有一个关于 提示#4.

These are all excellent tips but I have a question regarding hint#4.

这真的意味着用户正在请求"JavaScriptTemplate.html" 然后随后请求所有JSON(此处未实现)?

Does this really mean a user is requesting "JavaScriptTemplate.html" and then all the JSON is requested subsequently (which is not implemented here)?

假定所有动态内容都应在没有用户的情况下可用 互动(例如,在按钮单击事件上请求JSON)是什么 实现这一目标的最佳方法?我可以考虑加载其他JS依赖项 (requirejs),在其中执行函数以请求JSON内容.

Assuming that all the dynamic content should be available without user interaction (e.g. requesting JSON on a button click event) what is the best way to achive this? I can think of loading additional JS dependencies (requirejs) where functions are executed to request the JSON stuff.

由于我从未见过大型网站调用静态html文件,但 而是请求到其应用程序服务器的路由有多常见 上面的链接建议的解决方案?他们真的吗 使用服务器端模板浪费CPU利用率 大部分是静态文本内容?

Since I never see big websites to call static html files but instead requesting routes to their application servers how common is the solution suggested by the link above? Do they really use server side templates to waste CPU utilization on mostly static text content???

对于Node(expressJS),这必须是次优的方式,尤其是 如果要生成的HTML非常复杂...理想情况下 节点应仅用作提供JSON数据的API服务器.

For Node (expressJS) this must be a suboptimal way especially if the HTML to be produced is fairly complex... ideally Node should just operate as an API server providing JSON data.

您有什么想法要分享吗?

Do you have some ideas to share?

推荐答案

感谢分享帖子-这是一个很棒的帖子(对我来说非常及时).

Thanks for sharing the post - it is a great one (and very timely for me).

您实际上是在问两个问题-1)如何在没有客户端交互的情况下加载数据以呈现html客户端,以及2)当用户实际请求路由时如何向浏览器发送静态文件.

You're actually asking two questions - 1) how to load data to render html client-side without client interaction and 2) how to send a static file to a browser when user actually requests a route.

1)您需要运行所有初始化/数据加载/渲染代码以在页面加载后呈现页面.如果您在客户端中使用jQuery(就像大多数Web应用程序一样):

1) You need to run all the initialisation/data-loading/rendering code to render the page after the page has been loaded. If you use jQuery in the client (as most web applications do):

$(document).ready(function(){
    // Your code here
});

它只是从 jQuery文档中复制的.

大多数人使用主干/下划线在客户端中构建MVC层.尽管有很多更高级(而且似乎更强大)的客户端框架可以执行此操作,但这对夫妇为您提供了足够的功能,而不会限制您的选择或降低您在某些时候肯定需要的灵活性.下划线(无论如何都是骨干依赖项)以及许多非常有用的功能(如果花一小时阅读整个一页的手册,您会惊讶于JavaScript可能实现的功能)具有自己的模板,这些模板看似简单且相同时间非常强大,因为它们只运行模板中的所有javascript.

Most folks use backbone/underscore to build MVC layer in the client. Although there are a lot of much fancier (and seemingly more powerful) client-side frameworks to do so, this couple gives you just enough power without limiting your options or reducing your flexibility you will definitely need at some point. Underscore (which is backbone dependency anyway) in addition to many very useful functions (you will be surprised what's possible with JavaScript if you spend one hour to read through the whole one page manual) has its own templates which are deceptively simple and at the same time extremely powerful as they just run all javascript inside templates.

尽管将应用程序逻辑包含在模板中通常是一件坏事(因为下划线允许,大多数更高级和功能更强大"的模板引擎不允许),但是添加一些东西通常很方便,而且要好得多当您发现自己陷入困境(就像通常那样)时,可以使用模板中的逻辑,而不是重新设计大量的应用程序逻辑或添加其他模板.

Although it is usually a bad thing to have the application logic in templates (as underscore allows and most fancier and "more powerful" templating engines don't), it is often very handy and much better to be able to add some logic in template when you discover yourself in some tight corner (as you often will) than to redesign a lot of application logic or add additional templates.

此外,我的观点是避免在我写

Also, my opinion is to avoid using require.js or any other module loader (until you really must use them) as I wrote here.

2)您需要重写对所有路由的请求,以使用相同的静态html文件(或几个与路由相关的html文件)进行响应.根据您的首选项或应用程序要求,它可以是带有空正文的文件(在这种情况下,用户将看到空白页,直到加载数据并在正文中呈现/插入页面为止),一些欢迎页甚至是一些模板页,其中而不是数据,而是显示了一个纺车.

2) You need to rewrite requests to all routes to respond with the same static html file (or several route-dependent html files). Depending on your preferences or application requirements it can be the file with an empty body (in which case users will see a blank page until your data is loaded and page is rendered/inserted in body), some welcome page or even some template page where instead of data a spinning wheel is shown.

重写请求的方式取决于用于提供静态内容和代理请求的Web服务器.如果您使用Apache(因为它是同步的,所以它是Node的不大可能选择),则需要使用.htaccess文件.如果像大多数使用node的人那样使用Nginx,则需要在配置文件中的服务器块内使用rewrite指令,如下面的示例所示:

The way you rewrite requests depends on the web server you use to serve static content and to proxy requests. If you use Apache (an unlikely choice with Node, as it is synchronous) you need to use .htaccess files. If you use Nginx as most folks using node do, you need to use rewrite directive inside server block in config file like it is done in example below:

server {
    listen       80;
    server_name  example.com;
    root         html/example;

    access_log   logs/example.log;

    # location block below sends specific static assets from inside your app's
    # public directory when routes /img, /js, /css, /views are requested
    location ~ /(img|js|css|views)/ {
        rewrite ^(.*)$ /public/$1 break;
    }

    # location block below proxies all data requests (/api route) to your node app
    location /api {
        proxy_pass             http://localhost:3000/;
        proxy_redirect         http://localhost:3000/ http://example.com;
        proxy_connect_timeout  30s;
        proxy_read_timeout     30s;
        proxy_cookie_domain    localhost example.com;
        #proxy_http_version     1.1;
    }

    # location block below rewrites all other routes to a specific html file
    # that is sent to the client and that is supposed to load all JS and
    # static assets to render a page
    location / {
        rewrite ^(.*)$ /public/app.html;
    }
}

在客户端中呈现页面的方式(以及您从服务器请求的数据)取决于用户请求的路由(您可以在javascript中访问/更改以及设置/访问) /更改Cookie).在应用程序内部进行的所有导航(当用户单击任何按钮或内部链接时-您需要捕获所有单击事件)都不会产生对已加载的页面或静态资产的额外请求,仅将数据请求发送到服务器.

The way you render a page in the client (and the data you request from the server to do so) will depend on the route the user requested (which you can access/change in javascript as well as you can set/access/change cookies). All the navigation inside application (when the user clicks any buttons or internal links - you need to catch all click events) happens without additional requests for pages or static assets that are already loaded, only data requests are sent to the server.

希望对您有帮助.

nginx的建议配置仅在您不需要由机器人编入索引并且对需要静态html的其他Web应用程序可见的页面(例如Facebook)适用时.对于要建立索引的页面,您需要添加条件以不同方式路由来自机械手的请求(基于$ http_user_agent),并为这些路由呈现一些静态html.但这可以是不同的纯语义html(较小,没有设计图像,布局div/类,UI元素和javascript,以减少来自爬行机器人和Web应用程序的请求).

The suggested configuration for nginx is suitable only if you don't need any pages indexed by robots and visible to other web apps that need your static htmls, like facebook, e.g. For pages that you want to be indexed, you need to add conditions to route requests from robots differently (based on $http_user_agent) and also render some static htmls for those routes. But it can be a different purely semantic html (smaller, without design images, layout divs/classes, UI elements and javascript to reduce requests from crawling robots and web apps).

这篇关于降低Node.js服务器上的CPU利用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆