Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 谷歌应用引擎是否支持redis发布和订阅_Google App Engine_Redis - Fatal编程技术网

Google app engine 谷歌应用引擎是否支持redis发布和订阅

Google app engine 谷歌应用引擎是否支持redis发布和订阅,google-app-engine,redis,Google App Engine,Redis,redis将在Google app engine(灵活环境)上发布和订阅工作 我正在使用node.js 出于这个问题,我理解GAE不支持websockets/realtime 您可以将您的redis实例连接到您的Google App Engine Flex应用程序实例,您只需要知道,它们都需要位于同一区域,并且具有授权访问的同一网络。您可以按照文档的说明来实现这一点 我将在这里描述一般步骤: 1。-创建一个Redis实例。注意Redis实例的区域、IP地址和端口 2。-创建一个HTTP服务器应用

redis将在Google app engine(灵活环境)上发布和订阅工作

我正在使用node.js


出于这个问题,我理解GAE不支持websockets/realtime

您可以将您的redis实例连接到您的Google App Engine Flex应用程序实例,您只需要知道,它们都需要位于同一区域,并且具有授权访问的同一网络。您可以按照文档的说明来实现这一点

我将在这里描述一般步骤:

1。-创建一个Redis实例。注意Redis实例的区域、IP地址和端口

2。-创建一个HTTP服务器应用程序,该应用程序从与Redis实例位于同一区域的App Engine flexible environment实例建立到Redis实例的连接。您的“app.yaml”看起来像这样:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

# update with Redis instance host IP, port
env_variables:
  REDISHOST:  redis-ip
  REDISPORT: 6379

# update with Redis instance network name
network:
  name: default 
3。-使用
gcloud app Deploy
部署应用程序


我已经在GAE Flex上使用Nodejs尝试了redis的发布/订阅行为,并为我做了以下工作:

在服务器端

'use strict';

const redis = require('redis');
const http = require('http');
const nconf = require('nconf');
const express = require('express');
const app = express();

// Read in keys and secrets. Using nconf use can set secrets via
// environment variables, command-line arguments, or a keys.json file.
nconf.argv().env().file('keys.json');

// Connect to a redis server provisioned over at
// Redis Labs. See the README for more info.
const pub = redis.createClient(
  nconf.get('redisPort') || '6379',
  nconf.get('redisHost') || '127.0.0.1',
  {
    'auth_pass': nconf.get('redisKey'),
    'return_buffers': true
  }
).on('error', (err) => console.error('ERR:REDIS:', err));


app.get('/', (req, res) => {

 // subscribe to the publisher

// publisher publish a message and exit
  pub.publish('notification', '{\'message\': '+req.query.message +'}', function(){

  res.send('message sent:'+ req.query.message);

  });

});

const server = app.listen(8080, () => {
  const host = server.address().address;
  const port = server.address().port;

  console.log(`Example app listening at http://${host}:${port}`);
});
'use strict';

const redis = require('redis');
const nconf = require('nconf');

nconf.argv().env().file('keys.json');


const sub = redis.createClient(
  nconf.get('redisPort') || '6379',
  nconf.get('redisHost') || '127.0.0.1',
  {
    'auth_pass': nconf.get('redisKey'),
    'return_buffers': true
  }
).on('error', (err) => console.error('ERR:REDIS:', err));


sub.on('message', function (channel, message) {
 console.log('Message: ' + message + ' on channel: ' + channel + ' is arrive!');
});
sub.subscribe('notification');
json是这样的(我使用redislab创建RedisIntance):

在客户端

'use strict';

const redis = require('redis');
const http = require('http');
const nconf = require('nconf');
const express = require('express');
const app = express();

// Read in keys and secrets. Using nconf use can set secrets via
// environment variables, command-line arguments, or a keys.json file.
nconf.argv().env().file('keys.json');

// Connect to a redis server provisioned over at
// Redis Labs. See the README for more info.
const pub = redis.createClient(
  nconf.get('redisPort') || '6379',
  nconf.get('redisHost') || '127.0.0.1',
  {
    'auth_pass': nconf.get('redisKey'),
    'return_buffers': true
  }
).on('error', (err) => console.error('ERR:REDIS:', err));


app.get('/', (req, res) => {

 // subscribe to the publisher

// publisher publish a message and exit
  pub.publish('notification', '{\'message\': '+req.query.message +'}', function(){

  res.send('message sent:'+ req.query.message);

  });

});

const server = app.listen(8080, () => {
  const host = server.address().address;
  const port = server.address().port;

  console.log(`Example app listening at http://${host}:${port}`);
});
'use strict';

const redis = require('redis');
const nconf = require('nconf');

nconf.argv().env().file('keys.json');


const sub = redis.createClient(
  nconf.get('redisPort') || '6379',
  nconf.get('redisHost') || '127.0.0.1',
  {
    'auth_pass': nconf.get('redisKey'),
    'return_buffers': true
  }
).on('error', (err) => console.error('ERR:REDIS:', err));


sub.on('message', function (channel, message) {
 console.log('Message: ' + message + ' on channel: ' + channel + ' is arrive!');
});
sub.subscribe('notification');
注意事项:

  • 要进行尝试,您必须通过url上的GET参数(名为“message”的参数)发送消息

  • 请记住,GAE通常会在可用实例之间平均分配请求,因此并非来自同一用户的所有请求都会发送到同一实例,但是,由于客户端将直接连接到redis客户端,因此只要is保持运行并侦听传入的消息,就无所谓了(这将取决于您如何实现应用程序的逻辑)

  • 如果您需要在服务器和客户端之间建立持久连接,请尝试使用支持的。如果您使用类似“socket.io”的内容这可以追溯到http长轮询(http long polling)有一个名为会话关联(sessionaffinity)Beta功能,允许您将同一用户的请求发送到同一实例

  • 要激活会话关联,请在app.yaml上使用此选项(必须在所有客户端上启用Cookie):

    请记住,文档警告:

    应用程序引擎应用程序必须始终容忍会话关联 中断,尤其是因为所有应用程序引擎实例 定期重新启动。启用会话关联还可以限制 App Engine负载平衡算法的有效性,并可能导致 您的实例可能会过载


    您还可以使用谷歌的云发布/订阅并使用谷歌云客户端库从应用程序内部控制一切,下面是一个如何实现这一点的示例。

    有人能帮您回答为什么要否决吗?谢谢。我知道redis可以与GAE一起使用-但我怀疑redis发布/订阅是否会与GAE?Co一起工作uld请确认。原因是-对于redis发布/订阅工作,我想可能需要GAE不支持的套接字,因此我提出了这个问题。我曾尝试在GAE Flex上使用redis发布/订阅,并为我工作过,我将编辑我的答案以提供更多详细信息。感谢您提供的详细和有用的答案。非常感谢。您能帮我吗如果你对这个问题投反对票——这将有助于其他人跟进。