简体   繁体   English

如何在Google Cloud Platform中存储变量

[英]How to store variables in Google Cloud Platform

How can I store a variable in Google Cloud Platform? 如何在Google Cloud Platform中存储变量? Right now I am using a global variable that my routes share, but the values are off every time a request has been made to the route. 现在,我正在使用我的路线共享的全局变量,但是每次对路线提出请求时,这些值都会关闭。 I think it's because my server may have multiple instances running sharing the same variable. 我认为这是因为我的服务器可能有多个实例在运行共享同一变量。 My application is using Node.js/Express. 我的应用程序正在使用Node.js / Express。

My POST route sets the new_posted_time variable that /display-message uses: 我的POST路由设置了/display-message使用的new_posted_time变量:

var new_posted_time = 0;
app.post('/message', function (request, response) {
  message = request.body.Body;

  new_posted_time = new_posted_time + 1;

  console.log("This is the new time from POST: " + new_posted_time);
  response.send("<Response><Message>Heyyo!</Message></Response>");

});

My GET route gets the value for new_posted_time that was defined in /message . 我的GET路线获取/message中定义的new_posted_time的值。

app.get('/display-message', function(req,res){

    var last_updated_time = req.query.last_updated;

    function checkMessage() {
        var new_time = new_posted_time;
        console.log("Checking messaged with new time from GET: " + new_time);

        if(!last_updated_time || last_updated_time < new_time) {
            updateMessage(new_time);
        }
        else {
            console.log("No new messages at this time");
            myTimeout = setTimeout(function(){
                checkMessage(res);
            }, 1000 * 10); //10 seconds
        }
    }

    function updateMessage() {
        console.log("Updating message now");
        var output = {
            success: 1,
            data: message,
            old_stamp: last_updated_time,
            new_stamp: new_stamp
        };

        return res.json(output);
    }

    checkMessage();

});

So I need to be able to store the shared variable new_posted_time somewhere in Google's database because the value is not consistent every time my application runs due to many instances running (which is my guess). 因此,我需要能够将共享变量new_posted_time存储在Google数据库中的某个位置,因为由于许多实例正在运行,因此每次我的应用程序运行时该值都不相同(这是我的猜测)。

You should probably store 'state' like this in a persistent store like Google Cloud Datastore . 您应该将这样的“状态”存储在Google Cloud Datastore之类的持久性存储中 Here is an article on implementing counters with Google Cloud Datastore (its from the AppEngine Datastore API, but it is the same principle). 这是一篇有关使用Google Cloud Datastore 实现计数器的文章(它来自AppEngine Datastore API,但原理相同)。

Storing state in your instances is a bad idea because not only will you have consistency problems between instances, you will also lose data when an instance is shutdown automatically by AppEngine. 在实例中存储状态是一个坏主意,因为不仅实例之间会出现一致性问题,而且当AppEngine自动关闭实例时,还会丢失数据。

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

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